diff options
author | Nick Drozd <nicholasdrozd@gmail.com> | 2018-02-09 18:34:08 -0600 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-03-02 10:10:13 +0100 |
commit | 9b5aa979effadfe55be602f7e157368316a373be (patch) | |
tree | cb5f169a47ce0e9dc17489fcc13908d8c65f5de2 /astroid/inference.py | |
parent | f44fced2b3c8eedd21ec30e3e0b4bb4287661c41 (diff) | |
download | astroid-git-9b5aa979effadfe55be602f7e157368316a373be.tar.gz |
Add type-specific nodes_of_class
nodes_of_class is a very flexible method, which is great for use in
client code (e.g. Pylint). However, that flexibility requires a great
deal of runtime type checking:
def nodes_of_class(self, klass, skip_klass=None):
if isinstance(self, klass):
yield self
if skip_klass is None:
for child_node in self.get_children():
for matching in child_node.nodes_of_class(klass, skip_klass):
yield matching
return
for child_node in self.get_children():
if isinstance(child_node, skip_klass):
continue
for matching in child_node.nodes_of_class(klass, skip_klass):
yield matching
First, the node has to check its own type to see whether it's of the
desired class. Then the skip_klass flag has to be checked to see
whether anything needs to be skipped. If so, the type of every yielded
node has to be check to see if it should be skipped.
This is fine for calling code whose arguments can't be known in
advance ("Give me all the Assign and ClassDef nodes, but skip all the
BinOps, YieldFroms, and Globals."), but in Astroid itself, every call
to this function can be known in advance. There's no need to do any
type checking if all the nodes know how to respond to certain
requests. Take get_assign_nodes for example. The Assign nodes know
that they should yield themselves and then yield their Assign
children. Other nodes know in advance that they aren't Assign nodes,
so they don't need to check their own type, just immediately yield
their Assign children.
Overly specific functions like get_yield_nodes_skip_lambdas certainly
aren't very elegant, but the tradeoff is to take advantage of knowing
how the library code works to improve speed.
Diffstat (limited to 'astroid/inference.py')
0 files changed, 0 insertions, 0 deletions