From 5f480f37ff3704ed581db944bfdf99c655b3dabe Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 2 Jan 2023 17:48:48 +0100 Subject: [PATCH] Fix Unpack imported from typing (#14378) Add missing check for `typing.Unpack` to fix running with `--python 3.11`. Co-authored-by: Jelle Zijlstra --- mypy/semanal.py | 2 +- test-data/unit/check-python311.test | 12 ++++++++++++ test-data/unit/lib-stub/typing.pyi | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 51310e4f3e4d..13350b58af87 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1881,7 +1881,7 @@ def analyze_unbound_tvar(self, t: Type) -> tuple[str, TypeVarLikeExpr] | None: # It's bound by our type variable scope return None return unbound.name, sym.node - if sym and sym.fullname == "typing_extensions.Unpack": + if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"): inner_t = unbound.args[0] if not isinstance(inner_t, UnboundType): return None diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index 9bf62b0c489d..7196f10f8863 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -51,3 +51,15 @@ try: except* (RuntimeError, ExceptionGroup) as e: # E: Exception type in except* cannot derive from BaseExceptionGroup reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, Any]]" [builtins fixtures/exception.pyi] + +[case testBasicTypeVarTupleGeneric] +from typing import Generic, TypeVarTuple, Unpack + +Ts = TypeVarTuple("Ts") + +class Variadic(Generic[Unpack[Ts]]): + ... + +variadic: Variadic[int, str] +reveal_type(variadic) # N: Revealed type is "__main__.Variadic[builtins.int, builtins.str]" +[builtins fixtures/tuple.pyi] diff --git a/test-data/unit/lib-stub/typing.pyi b/test-data/unit/lib-stub/typing.pyi index f3850d3936b4..a306b70f74d7 100644 --- a/test-data/unit/lib-stub/typing.pyi +++ b/test-data/unit/lib-stub/typing.pyi @@ -27,6 +27,8 @@ NoReturn = 0 Never = 0 NewType = 0 ParamSpec = 0 +TypeVarTuple = 0 +Unpack = 0 Self = 0 TYPE_CHECKING = 0