Coverage for src/minihtml/_globals.py: 90%
17 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-27 21:41 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-27 21:41 +0100
1#!/usr/bin/env python3
2import sys
3from contextvars import ContextVar
4from typing import Any
6if sys.version_info >= (3, 11): 6 ↛ 9line 6 didn't jump to line 9 because the condition on line 6 was always true
7 from typing import Self
8else:
9 from typing_extensions import Self
12_globals_context = ContextVar[dict[type, Any]]("globals_context")
15class Globals:
16 def set(self) -> None:
17 g: dict[type, Any] | None = _globals_context.get(None)
18 if g is None:
19 g = {}
20 _globals_context.set(g)
21 g[type(self)] = self
23 @classmethod
24 def get(cls) -> Self:
25 return _globals_context.get()[cls]