Skip to content

Conversation

@isHarryh
Copy link
Contributor

@isHarryh isHarryh commented Feb 22, 2025

Summary

This PR makes code quality improvements to the files module, such as type hints and typo corrections.

Commit Details

1. fix: better type hint in EndianBinaryReader/Writer

In EndianBinaryReader, use -> Tuple[xxx, ...] instead of -> Tuple[xxx], where the latter one is a common mistake.

In EndianBinaryWriter, use value: Sequence instead of value: list because some invocations pass a tuple argument instead of list.

2. refactor(files): use assertion for version-specific assignment

Use assert xxx is not None before value assignment.

This is also the practice adopted by MeshHelper and other classes, which can correctly satisfy the NoneType checking.

3. chore(files): fix typo and remove legacy code

In File class:

  • class File(object) can be simplified to class File (Python 3).
  • self.environment = self.environment = ... causes a duplicated self-assignment.

In ContainerHelper, method __or__ cannot be used anymore since 5ac0a62 changed the constructor of this class.

4. fix(files): overall type hint corrections

Correct the type hints in the files module.

Refactors BundleFile.get_version_tuple:

  • Uses (next(...), next(...), next(...)) instead of tuple(map(...)) to respect the returned type -> Tuple[int, int, int].
  • Prevents AttributeError by validating the regex match before accessing groups.

Updates with 2 commits:

1. fix(files): use generic type in EndianBinaryWriter

Applies generic type to Sequence and Callable in EndianBinaryWriter.

2. feat(config): unset default fallback version

Sets the FALLBACK_UNITY_VERSION to None (unset).

Raises UnityVersionFallbackError when the fallback version is unset or invalid.

Refactors the warning mechanism using the warnings module.

Optimizes the documents string of the constants in the config.

Copy link
Owner

@K0lb3 K0lb3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.
I made most of these changes in the 1.21 files branch and the rewrite as well too.
As that branch is going to be shelved, I'm happy about your PR for fixing some of the hint and check issues of the current version.

@isHarryh
Copy link
Contributor Author

Adds 2 commit:

1. fix(files): use generic type in EndianBinaryWriter

Applies generic type to Sequence and Callable in EndianBinaryWriter.

2. feat(config): unset default fallback version

Sets the FALLBACK_UNITY_VERSION to None (unset).

Raises UnityVersionFallbackError when the fallback version is unset or invalid.

Refactors the warning mechanism using the warnings module.

Optimizes the documents string of the constants in the config.

@isHarryh isHarryh changed the title Improve files module Improve config and files module Feb 25, 2025
Copy link
Owner

@K0lb3 K0lb3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot 👍

@K0lb3 K0lb3 merged commit 7e7542b into K0lb3:master Feb 25, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants