<!-- If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not the right place to seek help. Consider the following options instead: - reading the Python tutorial: https://docs.python.org/3/tutorial/ - posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7 - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list - searching our issue tracker (https://github.com/python/cpython/issues) to see if your problem has already been reported --> # Bug report A clear and concise description of what the bug is. Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible. Repo: ``` class MyConfig(dict): def __getattr__(self, x): return self[x] obj = MyConfig(offset=5) obj_weakref = weakref.ref(obj) str(obj_weakref) # raise error: KeyError: '__name__' ``` Error: ``` Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in __getattr__ KeyError: '__name__' ``` # Your environment <!-- Include as many relevant details as possible about the environment you experienced the bug in --> - CPython versions tested on: python 3.8 & 3.9 - Operating system and architecture: Ubuntu Linux and MacOS <!-- You can freely edit this text. Remove any lines you believe are unnecessary. --> <!-- gh-pr-number: gh-99244 --> * PR: gh-99244 <!-- /gh-pr-number --> <!-- gh-pr-number: gh-103789 --> * PR: gh-103789 <!-- /gh-pr-number -->