ruff
c8cd59f1 - [ty] Infer class attributes assigned by metaclass initialization (#25342)

Commit
173 days ago
[ty] Infer class attributes assigned by metaclass initialization (#25342) ## Summary Prior to this change, we didn't recognize class-object attributes populated by a metaclass during class initialization. For example: ```python class Meta(type): def __init__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, object]) -> None: cls.attr: int = 1 class C(metaclass=Meta): ... reveal_type(C.attr) # revealed: int ``` We now recognize attributes that a metaclass adds to a class while creating it. If the metaclass overwrites an existing class-body value, we use the overwritten value's type. If the class provides an annotation for the generated attribute, we preserve that annotation as its public type. For example, if the class body initially gives attr a value, the metaclass assignment happens later at runtime and overwrites it: ```python class Meta(type): def __init__(cls, name, bases, namespace): cls.attr: int = 1 class C(metaclass=Meta): attr = "initial value" reveal_type(C.attr) # int ``` Closes https://github.com/astral-sh/ty/issues/1138.
Author
Parents
Loading