A lazily evaluated mapping, to be used as the `locals` argument during annotations evaluation. While the [`eval`][eval] function expects a mapping as the `locals` argument, it only performs `__getitem__` calls. The [`Mapping`][collections.abc.Mapping] abstract base class is fully implem
| 64 | # Note that this class is almost identical to `collections.ChainMap`, but need to enforce |
| 65 | # immutable mappings here: |
| 66 | class LazyLocalNamespace(Mapping[str, Any]): |
| 67 | """A lazily evaluated mapping, to be used as the `locals` argument during annotations evaluation. |
| 68 | |
| 69 | While the [`eval`][eval] function expects a mapping as the `locals` argument, it only |
| 70 | performs `__getitem__` calls. The [`Mapping`][collections.abc.Mapping] abstract base class |
| 71 | is fully implemented only for type checking purposes. |
| 72 | |
| 73 | Args: |
| 74 | *namespaces: The namespaces to consider, in ascending order of priority. |
| 75 | |
| 76 | Example: |
| 77 | ```python {lint="skip" test="skip"} |
| 78 | ns = LazyLocalNamespace({'a': 1, 'b': 2}, {'a': 3}) |
| 79 | ns['a'] |
| 80 | #> 3 |
| 81 | ns['b'] |
| 82 | #> 2 |
| 83 | ``` |
| 84 | """ |
| 85 | |
| 86 | def __init__(self, *namespaces: MappingNamespace) -> None: |
| 87 | self._namespaces = namespaces |
| 88 | |
| 89 | @cached_property |
| 90 | def data(self) -> dict[str, Any]: |
| 91 | return {k: v for ns in self._namespaces for k, v in ns.items()} |
| 92 | |
| 93 | def __len__(self) -> int: |
| 94 | return len(self.data) |
| 95 | |
| 96 | def __getitem__(self, key: str) -> Any: |
| 97 | return self.data[key] |
| 98 | |
| 99 | def __contains__(self, key: object) -> bool: |
| 100 | return key in self.data |
| 101 | |
| 102 | def __iter__(self) -> Iterator[str]: |
| 103 | return iter(self.data) |
| 104 | |
| 105 | |
| 106 | def ns_for_function(obj: Callable[..., Any], parent_namespace: MappingNamespace | None = None) -> NamespacesTuple: |
no outgoing calls
no test coverage detected