Skip to content

__dict__ attribute of a meta-class is incorrectly typed. #11033

@tim-mitchell

Description

@tim-mitchell

Initially lodged as python/mypy#16501

Given a metaclass called Meta, mypy considers the Meta.dict attribute to have type def (self: builtins.type) -> types.MappingProxyType[builtins.str, Any] when the actual type is types.MappingProxyType[builtins.str, Any].

To Reproduce
Run mypy on this file.

class Meta(type):
    def __call__(cls, *args, **kw):
        return super().__call__(*args, **kw)

reveal_type(Meta.__dict__)
call = Meta.__dict__['__call__']  # mypy raises error here

https://mypy-play.net/?mypy=latest&python=3.11&gist=843cafceeef590efc6d3705b44c29c19

Expected Behavior

reveal_type(Meta.__dict__) returns types.MappingProxyType[builtins.str, Any]

Actual Behavior

mypy_test.py:6: note: Revealed type is "def (self: builtins.type) -> types.MappingProxyType[builtins.str, Any]"

This is likely due to builtins.pyi defining __dict__ as a property.

class type:
    ...
    @property
    def __dict__(self) -> types.MappingProxyType[str, Any]: ...  # type: ignore[override]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions