Skip to content

Improve our support for hasattr()#20914

Merged
ilevkivskyi merged 5 commits intopython:masterfrom
ilevkivskyi:redef-hasattr
Feb 27, 2026
Merged

Improve our support for hasattr()#20914
ilevkivskyi merged 5 commits intopython:masterfrom
ilevkivskyi:redef-hasattr

Conversation

@ilevkivskyi
Copy link
Member

Our support for hasattr() is quite fragile/limited. In particular, it didn't work if there was an existing narrowing in the current frame. For --allow-redefinition-new this was a critical hit, since it can do binder.put() on initial assignment, thus making our hasattr() support non-functional.

This (somewhat ad-hoc) fix seems to restore parity for hasattr() support. Also it may even improve hasattr() support n regular mode in some edge cases

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Right before merging this I discovered there is another edge case that is exacerbated by --allow-redefinition-new, this time a false negative. Fix is a bit ugly to avoid performance penalty. I also try to micro-optimize try_getting_instance_fallback() some more.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi merged commit 0177c0d into python:master Feb 27, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the redef-hasattr branch February 27, 2026 18:46
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