Make get_call_names more resilient#148
Merged
Merged
Conversation
Code where we call a method of something which isn't a variable name
e.g.
```
yesterday = (date.today() - timedelta(days=1)).strftime("%Y-%m-%d")
```
was causing pyt to crash.
Previously the else branch would only handle ast.Attribute, and crash on
everything else.
```
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/pyt/pyt/__main__.py", line 141, in <module>
main()
File "/pyt/pyt/__main__.py", line 97, in main
path
File "/pyt/pyt/cfg/make_cfg.py", line 40, in make_cfg
module_definitions
File "/pyt/pyt/cfg/expr_visitor.py", line 60, in __init__
self.init_cfg(node)
File "/pyt/pyt/cfg/expr_visitor.py", line 67, in init_cfg
module_statements = self.visit(node)
File "/usr/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/pyt/pyt/cfg/stmt_visitor.py", line 58, in visit_Module
return self.stmt_star_handler(node.body)
File "/pyt/pyt/cfg/stmt_visitor.py", line 79, in stmt_star_handler
node = self.visit(stmt)
File "/usr/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/pyt/pyt/cfg/stmt_visitor.py", line 413, in visit_Assign
return self.assignment_call_node(label.result, node)
File "/pyt/pyt/cfg/stmt_visitor.py", line 437, in assignment_call_node
call = self.visit(ast_node.value)
File "/usr/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/pyt/pyt/cfg/expr_visitor.py", line 540, in visit_Call
_id = get_call_names_as_string(node.func)
File "/pyt/pyt/core/ast_helper.py", line 78, in get_call_names_as_string
return _list_to_dotted_string(get_call_names(node))
File "/pyt/pyt/core/ast_helper.py", line 68, in get_call_names
return reversed(_get_call_names_helper(node, result))
File "/pyt/pyt/core/ast_helper.py", line 62, in _get_call_names_helper
return _get_call_names_helper(node.value, result)
File "/pyt/pyt/core/ast_helper.py", line 61, in _get_call_names_helper
result.append(node.attr)
AttributeError: 'BinOp' object has no attribute 'attr'
```
Collaborator
|
Nice, this is wonderful! |
KevinHock
approved these changes
Jul 20, 2018
|
|
||
| def get_call_names(node): | ||
| """Get a list of call names.""" | ||
| result = list() |
Collaborator
There was a problem hiding this comment.
So much more pythonic to use yield and list like you did 💯 🥇
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code where we call a method of something which isn't a variable name
e.g.
was causing pyt to crash.
Previously the else branch would only handle ast.Attribute, and crash on
everything else.