Skip to content

Move __getattribute__ to FakeFileWrapper - #1379

Open
kabilanpichaipillai wants to merge 3 commits into
pytest-dev:mainfrom
kabilanpichaipillai:refactor-pull-up-getattribute
Open

kabilanpichaipillai wants to merge 3 commits into
pytest-dev:mainfrom
kabilanpichaipillai:refactor-pull-up-getattribute

Conversation

@kabilanpichaipillai

Copy link
Copy Markdown
Contributor

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.

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.
@kabilanpichaipillai

Copy link
Copy Markdown
Contributor Author

@mrbean-bremen - The previous pytype failure has been fixed in the latest commit by hiding __getattribute__ from type checkers, and pytype now passes.
The remaining failure appears to be RealOsModuleTest.test_lexists_with_trailing_separator_macos on macOS 3.12. This test checks the real OS rather than pyfakefs code, and it passed on both the previous commit and main, so it seems to be a flaky test.

Could you please re-run the failed job when you have a chance? Thank you!

@kabilanpichaipillai

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread pyfakefs/fake_file.py
# 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 branch has not been deployed

No deployments
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