Skip to content

Fix crashes in astdiff when sorting union items #7842

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

Merged
merged 1 commit into from
Nov 1, 2019
Merged
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
18 changes: 14 additions & 4 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ def snapshot_simple_type(typ: Type) -> SnapshotItem:
return (type(typ).__name__,)


def encode_optional_str(s: Optional[str]) -> str:
if s is None:
return '<None>'
else:
return s


class SnapshotTypeVisitor(TypeVisitor[SnapshotItem]):
"""Creates a read-only, self-contained snapshot of a type object.

Expand All @@ -256,6 +263,9 @@ class SnapshotTypeVisitor(TypeVisitor[SnapshotItem]):
- Has no references to mutable or non-primitive objects.
- Two snapshots represent the same object if and only if they are
equal.
- Results must be sortable. It's important that tuples have
consistent types and can't arbitrarily mix str and None values,
for example, since they can't be compared.
"""

def visit_unbound_type(self, typ: UnboundType) -> SnapshotItem:
Expand All @@ -282,9 +292,9 @@ def visit_deleted_type(self, typ: DeletedType) -> SnapshotItem:

def visit_instance(self, typ: Instance) -> SnapshotItem:
return ('Instance',
typ.type.fullname(),
encode_optional_str(typ.type.fullname()),
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this mean there is a bug elsewhere? Maybe we shouldn't mask it (especially if there is no repro)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that this is not a great place to detect the bug, since the traceback won't tell much.

snapshot_types(typ.args),
None if typ.last_known_value is None else snapshot_type(typ.last_known_value))
('None',) if typ.last_known_value is None else snapshot_type(typ.last_known_value))

def visit_type_var(self, typ: TypeVarType) -> SnapshotItem:
return ('TypeVar',
Expand All @@ -301,7 +311,7 @@ def visit_callable_type(self, typ: CallableType) -> SnapshotItem:
return ('CallableType',
snapshot_types(typ.arg_types),
snapshot_type(typ.ret_type),
tuple(typ.arg_names),
tuple([encode_optional_str(name) for name in typ.arg_names]),
tuple(typ.arg_kinds),
typ.is_type_obj(),
typ.is_ellipsis_args)
Expand All @@ -316,7 +326,7 @@ def visit_typeddict_type(self, typ: TypedDictType) -> SnapshotItem:
return ('TypedDictType', items, required)

def visit_literal_type(self, typ: LiteralType) -> SnapshotItem:
return ('LiteralType', typ.value, snapshot_type(typ.fallback))
return ('LiteralType', snapshot_type(typ.fallback), typ.value)

def visit_union_type(self, typ: UnionType) -> SnapshotItem:
# Sort and remove duplicates so that we can use equality to test for
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/diff.test
Original file line number Diff line number Diff line change
Expand Up @@ -1443,3 +1443,25 @@ class C:
[out]
__main__.C.method
__main__.func

[case testUnionOfLiterals]
from typing_extensions import Literal
x: Literal[1, '2']
[file next.py]
from typing_extensions import Literal
x: Literal[1, 2]
[out]
__main__.x

[case testUnionOfCallables]
from typing import Callable, Union
from mypy_extensions import Arg
x: Union[Callable[[Arg(int, 'x')], None],
Callable[[int], None]]
[file next.py]
from typing import Callable, Union
from mypy_extensions import Arg
x: Union[Callable[[Arg(int, 'y')], None],
Callable[[int], None]]
[out]
__main__.x