Skip to content
Open
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: 12 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,18 @@ def check_call(
object_type,
original_type=callee,
)
elif isinstance(callee, LiteralType):
return self.check_call(
callee.fallback,
args,
arg_kinds,
context,
arg_names,
callable_node,
callable_name,
object_type,
original_type=original_type,
)
elif isinstance(callee, UninhabitedType):
ret = UninhabitedType()
ret.ambiguous = callee.ambiguous
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -2983,3 +2983,27 @@ def f(x: SE | None) -> None:
else:
reveal_type(x) # N: Revealed type is "__main__.SE | None"
[builtins fixtures/primitives.pyi]


[case testCallableEnumNarrowing]
# See: https://github.com/python/mypy/issues/17222

import enum

class Foo(enum.Enum):
FOO1 = enum.auto()
FOO2 = enum.auto()

def __call__(self) -> str:
return self.name

foo = Foo.FOO1

match foo:
case Foo.FOO1:
foo()
reveal_type(foo) # N: Revealed type is "Literal[__main__.Foo.FOO1]"
case _:
foo()
reveal_type(foo) # N: Revealed type is "Literal[__main__.Foo.FOO2]"
[builtins fixtures/primitives.pyi]
Loading