Skip to content

Commit b721bdd

Browse files
committed
Merge remote-tracking branch 'upstream/master' into default-native-parser
2 parents 443151a + 0d30f08 commit b721bdd

7 files changed

Lines changed: 63 additions & 4 deletions

File tree

mypy-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ mypy_extensions>=1.0.0
66
pathspec>=1.0.0
77
tomli>=1.1.0; python_version<'3.11'
88
librt>=0.15.0; platform_python_implementation != 'PyPy'
9-
ast-serialize>=0.11.0,<1.0.0
9+
ast-serialize>=0.11.1,<1.0.0

mypy/checkexpr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,18 @@ def check_call(
16571657
object_type,
16581658
original_type=callee,
16591659
)
1660+
elif isinstance(callee, LiteralType):
1661+
return self.check_call(
1662+
callee.fallback,
1663+
args,
1664+
arg_kinds,
1665+
context,
1666+
arg_names,
1667+
callable_node,
1668+
callable_name,
1669+
object_type,
1670+
original_type=original_type,
1671+
)
16601672
elif isinstance(callee, UninhabitedType):
16611673
ret = UninhabitedType()
16621674
ret.ambiguous = callee.ambiguous

mypy/semanal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
)
260260
from mypy.typeops import function_type, get_type_vars, try_getting_str_literals_from_type
261261
from mypy.types import (
262+
ANNOTATED_TYPE_NAMES,
262263
ASSERT_TYPE_NAMES,
263264
DATACLASS_TRANSFORM_NAMES,
264265
DEPRECATED_TYPE_NAMES,
@@ -1190,11 +1191,15 @@ def is_expected_self_type(self, typ: Type, is_classmethod: bool) -> bool:
11901191
sym = self.lookup_qualified(typ.name, typ, suppress_errors=True)
11911192
if sym is not None and sym.fullname in TYPE_NAMES and typ.args:
11921193
return self.is_expected_self_type(typ.args[0], is_classmethod=False)
1194+
if sym is not None and sym.fullname in ANNOTATED_TYPE_NAMES and typ.args:
1195+
return self.is_expected_self_type(typ.args[0], is_classmethod=True)
11931196
return False
11941197
if isinstance(typ, TypeVarType):
11951198
return typ == self.type.self_type
11961199
if isinstance(typ, UnboundType):
11971200
sym = self.lookup_qualified(typ.name, typ, suppress_errors=True)
1201+
if sym is not None and sym.fullname in ANNOTATED_TYPE_NAMES and typ.args:
1202+
return self.is_expected_self_type(typ.args[0], is_classmethod=False)
11981203
return sym is not None and sym.fullname in SELF_TYPE_NAMES
11991204
return False
12001205

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ requires = [
1515
"types-psutil",
1616
"types-setuptools",
1717
# required to work around a mypyc import bug
18-
"ast-serialize>=0.11.0,<1.0.0",
18+
"ast-serialize>=0.11.1,<1.0.0",
1919
]
2020
build-backend = "setuptools.build_meta"
2121

@@ -59,7 +59,7 @@ dependencies = [
5959
"pathspec>=1.0.0",
6060
"tomli>=1.1.0; python_version<'3.11'",
6161
"librt>=0.15.0; platform_python_implementation != 'PyPy'",
62-
"ast-serialize>=0.11.0,<1.0.0",
62+
"ast-serialize>=0.11.1,<1.0.0",
6363
]
6464
dynamic = ["version"]
6565

test-data/unit/check-enum.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,3 +2983,27 @@ def f(x: SE | None) -> None:
29832983
else:
29842984
reveal_type(x) # N: Revealed type is "__main__.SE | None"
29852985
[builtins fixtures/primitives.pyi]
2986+
2987+
2988+
[case testCallableEnumNarrowing]
2989+
# See: https://github.com/python/mypy/issues/17222
2990+
2991+
import enum
2992+
2993+
class Foo(enum.Enum):
2994+
FOO1 = enum.auto()
2995+
FOO2 = enum.auto()
2996+
2997+
def __call__(self) -> str:
2998+
return self.name
2999+
3000+
foo = Foo.FOO1
3001+
3002+
match foo:
3003+
case Foo.FOO1:
3004+
foo()
3005+
reveal_type(foo) # N: Revealed type is "Literal[__main__.Foo.FOO1]"
3006+
case _:
3007+
foo()
3008+
reveal_type(foo) # N: Revealed type is "Literal[__main__.Foo.FOO2]"
3009+
[builtins fixtures/primitives.pyi]

test-data/unit/check-selftype.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,24 @@ reveal_type(D.meth()) # N: Revealed type is "__main__.D"
15491549
reveal_type(D.bad()) # N: Revealed type is "Never"
15501550
[builtins fixtures/classmethod.pyi]
15511551

1552+
[case testTypingSelfAnnotatedType]
1553+
# See: https://github.com/python/mypy/issues/21917
1554+
1555+
from typing import Self
1556+
from typing_extensions import Annotated
1557+
1558+
class C:
1559+
def foo(self: Annotated[Self, "some_metadata"], x: int) -> None:
1560+
...
1561+
1562+
@classmethod
1563+
def bar(cls: Annotated[type[Self], "some_metadata"], x: int) -> None:
1564+
...
1565+
1566+
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C] (self: Self`2, x: builtins.int)"
1567+
reveal_type(C.bar) # N: Revealed type is "def (x: builtins.int)"
1568+
[builtins fixtures/tuple.pyi]
1569+
15521570
[case testTypingSelfOverload]
15531571
from typing import Self, overload, Union
15541572

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# pip-compile --allow-unsafe --output-file=test-requirements.txt --strip-extras test-requirements.in
66
#
7-
ast-serialize==0.11.0
7+
ast-serialize==0.11.1
88
# via -r mypy-requirements.txt
99
attrs==26.1.0
1010
# via -r test-requirements.in

0 commit comments

Comments
 (0)