Skip to content

using inspect.unwrap on maurix.types.Obj places the object into a state where serializing raises a RecursionError #176

@MinekPo1

Description

@MinekPo1
>>> import mautrix.types
>>> import inspect
>>> inspect.unwrap(mautrix.types.Obj())
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    inspect.unwrap(mautrix.types.Obj())
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/home/lily/.pyenv/versions/3.13.2/lib/python3.13/inspect.py", line 791, in unwrap
    raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/home/lily/Documents/matrix-codecrimes/.venv/lib/python3.13/site-packages/mautrix/types/util/obj.py", line 36, in __repr__
    return repr(self.serialize())
                ~~~~~~~~~~~~~~^^
  File "/home/lily/Documents/matrix-codecrimes/.venv/lib/python3.13/site-packages/mautrix/types/util/obj.py", line 62, in serialize
    k: v.serialize() if isinstance(v, Serializable) else v
       ~~~~~~~~~~~^^
  File "/home/lily/Documents/matrix-codecrimes/.venv/lib/python3.13/site-packages/mautrix/types/util/obj.py", line 62, in serialize
    k: v.serialize() if isinstance(v, Serializable) else v
       ~~~~~~~~~~~^^
  File "/home/lily/Documents/matrix-codecrimes/.venv/lib/python3.13/site-packages/mautrix/types/util/obj.py", line 62, in serialize
    k: v.serialize() if isinstance(v, Serializable) else v
       ~~~~~~~~~~~^^
  [Previous line repeated 986 more times]
RecursionError: maximum recursion depth exceeded

this appears to be caused because inspect.wrapper attempts to access .__wrapped__ which causes the __getattr__ to create it, which is bad.

maybe it would be a good idea not to create __dunder__ and _sunder_ names? so something like:

def __getattr__(self, name):
    if name.startswith("_") and name.endswith("_"):
        raise AttributeError(name)
    ...

just to prevent brainfarts: this wont prevent access to defined dunders since __getattr__ is only called when the name cannot be found through other means.

Metadata

Metadata

Assignees

No one assigned

    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