Skip to content

Move __getattribute__ to FakeFileWrapper - #1379

Merged
mrbean-bremen merged 4 commits into
pytest-dev:mainfrom
kabilanpichaipillai:refactor-pull-up-getattribute
Nov 23, 2026
Merged

mrbean-bremen merged 4 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 Resolved
Replace the explanatory comment with a short docstring and a brief
note on the TYPE_CHECKING guard, as suggested in review.
@mrbean-bremen

Copy link
Copy Markdown
Member

The cancelled tests are a GitHub problem - they don't have enough macOS runners given the increased demand due to all the AI-generated PRs.

@mrbean-bremen
mrbean-bremen merged commit 4b2215f into pytest-dev:main Nov 23, 2026
98 of 104 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