Repository navigation
Move __getattribute__ to FakeFileWrapper - #1379
kabilanpichaipillai wants to merge 3 commits into
Conversation
FakeTextFileWrapper and FakeBinaryFileWrapper had identical __getattribute__ overrides; define it once in their base class.
Defining __getattribute__ in FakeFileWrapper made pytype treat all of its attributes as dynamic, which caused a bad-return-type error in fileno(). Define it under "if not TYPE_CHECKING" so runtime behavior is unchanged while type checkers keep the real attribute types.
|
@mrbean-bremen - The previous Could you please re-run the failed job when you have a chance? Thank you! |
|
@mrbean-bremen - After merged main branch into this PR, the CI jobs were triggered again. This time, the failure occurred in Testsuite / tests (macOS-latest, 3.15), while the macOS 3.12 test suite completed successfully. |
| # over those of the io base class a subclass derives from; | ||
| # everything else is delegated to the wrapped stream via __getattr__. | ||
| # Hidden from type checkers, which would otherwise treat all | ||
| # attributes of the class as dynamic. |
There was a problem hiding this comment.
This is a bit over-documented for my taste. Just state what the method does (e.g. delegate unhandled methods), probably in a doc string, no need to explain the obvious in the three lines of code.
Same with the type checking. You may add a short comment after the if not TYPE_CHECKING: statement (something like "avoid override warnings"), but there is no need for the comment inside the function.
Apart from that it looks good.
This PR removes the duplicated getattribute implementation from FakeTextFileWrapper and FakeBinaryFileWrapper by moving it into their common base class, FakeFileWrapper.
Both subclasses now inherit the implementation from FakeFileWrapper.
Changed the call to object.getattribute(self, name) to avoid recursive calls after moving the method.
Added a comment explaining why the override is needed.
Since FakeFileWrapper is only used through its subclasses, this change does not affect existing behaviour. The existing tests pass locally on Windows without any modifications, and pre-commit checks also pass.
This is a small, independent refactoring PR related to my university assignment and is part of the same assignment as #1378.