Avoid setattr/getattr with fixed format cache#20826
Merged
ilevkivskyi merged 3 commits intopython:masterfrom Feb 16, 2026
Merged
Avoid setattr/getattr with fixed format cache#20826ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi merged 3 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
JukkaL
approved these changes
Feb 16, 2026
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Makes sense, the new code should be significantly more efficient when compiled.
mypy/nodes.py
Outdated
| setattr(node, flag, True) | ||
| def read_flags(data: ReadBuffer, num_flags: int) -> list[bool]: | ||
| packed = read_int(data) | ||
| return [packed & 1 << i != 0 for i in range(num_flags)] |
Collaborator
There was a problem hiding this comment.
Add parenthesees, since the precedence is otherwise a bit hard to see.
mypy/nodes.py
Outdated
| setattr(node, flag, True) | ||
| def read_flags(data: ReadBuffer, num_flags: int) -> list[bool]: | ||
| packed = read_int(data) | ||
| return [packed & 1 << i != 0 for i in range(num_flags)] |
Collaborator
There was a problem hiding this comment.
Use explicit parentheses to make precedence more obvious?
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a small optimization, and something that bothered me for a while. I write bit packing in Python because we have mypyc and we should use it (it should write relatively efficient code for this). Implementation is really basic, I only allow at most 26 flags, which will hopefully be enough for few years. Btw
Varhas 21 flags, I am quite sure some of those can be cleaned up.