Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2c4615

Browse files
authored
Merge pull request #143 from bcaller/bc-annassign
Support AnnAssign in StmtVisitor
2 parents f78599f + 5fec81b commit a2c4615

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

.codeclimate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ exclude_paths:
2323
- "pyt/intraprocedural_cfg.py"
2424
- "pyt/repo_runner.py"
2525
- "pyt/save.py"
26-
- "example/**"
26+
- "examples/**"
2727
- "profiling/**"
2828
- "tests/**"
2929
- "LICENSE"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x: int
2+
y: int=5

pyt/cfg/stmt_visitor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ def visit_Assign(self, node):
422422
path=self.filenames[-1]
423423
))
424424

425+
def visit_AnnAssign(self, node):
426+
if node.value is None:
427+
return IgnoredNode()
428+
else:
429+
assign = ast.Assign(targets=[node.target], value=node.value)
430+
ast.copy_location(assign, node)
431+
return self.visit(assign)
432+
425433
def assignment_call_node(self, left_hand_label, ast_node):
426434
"""Handle assignments that contain a function call on its right side."""
427435
self.undecided = True # Used for handling functions in assignments

tests/cfg/cfg_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,25 @@ def test_assignment_and_builtin_line_numbers(self):
720720
self.assertLineNumber(assign, 1)
721721
self.assertLineNumber(builtin, 2)
722722

723+
def test_assignment_with_annotation(self):
724+
self.cfg_create_from_file('examples/example_inputs/assignment_with_annotation.py')
725+
726+
self.assert_length(self.cfg.nodes, expected_length=3)
727+
728+
entry = 0
729+
assign = 1
730+
exit_node = 2
731+
732+
self.assertInCfg([(assign, entry), (exit_node, assign)])
733+
self.assertEqual(self.cfg.nodes[assign].label, 'y = 5')
734+
735+
def test_assignment_with_annotation_line_numbers(self):
736+
self.cfg_create_from_file('examples/example_inputs/assignment_with_annotation.py')
737+
738+
assign = self.cfg.nodes[1]
739+
740+
self.assertLineNumber(assign, 2)
741+
723742
def test_multiple_assignment(self):
724743
self.cfg_create_from_file('examples/example_inputs/assignment_multiple_assign.py')
725744

0 commit comments

Comments
 (0)