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 enum truthiness for StrEnum #18379

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,19 +648,14 @@ def _remove_redundant_union_items(items: list[Type], keep_erased: bool) -> list[
return items


def _get_type_method_ret_type(t: Type, *, name: str) -> Type | None:
t = get_proper_type(t)

def _get_type_method_ret_type(t: ProperType, *, name: str) -> Type | None:
# For Enum literals the ret_type can change based on the Enum
# we need to check the type of the enum rather than the literal
if isinstance(t, LiteralType) and t.is_enum_literal():
t = t.fallback

if isinstance(t, Instance):
sym = t.type.get(name)
# Fallback to the metaclass for the lookup when necessary
if not sym and (m := t.type.metaclass_type):
sym = m.type.get(name)
if sym:
sym_type = get_proper_type(sym.type)
if isinstance(sym_type, CallableType):
Expand Down Expand Up @@ -733,7 +728,10 @@ def false_only(t: Type) -> ProperType:
if ret_type:
if not ret_type.can_be_false:
return UninhabitedType(line=t.line)
elif isinstance(t, Instance) and t.type.is_final:
elif isinstance(t, Instance):
if t.type.is_final or t.type.is_enum:
return UninhabitedType(line=t.line)
elif isinstance(t, LiteralType) and t.is_enum_literal():
return UninhabitedType(line=t.line)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metaclasses are a red herring, enums are just effectively final, so this is safe to do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it's not about the order of operations? I.e. first __bool__ gets checked, then __len__ and then the same thing happens for the metaclass.

But here __bool__ gets checked and then immediately it goes to the metaclass.

I think falling back to the metaclass is correct, it just happens too early.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I had a read of the blog post, maybe metaclasses aren't involved in truthiness after all and it just so happens that the default is to return True if you have neither a __bool__ or __len__, although I'm a little confused about the purpose of __bool__ on EnumMeta, when that is the default truthiness anyways.

Copy link
Contributor

@Daverball Daverball Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked, looks like the metaclass does matter, but only for the truthiness of type[Enum], not the instances.

>>> from enum import EnumMeta
>>> EnumMeta.__bool__ = lambda self: False
>>> from enum import Enum                                                                   
>>> bool(Enum)
False
>>> class Foo(Enum):
...     a = "a"
...     
>>> bool(Foo.a)                                                                             
True
>>> 

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. It would also affect bool(Foo)

... continuing from your monkey patch ...
>>> bool(Foo)
False
>>> type(Foo.a).__mro__
(<enum 'Foo'>, <enum 'Enum'>, <class 'object'>)
>>> type(Foo).__mro__
(<class 'enum.EnumType'>, <class 'type'>, <class 'object'>)


new_t = copy_type(t)
Expand Down
93 changes: 83 additions & 10 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,100 @@ def infer_truth(truth: Truth) -> None:
[case testEnumTruthyness]
# mypy: warn-unreachable
import enum
from typing_extensions import Literal

class E(enum.Enum):
x = 0
if not E.x:
"noop"
zero = 0
one = 1

def print(s: str) -> None: ...

if E.zero:
print("zero is true")
if not E.zero:
print("zero is false") # E: Statement is unreachable

if E.one:
print("one is true")
if not E.one:
print("one is false") # E: Statement is unreachable

def main(zero: Literal[E.zero], one: Literal[E.one]) -> None:
if zero:
print("zero is true")
if not zero:
print("zero is false") # E: Statement is unreachable
if one:
print("one is true")
if not one:
print("one is false") # E: Statement is unreachable
[builtins fixtures/tuple.pyi]
[out]
main:6: error: Statement is unreachable

[case testEnumTruthynessCustomDunderBool]
# mypy: warn-unreachable
import enum
from typing_extensions import Literal

class E(enum.Enum):
x = 0
zero = 0
one = 1
def __bool__(self) -> Literal[False]:
return False
if E.x:
"noop"

def print(s: str) -> None: ...

if E.zero:
print("zero is true") # E: Statement is unreachable
if not E.zero:
print("zero is false")

if E.one:
print("one is true") # E: Statement is unreachable
if not E.one:
print("one is false")

def main(zero: Literal[E.zero], one: Literal[E.one]) -> None:
if zero:
print("zero is true") # E: Statement is unreachable
if not zero:
print("zero is false")
if one:
print("one is true") # E: Statement is unreachable
if not one:
print("one is false")
[builtins fixtures/enum.pyi]

[case testEnumTruthynessStrEnum]
# mypy: warn-unreachable
import enum
from typing_extensions import Literal

class E(enum.StrEnum):
empty = ""
not_empty = "asdf"

def print(s: str) -> None: ...

if E.empty:
print("empty is true")
if not E.empty:
print("empty is false")

if E.not_empty:
print("not_empty is true")
if not E.not_empty:
print("not_empty is false")

def main(empty: Literal[E.empty], not_empty: Literal[E.not_empty]) -> None:
if empty:
print("empty is true")
if not empty:
print("empty is false")
if not_empty:
print("not_empty is true")
if not not_empty:
print("not_empty is false")
[builtins fixtures/enum.pyi]
[out]
main:9: error: Statement is unreachable

[case testEnumUnique]
import enum
Expand Down
4 changes: 3 additions & 1 deletion test-data/unit/fixtures/enum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class tuple(Generic[T]):
def __getitem__(self, x: int) -> T: pass

class int: pass
class str: pass
class str:
def __len__(self) -> int: pass

class dict: pass
class ellipsis: pass
Loading