tomwojcik/starlette-context

Improving the __repr__ method of _Context

zachary822 opened this issue · 2 comments

UserDict.__repr__ attempting to print the contents of _Context.data outside of a starlette app raises starlette_context.errors.ContextDoesNotExistError. Can we make the __repr__ return something that indicates that rather than raising exception?

Current problem:

# some route.py
"""
Module docstring
"""
from starlette_context import context
...

# in python console
import route
help(route)  # raises exception

I agree, that this fallible attempt to read inner data to mimic the dict is problematic in our case.
One solution could be to enforce an opaque repr, such as <starlette_context.ctx_Context object>, as is the case in many other types.
It could be annoying in cases where the context is expected to be present, such as debugging while in the request cycle, which also uses the repr. However, digging into it can still reveal the data, so I don't feel like much usability would be lost.

@hhamana fixed this in #66. I'll release the fix within a week, I think. Thanks for opening the issue.