Skip to content

Support AnnAssign in StmtVisitor #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2018
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
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exclude_paths:
- "pyt/intraprocedural_cfg.py"
- "pyt/repo_runner.py"
- "pyt/save.py"
- "example/**"
- "examples/**"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ Thanks 😄

- "profiling/**"
- "tests/**"
- "LICENSE"
Expand Down
2 changes: 2 additions & 0 deletions examples/example_inputs/assignment_with_annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x: int
y: int=5
8 changes: 8 additions & 0 deletions pyt/cfg/stmt_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ def visit_Assign(self, node):
path=self.filenames[-1]
))

def visit_AnnAssign(self, node):
if node.value is None:
return IgnoredNode()
else:
assign = ast.Assign(targets=[node.target], value=node.value)
ast.copy_location(assign, node)
return self.visit(assign)

def assignment_call_node(self, left_hand_label, ast_node):
"""Handle assignments that contain a function call on its right side."""
self.undecided = True # Used for handling functions in assignments
Expand Down
19 changes: 19 additions & 0 deletions tests/cfg/cfg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,25 @@ def test_assignment_and_builtin_line_numbers(self):
self.assertLineNumber(assign, 1)
self.assertLineNumber(builtin, 2)

def test_assignment_with_annotation(self):
self.cfg_create_from_file('examples/example_inputs/assignment_with_annotation.py')

self.assert_length(self.cfg.nodes, expected_length=3)

entry = 0
assign = 1
exit_node = 2

self.assertInCfg([(assign, entry), (exit_node, assign)])
self.assertEqual(self.cfg.nodes[assign].label, 'y = 5')

def test_assignment_with_annotation_line_numbers(self):
self.cfg_create_from_file('examples/example_inputs/assignment_with_annotation.py')

assign = self.cfg.nodes[1]

self.assertLineNumber(assign, 2)

def test_multiple_assignment(self):
self.cfg_create_from_file('examples/example_inputs/assignment_multiple_assign.py')

Expand Down