Skip to content

Commit

Permalink
Handle fussy typing._type_check (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki authored Dec 16, 2023
1 parent 5bed613 commit 1b05f64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion eval_type_backport/eval_type_backport.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ def visit_BinOp(self, node) -> ast.BinOp | ast.Subscript:

def visit_Subscript(self, node):
value_node = self.visit(node.value)
value_val = self.eval_type(value_node)
try:
value_val = self.eval_type(value_node)
except TypeError:
# Likely typing._type_check complaining that the result isn't a type,
# e.g. that it's a plain `Literal`.
# Either way, this probably isn't one of the new generic types that needs replacing.
return self.generic_visit(node)
if value_val not in new_generic_types:
return self.generic_visit(node)
slice_node = self.visit(node.slice)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_eval_type_backport.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def test_eval_type_backport():
]
],
)
if hasattr(t, 'Literal'):
check_eval('t.Literal[1]', t.Literal[1])
check_eval('t.Literal[1] | t.Literal[2]', t.Union[t.Literal[1], t.Literal[2]])
check_eval(
't.List[t.Literal[1] | t.Literal[2]]',
t.List[t.Union[t.Literal[1], t.Literal[2]]],
)


def test_other_type_error():
Expand Down

0 comments on commit 1b05f64

Please sign in to comment.