-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
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
Conversation
or
-ing of enum.Flag
members as a parameter defaultor
-ing of enum.Flag
members is used as a parameter default
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__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
class Flag(Enum): | |
def __or__(self: _T, other: Union[int, _T]) -> _T: pass |
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
Thanks for the analysis and fix, Alex! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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.