Skip to content
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

Merged
merged 5 commits into from
Aug 23, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Aug 22, 2023

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.

@AlexWaygood AlexWaygood changed the title Fix edge-case stubtest crash on Python <=3.10 involving using a bitwise or-ing of enum.Flag members as a parameter default Fix edge-case stubtest crash on Python <=3.10 when a bitwise or-ing of enum.Flag members is used as a parameter default 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__
Copy link
Member Author

@AlexWaygood AlexWaygood Aug 22, 2023

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

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

@AlexWaygood AlexWaygood changed the title Fix edge-case stubtest crash on Python <=3.10 when a bitwise or-ing of enum.Flag members is used as a parameter default Fix edge-case stubtest crashes when an instanace of an enum.Flag that is not a member of that enum.Flag is used as a parameter default Aug 22, 2023
@srittau
Copy link
Contributor

srittau commented Aug 23, 2023

Thanks for the analysis and fix, Alex!

Copy link
Collaborator

@hauntsaninja hauntsaninja left a 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!

@hauntsaninja hauntsaninja merged commit 48835a3 into python:master Aug 23, 2023
12 checks passed
@AlexWaygood AlexWaygood deleted the stubtest-IntFlag-crash branch August 23, 2023 07:12
@AlexWaygood AlexWaygood changed the title Fix edge-case stubtest crashes when an instanace of an enum.Flag that is not a member of that enum.Flag is used as a parameter default 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 Aug 23, 2023
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.

mypyc-related stubtest crash
3 participants