Skip to content

Commit

Permalink
Fix: compare unresolved late annotations using their expressions.
Browse files Browse the repository at this point in the history
Previously, they were compared using their types, which for unresolved
annotations is always Unsolvable, so checks like `LateAnnotation(expr='Z',
type=None) in {LateAnnotation(expr='Y', type=None)}` would incorrectly succeed.
PiperOrigin-RevId: 320666829
  • Loading branch information
rchen152 committed Jul 14, 2020
1 parent 545ae5c commit ce1149c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pytype/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,10 +1184,10 @@ def __repr__(self):
# set/dict comparisons.

def __hash__(self):
return hash(self._type)
return hash(self._type) if self.resolved else hash(self.expr)

def __eq__(self, other):
return self._type == other
return hash(self) == hash(other)

def __getattribute__(self, name):
if name == "_attribute_names" or name in self._attribute_names:
Expand Down
3 changes: 2 additions & 1 deletion pytype/tests/py3/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,8 @@ def test_forward_reference_in_type_alias(self):
self.Check("""
from typing import List
X = List["Y"]
Y = List[int]
Y = List["Z"]
Z = List[int]
""")

def test_fully_quoted_annotation(self):
Expand Down

0 comments on commit ce1149c

Please sign in to comment.