PY-36889 Type check assignments to class/instance attributes outside of class bodies

Previously, PyTypingTypeProvider.getReferenceType returned a type from a class attribute
type hint only for assignment to instance attribute located inside a class definition.

In other words, here we inferred the expected type from the annotation
and reported incompatible types in assignment:

```python
class C:
    attr: int

    def m(self):
        self.attr = "foo"
```
but in the following we didn't:

```python
class C:
    attr: int

inst = C()
inst.attr = "foo"
```

Now we try to resolve any qualified target expression to a class
or instance attribute and then infer the type from the corresponding
annotation.


(cherry picked from commit 086dbb678a8cd89cfe332bf801631568fb6c3a4d)

IJ-MR-147382

GitOrigin-RevId: 4e3f71baa598d4caf684d0aeab23d1a9a688b94d
This commit is contained in:
Mikhail Golubev
2024-10-21 14:33:50 +03:00
committed by intellij-monorepo-bot
parent 24284fe20f
commit 99a6645e5d
3 changed files with 72 additions and 6 deletions

View File

@@ -18,4 +18,4 @@ class C:
self.class_var = 1
self.class_var = <warning descr="Expected type 'int', got 'str' instead">'bar'</warning>
C.class_var = 1
C.class_var = 'bar'
C.class_var = <warning descr="Expected type 'int', got 'str' instead">'bar'</warning>