mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-12 00:44:40 +00:00
## Summary Remove a hack in control flow modeling that was treating `return` statements at the end of function bodies in a special way (basically considering the state *just before* the `return` statement as the end-of-scope state). This is not needed anymore now that #18750 has been merged. In order to make this work, we now use *all reachable bindings* for purposes of finding implicit instance attribute assignments as well as for deferred lookups of symbols. Both would otherwise be affected by this change: ```py def C: def f(self): self.x = 1 # a reachable binding that is not visible at the end of the scope return ``` ```py def f(): class X: ... # a reachable binding that is not visible at the end of the scope x: "X" = X() # deferred use of `X` return ``` Implicit instance attributes also required another change. We previously kept track of possibly-unbound instance attributes in some cases, but we now give up on that completely and always consider *implicit* instance attributes to be bound if we see a reachable binding in a reachable method. The previous behavior was somewhat inconsistent anyway because we also do not consider attributes possibly-unbound in other scenarios: we do not (and can not) keep track of whether or not methods are called that define these attributes. closes https://github.com/astral-sh/ty/issues/711 ## Ecosystem analysis I think this looks very positive! * We see an unsurprising drop in `possibly-unbound-attribute` diagnostics (599), mostly for classes that define attributes in `try … except` blocks, `for` loops, or `if … else: raise …` constructs. There might obviously also be true positives that got removed, but the vast majority should be false positives. * There is also a drop in `possibly-unresolved-reference` / `unresolved-reference` diagnostics (279+13) from the change to deferred lookups. * Some `invalid-type-form` false positives got resolved (13), because we can now properly look up the names in the annotations. * There are some new *true* positives in `attrs`, since we understand the `Attribute` annotation that was previously inferred as `Unknown` because of a re-assignment after the class definition. ## Test Plan The existing attributes.md test suite has sufficient coverage here. |
||
|---|---|---|
| .. | ||
| annotations | ||
| assignment | ||
| binary | ||
| boolean | ||
| boundness_declaredness | ||
| call | ||
| class | ||
| comparison | ||
| comprehensions | ||
| conditional | ||
| declaration | ||
| diagnostics | ||
| directives | ||
| doc | ||
| exception | ||
| expression | ||
| function | ||
| generics | ||
| ide_support | ||
| import | ||
| literal | ||
| loops | ||
| narrow | ||
| regression | ||
| scopes | ||
| shadowing | ||
| snapshots | ||
| stubs | ||
| subscript | ||
| suppressions | ||
| type_compendium | ||
| type_of | ||
| type_properties | ||
| type_qualifiers | ||
| unary | ||
| with | ||
| .mdformat.toml | ||
| attributes.md | ||
| cycle.md | ||
| dataclass_transform.md | ||
| dataclasses.md | ||
| decorators.md | ||
| del.md | ||
| descriptor_protocol.md | ||
| final.md | ||
| instance_layout_conflict.md | ||
| intersection_types.md | ||
| invalid_syntax.md | ||
| known_constants.md | ||
| mdtest_config.md | ||
| mdtest_custom_typeshed.md | ||
| metaclass.md | ||
| mro.md | ||
| named_tuple.md | ||
| overloads.md | ||
| pep695_type_aliases.md | ||
| properties.md | ||
| protocols.md | ||
| public_types.md | ||
| statically_known_branches.md | ||
| sys_platform.md | ||
| sys_version_info.md | ||
| terminal_statements.md | ||
| type_api.md | ||
| typed_dict.md | ||
| union_types.md | ||
| unpacking.md | ||
| unreachable.md | ||