Skip to content

Commit f898690

Browse files
committed
Allow "assignments" like self.foo: int
Although this is an assignment in the Python AST, there is no value being assigned.
1 parent cda5724 commit f898690

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

rewrite/rewrite/python/_parser_visitor.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def visit_arguments(self, node, with_close_paren: bool = True) -> List[JRightPad
102102
if not node.vararg:
103103
empty_name = j.VariableDeclarations.NamedVariable(random_id(), Space.EMPTY, Markers.EMPTY,
104104
cast(j.Identifier, self.__convert_name('', None)), [],
105-
None, None, None)
105+
None, None)
106106
kwonly_prefix = self.__source_before('*')
107107
mapped.append(
108108
JRightPadded(
@@ -308,6 +308,24 @@ def visit_AnnAssign(self, node):
308308
) if node.value else None,
309309
self.__map_type(node)
310310
)
311+
elif not node.value:
312+
return py.ExpressionStatement(
313+
random_id(),
314+
py.TypeHintedExpression(
315+
random_id(),
316+
prefix,
317+
Markers.EMPTY,
318+
self.__convert(node.target),
319+
py.TypeHint(
320+
random_id(),
321+
self.__source_before(':'),
322+
Markers.EMPTY,
323+
self.__convert_type(node.annotation),
324+
self.__map_type(node.annotation)
325+
),
326+
self.__map_type(node)
327+
)
328+
)
311329
else:
312330
name = cast(j.Identifier, self.__convert(node.target))
313331
if node.annotation:

rewrite/tests/python/all/class_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ class Foo:
2525
)
2626

2727

28+
def test_typed_field():
29+
# language=python
30+
rewrite_run(
31+
python(
32+
"""\
33+
class Foo:
34+
def __init__(self):
35+
self.target: int
36+
"""
37+
)
38+
)
39+
40+
2841
def test_enum():
2942
# language=python
3043
rewrite_run(

0 commit comments

Comments
 (0)