Skip to content

reportUninitializedInstanceVariable false negatives/positives for the __weakref__ slot #11200

@x42005e1f

Description

@x42005e1f

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions