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

Handle fussy typing._type_check #2

Merged
merged 2 commits into from
Dec 16, 2023
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
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