microsoft / microsoft/pyright

Expand `reportUninitializedInstanceVariable` to protocol variable implementation detection

Open
#10,770 0 comments 0 reactions 0 assignees View on GitHub
enhancement request
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

```python
class MyProtocol(Protocol):
a: int
def f(self) -> None:
self.a = 0

@final
class MyClass(MyProtocol): # want an error here
pass

class MyClass2:
def f(self) -> None:
self.a = 0 # error: reportUninitializedInstanceVariable

x: MyProtocol = MyClass2() # want an error here
```
Currently, `reportUninitializedInstanceVariable` setting is not applied to protocol variable implemetation detection for both implicit and explicit protocol implementation.

For implicit implementation, `reportUninitializedInstanceVariable` error appears in the method but this is not an error indicating the class does not implement the protocol.

It's really helpful if `reportUninitializedInstanceVariable` prevents protocol variable implementation outside of `__init__`.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the implicit and explicit protocol examples with reportUninitializedInstanceVariable enabled, then trace the protocol variable implementation detection entry point and its handling of that setting. Done means both examples produce the intended protocol implementation error rather than only the method-local diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.