From 7b3039762515cbd55980f3109f4a8e3bd2d1735b Mon Sep 17 00:00:00 2001 From: rechen Date: Wed, 3 Jan 2024 14:25:32 -0800 Subject: [PATCH] Fix crash caused by incorrect assumption about the type of an ast node. Fixes https://github.com/google/pytype/issues/1556. PiperOrigin-RevId: 595503542 --- pytype/pyi/parser.py | 6 +++++- pytype/pyi/parser_test.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pytype/pyi/parser.py b/pytype/pyi/parser.py index e6bab8bc9..92d4a64b4 100644 --- a/pytype/pyi/parser.py +++ b/pytype/pyi/parser.py @@ -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) diff --git a/pytype/pyi/parser_test.py b/pytype/pyi/parser_test.py index 3404a1949..10bc90a4c 100644 --- a/pytype/pyi/parser_test.py +++ b/pytype/pyi/parser_test.py @@ -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."""