Commit 8fba6ed
authored
Refactor how we analyse classes (#460)
Currently we analyse `ClassDef` nodes in many different places across
the tree in a somewhat scattershot and ad-hoc way. This means that
there's a fair amount of duplication currently, and that it can be
somewhat awkward to query information about the enclosing class
definition when we're visiting a method.
This PR thoroughly refactors the way we analyse classes:
- It's now all done in one place, the `EnclosingClassContext` class. As
well as reducing duplication, this also reduces potential for bugs: e.g.
we currently don't detect a protocol as being unused if the protocol is
generic (because that's an `ast.Subscript` node in the class's bases
tuple, rather than an `ast.Name` or `ast.Attribute` node).
- Get rid of the `_get_collections_abc_obj_id` function. It's not really
necessary; the name is really unclear; it's an over-abstraction that
obfuscates rather than clarifies.
- Get rid of the `in_class` nesting counter on the `PyiVisitor` class.
This works well to keep track of other contexts when visiting subnodes,
but doesn't work well for class definitions: it's no more code to just
check whether `self.enclosing_class_ctx is None` or not.1 parent 4711624 commit 8fba6ed
3 files changed
Lines changed: 168 additions & 130 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
0 commit comments