Skip to content

Commit a274aa3

Browse files
committed
Make Context's repr debug friendly
When viewing `Context`s in stack traces/debuggers, it was not obvious which context one was looking at, especially if multiple contexts had values of similar types. With this change, the context name is visible in e.g. Django's debug mode exception page. Since dataclasses are already in use for other classes in htpy, I opted to make the `Context` class a dataclass too. The constructor should be exactly the same before and after this change. Before: ``` <htpy.Context object at 0x7f9af95a16d0>: <<Request: GET '/foo/bar/'> at 0x7f9af5e88410> ``` After: ``` Context(name='request', default=<class 'htpy._NO_DEFAULT'>): <<Request: GET '/foo/bar/'> at 0x7fab5e7a6ad0> ```
1 parent 457a6f4 commit a274aa3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

htpy/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ class _NO_DEFAULT:
148148
pass
149149

150150

151+
@dataclasses.dataclass(frozen=True)
151152
class Context(t.Generic[T]):
152-
def __init__(self, name: str, *, default: T | type[_NO_DEFAULT] = _NO_DEFAULT) -> None:
153-
self.name = name
154-
self.default = default
153+
name: str
154+
_: dataclasses.KW_ONLY
155+
default: T | type[_NO_DEFAULT] = _NO_DEFAULT
155156

156157
def provider(self, value: T, node: Node) -> ContextProvider[T]:
157158
return ContextProvider(self, value, node)

0 commit comments

Comments
 (0)