Skip to content

Fix type analysis for typing.Unpack #15228

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

Merged
merged 3 commits into from
May 17, 2023
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: 11 additions & 1 deletion mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None

m = re.search("# flags: (.*)$", "\n".join(testcase.input), re.MULTILINE)
if m:
mypy_cmdline.extend(m.group(1).split())
additional_flags = m.group(1).split()
for flag in additional_flags:
if flag.startswith("--python-version="):
targetted_python_version = flag.split("=")[1]
targetted_major, targetted_minor = targetted_python_version.split(".")
if (int(targetted_major), int(targetted_minor)) > (
sys.version_info.major,
sys.version_info.minor,
):
return
mypy_cmdline.extend(additional_flags)

# Write the program to a file.
program = "_" + testcase.name + ".py"
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ def analyze_callable_args(
# Potentially a unpack.
sym = self.lookup_qualified(arg.name, arg)
if sym is not None:
if sym.fullname == "typing_extensions.Unpack":
if sym.fullname in ("typing_extensions.Unpack", "typing.Unpack"):
if found_unpack:
self.fail("Callables can only have a single unpack", arg)
found_unpack = True
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1986,3 +1986,28 @@ def good9(foo1: Foo[Concatenate[int, P]], foo2: Foo[[int, str, bytes]], *args: P

[out]
_testStrictEqualitywithParamSpec.py:11: error: Non-overlapping equality check (left operand type: "Foo[[int]]", right operand type: "Bar[[int]]")

[case testTypeVarTuple]
# flags: --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack --python-version=3.11
from typing import Any, Callable, Unpack, TypeVarTuple
Copy link
Member

Choose a reason for hiding this comment

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

CI fails if this is run before 3.11 (because Unpack doesn't exist). Not sure what the incantation is to make this test run only on 3.11+.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that adding if sys.version_info >= (3, 11) around the entire test case should do the trick.


Ts = TypeVarTuple("Ts")

def foo(callback: Callable[[], Any]) -> None:
call(callback)

def call(callback: Callable[[Unpack[Ts]], Any], *args: Unpack[Ts]) -> Any:
...

[case testTypeVarTupleTypingExtensions]
# flags: --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack
from typing_extensions import Unpack, TypeVarTuple
from typing import Any, Callable

Ts = TypeVarTuple("Ts")

def foo(callback: Callable[[], Any]) -> None:
call(callback)

def call(callback: Callable[[Unpack[Ts]], Any], *args: Unpack[Ts]) -> Any:
...