Skip to content

mypyc: Cls.__dict__ is mappingproxy, not dict #14056

@kai3341

Description

@kai3341

Bug Report

It looks mypy's type inference works fine and it's mypyc (maybe) bug
Maybe it's connected to builtins vars signature -- vars by the signature should return dict
But it's false, vars may return mappingproxy:

>>> class Foo:
... 
>>> Foo.__dict__
mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None})
>>> vars(Foo)
mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None})
>>>

To Reproduce

from typing import Type, TypeVar

T = TypeVar("T")


def decorate(cls: Type[T]) -> Type[T]:
    clsdict = cls.__dict__
    # clsdict = vars(cls)
    print(clsdict)
    return cls


@decorate
class Foo:
    pass

Then build it via mypy:

(.venv) kai@asus-ux360c:~$ mypyc mypyc_weird.py 
running build_ext
copying build/lib.linux-x86_64-3.9/mypyc_weird.cpython-39-x86_64-linux-gnu.so -> 

And run it

Expected Behavior

(.venv) kai@asus-ux360c:~$ python -m mypyc_weird
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None}

Actual Behavior

(.venv) kai@asus-ux360c:~$ python -m mypyc_weird.cpython-39-x86_64-linux-gnu.so 
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "mypyc_weird.py", line 14, in <module>
    class Foo:
  File "mypyc_weird.py", line 7, in decorate
    clsdict = cls.__dict__
TypeError: dict object expected; got mappingproxy

Your Environment

  • Mypy version used: mypy-1.0.0+dev.dbcbb3f5c3ef791c98088da0bd1dfa6cbf51f301 (latest@git)
  • Mypy command-line flags: empty
  • Mypy configuration options from mypy.ini (and other config files): empty
  • Python version used: 3.9
  • Description: Debian GNU/Linux 11 (bullseye)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions