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

Change tuple[T, ...] compatibility rules #711

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- `tuple[T, ...]` is no longer considered compatible with
fixed-length tuple types (#711)
- More PEP 695 support: generic classes and functions. Scoping rules
are not yet fully implemented. (#703)
- Fix type inference when constructing user-defined generic classes
Expand Down
4 changes: 2 additions & 2 deletions pyanalyze/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def test_sequence_value() -> None:
val = value.SequenceValue(
tuple, [(False, TypedValue(int)), (False, TypedValue(str))]
)
assert_can_assign(val, TypedValue(tuple))
assert_can_assign(val, GenericValue(tuple, [TypedValue(int) | TypedValue(str)]))
assert_cannot_assign(val, TypedValue(tuple))
assert_cannot_assign(val, GenericValue(tuple, [TypedValue(int) | TypedValue(str)]))
assert_cannot_assign(val, GenericValue(tuple, [TypedValue(int) | TypedValue(list)]))

assert_can_assign(val, val)
Expand Down
8 changes: 7 additions & 1 deletion pyanalyze/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ def get_type_object(
return self._type_object

def can_assign(self, other: Value, ctx: CanAssignContext) -> CanAssign:
if isinstance(self, SequenceValue):
return super().can_assign(other, ctx)
self_tobj = self.get_type_object(ctx)
if self_tobj.is_thrift_enum:
# Special case: Thrift enums. These are conceptually like
Expand Down Expand Up @@ -904,7 +906,11 @@ def can_assign(self, other: Value, ctx: CanAssignContext) -> CanAssign:
other = replace_known_sequence_value(other)
if isinstance(other, KnownValue):
other = TypedValue(type(other.val))
if isinstance(other, TypedValue) and not isinstance(other.typ, super):
if (
isinstance(other, TypedValue)
and not isinstance(other.typ, super)
and not isinstance(self, SequenceValue)
):
generic_args = other.get_generic_args_for_type(self.typ, ctx)
# If we don't think it's a generic base, try super;
# runtime isinstance() may disagree.
Expand Down
Loading