ruff
fd2cc37f - [ty] Add decorator check for implicit attribute assignments (#18587)

Commit
83 days ago
[ty] Add decorator check for implicit attribute assignments (#18587) ## Summary Previously, the checks for implicit attribute assignments didn't properly account for method decorators. This PR fixes that by: - Adding a decorator check in `implicit_instance_attribute`. This allows it to filter out methods with mismatching decorators when analyzing attribute assignments. - Adding attribute search for implicit class attributes: if an attribute can't be found directly in the class body, the `ClassLiteral::own_class_member` function will now search in classmethods. - Adding `staticmethod`: it has been added into `KnownClass` and together with the new decorator check, it will no longer expose attributes when the assignment target name is the same as the first method name. If accepted, it should fix https://github.com/astral-sh/ty/issues/205 and https://github.com/astral-sh/ty/issues/207. ## Test Plan This is tested with existing mdtest suites and is able to get most of the TODO marks for implicit assignments in classmethods and staticmethods removed. However, there's one specific test case I failed to figure out how to correctly resolve: https://github.com/med1844/ruff/blob/b279508bdc63c1ed6fc4ccf9d43d3719fe7a202b/crates/ty_python_semantic/resources/mdtest/attributes.md?plain=1#L754-L755 I tried to add `instance_member().is_unbound()` check in this [else branch](https://github.com/med1844/ruff/blob/b279508bdc63c1ed6fc4ccf9d43d3719fe7a202b/crates/ty_python_semantic/src/types/infer.rs#L3299-L3301) but it causes tests with class attributes defined in class body to fail. While it's possible to implicitly add `ClassVar` to qualifiers to make this assignment fail and keep everything else passing, it doesn't feel like the right solution.
Author
Parents
Loading