Skip to content
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
10 changes: 10 additions & 0 deletions pytype/pyi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def _attribute_to_name(node: ast3.Attribute) -> ast3.Name:
class AnnotationVisitor(visitor.BaseVisitor):
"""Converts typed_ast annotations to pytd."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Exclude defaults because they may contain strings
# which should not be interpreted as annotations
self._node_children[self._ast.arguments] = [
field
for field in self._ast.arguments._fields
if field not in ("kw_defaults", "defaults")
]

def show(self, node):
print(debug.dump(node, ast3, include_attributes=False))

Expand Down
11 changes: 9 additions & 2 deletions pytype/pyi/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ class A: ...

def test_def(self):
self.check("""
def f(x: "int") -> "str": ...
def f(x: "int", *args: "float", y: "str", **kwargs: "bool") -> "str": ...
""", """
def f(x: int) -> str: ...
def f(x: int, *args: float, y: str, **kwargs: bool) -> str: ...
""")


Expand Down Expand Up @@ -846,6 +846,13 @@ def test_params(self):
self.check("def foo(x: int, y: str, z: bool,) -> int: ...",
"def foo(x: int, y: str, z: bool) -> int: ...")

def test_defaults(self):
self.check("""
def f(x: int = 3, y: str = " ") -> None: ...
""", """
def f(x: int = ..., y: str = ...) -> None: ...
""")

def test_star_params(self):
self.check("def foo(*, x) -> str: ...")
self.check("def foo(x: int, *args) -> str: ...")
Expand Down