Skip to content

Report error when accessing generic instance variable from class object #7051

@erictraut

Description

@erictraut

The typing spec indicates that if an instance variable within a generic class is generic, an attempt to access it through the class object should generate an error because the type is erased at runtime for the class.

from typing import Generic, TypeVar

T = TypeVar("T")

class Node(Generic[T]):
    label: T

    def __init__(self, label: T | None = None) -> None:
        ...

Node[int].label = 1  # Should be type error
Node[int].label  # Should be type error
Node.label  # Should be type error

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions