Fix edge-case stubtest crashes when an instance of an enum.Flag that is not a member of that enum.Flag is used as a parameter default#15933
Merged
hauntsaninja merged 5 commits intopython:masterfrom Aug 23, 2023
Conversation
or-ing of enum.Flag members as a parameter defaultor-ing of enum.Flag members is used as a parameter default
AlexWaygood
commented
Aug 22, 2023
Comment on lines
+112
to
+141
| import sys | ||
| from typing import Any, TypeVar, Iterator | ||
|
|
||
| _T = TypeVar('_T') | ||
|
|
||
| class EnumMeta(type): | ||
| def __len__(self) -> int: pass | ||
| def __iter__(self: type[_T]) -> Iterator[_T]: pass | ||
| def __reversed__(self: type[_T]) -> Iterator[_T]: pass | ||
| def __getitem__(self: type[_T], name: str) -> _T: pass | ||
|
|
||
| class Enum(metaclass=EnumMeta): | ||
| def __new__(cls: type[_T], value: object) -> _T: pass | ||
| def __repr__(self) -> str: pass | ||
| def __str__(self) -> str: pass | ||
| def __format__(self, format_spec: str) -> str: pass | ||
| def __hash__(self) -> Any: pass | ||
| def __reduce_ex__(self, proto: Any) -> Any: pass | ||
| name: str | ||
| value: Any | ||
|
|
||
| class Flag(Enum): | ||
| def __or__(self: _T, other: _T) -> _T: pass | ||
| def __and__(self: _T, other: _T) -> _T: pass | ||
| def __xor__(self: _T, other: _T) -> _T: pass | ||
| def __invert__(self: _T) -> _T: pass | ||
| if sys.version_info >= (3, 11): | ||
| __ror__ = __or__ | ||
| __rand__ = __and__ | ||
| __rxor__ = __xor__ |
Member
Author
There was a problem hiding this comment.
This was required because the standard enum fixture is missing many methods on enum.Flag, and this was causing stubtest to complain about "missing methods in the stub" when I created a test case that used enum.Flag
mypy/test-data/unit/lib-stub/enum.pyi
Lines 36 to 37 in 7141d6b
I initially tried adding the missing methods to the standard enum fixture, but this caused many unrelated tests to fail
or-ing of enum.Flag members is used as a parameter defaultenum.Flag that is not a member of that enum.Flag is used as a parameter default
Contributor
|
Thanks for the analysis and fix, Alex! |
hauntsaninja
approved these changes
Aug 23, 2023
Collaborator
hauntsaninja
left a comment
There was a problem hiding this comment.
Nice, thanks for debugging this!
enum.Flag that is not a member of that enum.Flag is used as a parameter defaultenum.Flag that is not a member of that enum.Flag is used as a parameter default
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.
Fixes #15923.
Note: the test cases I've added reproduce the crash, but only if you're using a compiled version of mypy. (Some of them only repro the crash on <=py310, but some repro it on py311+ as well.)
We run stubtest tests in CI with compiled mypy, so they do repro the crash in the context of our CI.