Skip to content

Commit

Permalink
Fix crash caused by incorrect assumption about the type of an ast node.
Browse files Browse the repository at this point in the history
Fixes #1556.

PiperOrigin-RevId: 595503542
  • Loading branch information
rchen152 committed Jan 4, 2024
1 parent e7fcd3c commit 7b30397
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pytype/pyi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ class _MetadataVisitor(visitor.BaseVisitor):
def visit_Call(self, node):
posargs = tuple(evaluator.literal_eval(x) for x in node.args)
kwargs = {x.arg: evaluator.literal_eval(x.value) for x in node.keywords}
return (node.func.id, posargs, kwargs)
if isinstance(node.func, astlib.Attribute):
func_name = _attribute_to_name(node.func)
else:
func_name = node.func
return (func_name.id, posargs, kwargs)

def visit_Dict(self, node):
return evaluator.literal_eval(node)
Expand Down
13 changes: 13 additions & 0 deletions pytype/pyi/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2885,6 +2885,19 @@ class Foo:
x: Annotated[int, Signal]
""")

def test_attribute_access_and_call(self):
self.check("""
from typing import Annotated, Any
a: Any
def f() -> Annotated[list[int], a.b.C(3)]: ...
""", """
from typing import Annotated, Any, List
a: Any
def f() -> Annotated[List[int], {'tag': 'call', 'fn': 'a.b.C', 'posargs': (3,), 'kwargs': {}}]: ...
""")


class ErrorTest(test_base.UnitTest):
"""Test parser errors."""
Expand Down

0 comments on commit 7b30397

Please sign in to comment.