Skip to content

cls.__module__ attribute might not be set #1071

Description

@sobolevn

Repro:

>>> exec("""
... import msgspec
... S = msgspec.defstruct("S", [("x", int)])
... msgspec.convert({"x": 1}, type=S)
... """, {}, {})
Traceback (most recent call last):
  File "<python-input-8>", line 1, in <module>
    exec("""
    ~~~~^^^^
    import msgspec
    ^^^^^^^^^^^^^^
    S = msgspec.defstruct("S", [("x", int)])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    msgspec.convert({"x": 1}, type=S)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    """, {}, {})
    ^^^^^^^^^^^^
  File "<string>", line 4, in <module>
  File "/Users/sobolev/Desktop/msgspec/src/msgspec/_utils.py", line 136, in get_class_annotations
    cls_globals = getattr(sys.modules.get(cls.__module__, None), "__dict__", {})
                                          ^^^^^^^^^^^^^^
AttributeError: __module__. Did you mean '.__reduce__' instead of '.__module__'?

This happened to me while using pyperf that does a similar thing.
I will send a PR with the fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions