8000 Keep `TypeVar` arguments when narrowing generic subclasses with `isinstance` and `issubclass`. by tyralla · Pull Request #17099 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Keep TypeVar arguments when narrowing generic subclasses with isinstance and issubclass. #17099

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 21 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
84e3837
Keep `TypeVar` arguments when narrowing generic subclasses with `isin…
tyralla Apr 4, 2024
dbcfa1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 5, 2024
098e066
Remove a cast that is now redundant.
tyralla Apr 5, 2024
892c74f
fix: Remove a cast that is now redundant.
tyralla Apr 5, 2024
c24cc53
Use Unpack in testKeepTypeVarTupleArgsWhenNarrowingGenericsWithIsInst…
tyralla Apr 5, 2024
d013673
fix "local variable 'pos2' referenced before assignment"
tyralla Apr 5, 2024
fe51e85
Merge branch 'master' into feature/keep_type_var_args_when_narrowing_…
tyralla Apr 5, 2024
a94db4a
restart job
tyralla Apr 5, 2024
a700294
Revert "restart job"
tyralla Apr 5, 2024
73da8b6
remove empty line
tyralla Apr 5, 2024
325f190
avoid unnecessary union members when proposed type is current type (w…
tyralla Apr 5, 2024
bb63532
fix: avoid unnecessary union members when proposed type is current ty…
tyralla Apr 5, 2024
18bae83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 5, 2024
afa5450
fix: avoid unnecessary union members when proposed type is current ty…
tyralla Apr 5, 2024
9769638
apply one more union flattening to avoid (good or bad?) changes in na…
tyralla Apr 6, 2024
6741175
fix: apply one more union flattening to avoid (good or bad?) changes …
tyralla Apr 6, 2024
463c7d4
Change test name testKeepTypeVarArgsWhenNarrowingGenericsWithIsInstan…
tyralla Apr 16, 2024
6195de1
special handling of TupleType
tyralla Apr 16, 2024
116b70e
restart Mypy primer
tyralla Apr 16, 2024
ac4b9dc
Revert "restart Mypy primer"
tyralla Apr 16, 2024
557bfe2
Merge branch 'master' into feature/keep_type_var_args_when_narrowing_…
hauntsaninja Nov 3, 2024
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
fix: avoid unnecessary union members when proposed type is current ty…
…pe (without crashing pattern matching)
  • Loading branch information
tyralla committed Apr 5, 2024
commit afa54504d9cbadd36a6f0345f6a4dd8d4ebebe68
33 changes: 17 additions & 16 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7569,8 +7569,8 @@ def _get_instance_path_from_current_to_proposed(
for pos2, typevar2 in enumerate(instance.type.defn.type_vars):
if typevar1 == typevar2:
if instance.type.has_type_var_tuple_type:
assert (prefix := instance.type.type_var_tuple_prefix) is not None
if pos2 > prefix:
assert (pre := instance.type.type_var_tuple_prefix) is not None
if pos2 > pre:
pos2 += len(instance.args) - len(instance.type.defn.type_vars)
typevar1 = instance.args[pos2]
if isinstance(typevar1, UnpackType):
Expand All @@ -7581,21 +7581,22 @@ def _get_instance_path_from_current_to_proposed(
break

# Transfer the current type's type variable argument or type variable tuple arguments:
if (pos2 is not None) and isinstance(
get_proper_type(proposed_args[pos2]), (AnyType, UnpackType)
):
if current.type.has_type_var_tuple_type:
assert (prefix := current.type.type_var_tuple_prefix) is not None
assert (suffix := current.type.type_var_tuple_suffix) is not None
if pos1 < prefix:
proposed_args[pos2] = current.args[pos1]
elif pos1 == prefix:
proposed_args[pos2] = current.args[prefix : len(current.args) - suffix]
if pos2 is not None:
proposed_arg = proposed_args[pos2]
assert not isinstance(proposed_arg, tuple)
if isinstance(get_proper_type(proposed_arg), (AnyType, UnpackType)):
if current.type.has_type_var_tuple_type:
assert (pre := current.type.type_var_tuple_prefix) is not None
assert (suf := current.type.type_var_tuple_suffix) is not None
if pos1 < pre:
proposed_args[pos2] = current.args[pos1]
elif pos1 == pre:
proposed_args[pos2] = current.args[pre : len(current.args) - suf]
else:
middle = len(current.args) - pre - suf
proposed_args[pos2] = current.args[pos1 + middle - 1]
else:
middle = len(current.args) - prefix - suffix
proposed_args[pos2] = current.args[pos1 + middle - 1]
else:
proposed_args[pos2] = current.args[pos1]
proposed_args[pos2] = current.args[pos1]

# Combine all type variable and type variable tuple arguments to a flat list:
flattened_proposed_args: list[Type] = []
Expand Down
0