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

Open
#176 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Start in mautrix/types/util/obj.py, especially getattr and serialize(), and reproduce the inspect.unwrap(mautrix.types.Obj()) example from the issue. Check that missing dunder or sunder names are not created dynamically, while normal missing attributes retain their existing behavior. Done means the example no longer leaves the object recursively serializable and raises only the expected unwrap error.

Written by the indexing model from the issue text.

Description

bug
>>> 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.

Dominant language
Python
Stars
249
Forks
84
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mautrix/python

All issues in mautrix/python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.