Skip to content
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

Fix Unpack imported from typing #14378

Merged
merged 2 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if sym and sym.fullname in {"typing.Unpack", "typing_extensions.Unpack"}:
if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):

Nit, but this is what most other similar checks look like and I suspect mypyc handles it better. It's likely also faster in non-compiled Python to use a tuple here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure about mypyc, but for non-compiled Python it's actually a toss-up for two items. However, starting with three, sets are usually better.

Copy link
Member

Choose a reason for hiding this comment

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

I ran some local benchmarks and set is faster even for two-element tuples when the element is the second one. Still, better to be consistent with the other mypy code.

inner_t = unbound.args[0]
if not isinstance(inner_t, UnboundType):
return None
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-python311.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ NoReturn = 0
Never = 0
NewType = 0
ParamSpec = 0
TypeVarTuple = 0
Unpack = 0
Self = 0
TYPE_CHECKING = 0

Expand Down