Skip to content

Commit

Permalink
Allow "assignments" like self.foo: int
Browse files Browse the repository at this point in the history
Although this is an assignment in the Python AST, there is no value being assigned.
  • Loading branch information
knutwannheden committed Nov 14, 2024
1 parent cda5724 commit f898690
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def visit_arguments(self, node, with_close_paren: bool = True) -> List[JRightPad
if not node.vararg:
empty_name = j.VariableDeclarations.NamedVariable(random_id(), Space.EMPTY, Markers.EMPTY,
cast(j.Identifier, self.__convert_name('', None)), [],
None, None, None)
None, None)
kwonly_prefix = self.__source_before('*')
mapped.append(
JRightPadded(
Expand Down Expand Up @@ -308,6 +308,24 @@ def visit_AnnAssign(self, node):
) if node.value else None,
self.__map_type(node)
)
elif not node.value:
return py.ExpressionStatement(
random_id(),
py.TypeHintedExpression(
random_id(),
prefix,
Markers.EMPTY,
self.__convert(node.target),
py.TypeHint(
random_id(),
self.__source_before(':'),
Markers.EMPTY,
self.__convert_type(node.annotation),
self.__map_type(node.annotation)
),
self.__map_type(node)
)
)
else:
name = cast(j.Identifier, self.__convert(node.target))
if node.annotation:
Expand Down
13 changes: 13 additions & 0 deletions rewrite/tests/python/all/class_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ class Foo:
)


def test_typed_field():
# language=python
rewrite_run(
python(
"""\
class Foo:
def __init__(self):
self.target: int
"""
)
)


def test_enum():
# language=python
rewrite_run(
Expand Down

0 comments on commit f898690

Please sign in to comment.