Skip to content

Commit 905c2cb

Browse files
authored
Fix type analysis for typing.Unpack (#15228)
We were checking only for `typing_extensions.Unpack`, which breaks code when checking at 3.11+ even if importing `typing_extensions` because its just a re-export from `typing`. These basic pythoneval tests also assert that the feature at least somewhat works with the real stubs - otherwise up until now we had basically no coverage of that, and were relying on the fake typing stubs to be close enough to the real thing.
1 parent ce2c918 commit 905c2cb

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

mypy/test/testpythoneval.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
6161

6262
m = re.search("# flags: (.*)$", "\n".join(testcase.input), re.MULTILINE)
6363
if m:
64-
mypy_cmdline.extend(m.group(1).split())
64+
additional_flags = m.group(1).split()
65+
for flag in additional_flags:
66+
if flag.startswith("--python-version="):
67+
targetted_python_version = flag.split("=")[1]
68+
targetted_major, targetted_minor = targetted_python_version.split(".")
69+
if (int(targetted_major), int(targetted_minor)) > (
70+
sys.version_info.major,
71+
sys.version_info.minor,
72+
):
73+
return
74+
mypy_cmdline.extend(additional_flags)
6575

6676
# Write the program to a file.
6777
program = "_" + testcase.name + ".py"

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ def analyze_callable_args(
12981298
# Potentially a unpack.
12991299
sym = self.lookup_qualified(arg.name, arg)
13001300
if sym is not None:
1301-
if sym.fullname == "typing_extensions.Unpack":
1301+
if sym.fullname in ("typing_extensions.Unpack", "typing.Unpack"):
13021302
if found_unpack:
13031303
self.fail("Callables can only have a single unpack", arg)
13041304
found_unpack = True

test-data/unit/pythoneval.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,3 +1986,28 @@ def good9(foo1: Foo[Concatenate[int, P]], foo2: Foo[[int, str, bytes]], *args: P
19861986

19871987
[out]
19881988
_testStrictEqualitywithParamSpec.py:11: error: Non-overlapping equality check (left operand type: "Foo[[int]]", right operand type: "Bar[[int]]")
1989+
1990+
[case testTypeVarTuple]
1991+
# flags: --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack --python-version=3.11
1992+
from typing import Any, Callable, Unpack, TypeVarTuple
1993+
1994+
Ts = TypeVarTuple("Ts")
1995+
1996+
def foo(callback: Callable[[], Any]) -> None:
1997+
call(callback)
1998+
1999+
def call(callback: Callable[[Unpack[Ts]], Any], *args: Unpack[Ts]) -> Any:
2000+
...
2001+
2002+
[case testTypeVarTupleTypingExtensions]
2003+
# flags: --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack
2004+
from typing_extensions import Unpack, TypeVarTuple
2005+
from typing import Any, Callable
2006+
2007+
Ts = TypeVarTuple("Ts")
2008+
2009+
def foo(callback: Callable[[], Any]) -> None:
2010+
call(callback)
2011+
2012+
def call(callback: Callable[[Unpack[Ts]], Any], *args: Unpack[Ts]) -> Any:
2013+
...

0 commit comments

Comments
 (0)