microsoft / microsoft/pyright

`reportUninitializedInstanceVariable` false negatives/positives for the `__weakref__` slot

Open
#11,200 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

Describe the bug
When reportUninitializedInstanceVariable is true, pyright reports an error stating that __weakref__ is not initialized (false positive), even though it should never be initialized by the user. Interestingly, this only happens when the slot is defined via a sequence, but is ignored when defined via a string (false negative?).

Code or Screenshots

#!/usr/bin/env python3
# pyright: reportUninitializedInstanceVariable = true

import weakref


class EmptyViaString:
    __slots__ = "__weakref__"


class EmptyViaTuple:
    __slots__ = ("__weakref__",)


def main() -> None:
    weakref.ref(EmptyViaString())  # ok
    weakref.ref(EmptyViaTuple())  # ok


if __name__ == "__main__":
    main()

/home/user/workspace/example.py
  /home/user/workspace/example.py:12:18 - error: Instance variable "__weakref__" is not initialized in the class body or __init__ method (reportUninitializedInstanceVariable)
1 error, 0 warnings, 0 informations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Python reproducer and compare Pyright's handling of slots when it is a string versus a sequence. Trace the uninitialized-instance-variable analysis for weakref; done means both examples are accepted without false positives while the intended diagnostic remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.