8000 Narrow `is` with final types correctly by sobolevn · Pull Request #15646 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Narrow is with final types correctly #15646

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Prev Previous commit
Next Next commit
Merge branch 'master' into issue-15553
  • Loading branch information
hauntsaninja authored Oct 17, 2024
commit 2fcace1d552c40ba1e2672de8bcdae8e7dc3a2a9
66 changes: 66 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,72 @@ def with_unpacked_kwargs(self) -> NormalizedCallableType:
def is_singleton_type(self) -> bool:
return self.is_type_obj() and self.type_object().is_final

def with_normalized_var_args(self) -> Self:
var_arg = self.var_arg()
if not var_arg or not isinstance(var_arg.typ, UnpackType):
return self
unpacked = get_proper_type(var_arg.typ.type)
if not isinstance(unpacked, TupleType):
# Note that we don't normalize *args: *tuple[X, ...] -> *args: X,
# this should be done once in semanal_typeargs.py for user-defined types,
# and we ourselves rarely construct such type.
return self
unpack_index = find_unpack_in_list(unpacked.items)
if unpack_index == 0 and len(unpacked.items) > 1:
# Already normalized.
return self

# Boilerplate:
var_arg_index = self.arg_kinds.index(ARG_STAR)
types_prefix = self.arg_types[:var_arg_index]
kinds_prefix = self.arg_kinds[:var_arg_index]
names_prefix = self.arg_names[:var_arg_index]
types_suffix = self.arg_types[var_arg_index + 1 :]
kinds_suffix = self.arg_kinds[var_arg_index + 1 :]
names_suffix = self.arg_names[var_arg_index + 1 :]
no_name: str | None = None # to silence mypy

# Now we have something non-trivial to do.
if unpack_index is None:
# Plain *Tuple[X, Y, Z] -> replace with ARG_POS completely
types_middle = unpacked.items
kinds_middle = [ARG_POS] * len(unpacked.items)
names_middle = [no_name] * len(unpacked.items)
else:
# *Tuple[X, *Ts, Y, Z] or *Tuple[X, *tuple[T, ...], X, Z], here
# we replace the prefix by ARG_POS (this is how some places expect
# Callables to be represented)
nested_unpack = unpacked.items[unpack_index]
assert isinstance(nested_unpack, UnpackType)
nested_unpacked = get_proper_type(nested_unpack.type)
if unpack_index == len(unpacked.items) - 1:
# Normalize also single item tuples like
# *args: *Tuple[*tuple[X, ...]] -> *args: X
# *args: *Tuple[*Ts] -> *args: *Ts
# This may be not strictly necessary, but these are very verbose.
if isinstance(nested_unpacked, Instance):
assert nested_unpacked.type.fullname == "builtins.tuple"
new_unpack = nested_unpacked.args[0]
else:
if not isinstance(nested_unpacked, TypeVarTupleType):
# We found a non-nomralized tuple type, this means this method
# is called during semantic analysis (e.g. from get_proper_type())
# there is no point in normalizing callables at this stage.
return self
new_unpack = nested_unpack
else:
new_unpack = UnpackType(
unpacked.copy_modified(items=unpacked.items[unpack_index:])
)
types_middle = unpacked.items[:unpack_index] + [new_unpack]
kinds_middle = [ARG_POS] * unpack_index + [ARG_STAR]
names_middle = [no_name] * unpack_index + [self.arg_names[var_arg_index]]
return self.copy_modified(
arg_types=types_prefix + types_middle + types_suffix,
arg_kinds=kinds_prefix + kinds_middle + kinds_suffix,
arg_names=names_prefix + names_middle + names_suffix,
)

def __hash__(self) -> int:
# self.is_type_obj() will fail if self.fallback.type is a FakeInfo
if isinstance(self.fallback.type, FakeInfo):
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0