Skip to content

Support singleton pattern setting ClassName.attr in __init__ #2748

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

class Server:
    def __init__(self, name: str) -> None:
        Server.instance = self
        Server.server_name = name

s = Server("main")
current: "Server" = Server.instance  # FP: no class attribute `instance`
label: str = Server.server_name  # FP: no class attribute `server_name`

Only Pyright choose to support this though. Mypy and Ty both get confused as well. So this is low-pri.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQDKMATgG6uIA669f9mGGHoB9ERHQQALmIAUcGFDAAaeulQBbGInpwpLAJT0AtAD56AOVzptPfvaasOLQhL0ZKMegF5disHYOfMzsrIQKoSwi6lo%2BapowPDwMviHOslwgGqgSmQY8lACuLCww6FI6mWmsmXHVLm5SHl70AMT0AGIACjrouPTUtAyoUvoQ2IVSXgAGjc3TPDTYijp6LHVOYRHO0Ql87d29-YN09CNjE1P009usu1rTIMogk9BwJOSIIO0AqlLQ0lI9DAhXQlH%2B1jgSSwQmBuBY2Rk6EKGmWLFk%2BB0EikRjMun03F4-FKUmKvACIAsKLROmA%2BAAvpkeE8QGRSmAoKRCFJcBooBR2l1SOzOboMDgCANrJAAObFEYQayEHjtZheAAWo2IcEQAHpdWyhJzCPCZbqyrrMLhKHBdZRpRA5SwFdZdXD1qg2DklrApehZfKIbxcMQg%2B8eGQpOrrMZnHBFbxfJkAMyEACMACYmegQPTnqhwRAOB1oDAKGgsHgiGRc0A

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions