Coverage for python / lsst / daf / butler / nonempty_mapping.py: 44%
39 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:18 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:18 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30__all__ = ("NonemptyMapping",)
32from collections.abc import Callable, Iterator, Mapping
33from typing import Any, Protocol, Self, TypeVar, overload
36class Copyable(Protocol):
37 def copy(self) -> Self: ... 37 ↛ exitline 37 didn't return from function 'copy' because
40_K = TypeVar("_K")
41_T = TypeVar("_T")
42_V = TypeVar("_V", bound=Copyable, covariant=True)
45class NonemptyMapping(Mapping[_K, _V]):
46 """A `~collections.abc.Mapping` that implicitly adds values (like
47 `~collections.defaultdict`) but treats any that evaluate to `False` as not
48 present.
50 Parameters
51 ----------
52 default_factory : `~collections.abc.Callable`
53 A callable that takes no arguments and returns a new instance of the
54 value type.
56 Notes
57 -----
58 Unlike `~collections.defaultdict`, this class implements only
59 `collections.abc.Mapping`, not `~collections.abc.MutableMapping`, and hence
60 it can be modified only by invoking ``__getitem__`` with a key that does
61 not exist. It is expected that the value type will be a mutable container
62 like `set` or `dict`, and that an empty nested container should be
63 considered equivalent to the absence of a key. The value type must have
64 a `copy` method that copies all mutable state.
65 """
67 def __init__(self, default_factory: Callable[[], _V]) -> None:
68 self._mapping: dict[_K, _V] = {}
69 self._next: _V = default_factory()
70 self._default_factory = default_factory
72 def __len__(self) -> int:
73 return sum(bool(v) for v in self._mapping.values())
75 def __iter__(self) -> Iterator[_K]:
76 for key, values in self._mapping.items():
77 if values:
78 yield key
80 def __getitem__(self, key: _K) -> _V:
81 # We use setdefault with an existing empty inner container (_next),
82 # since we expect that to usually return an existing object and we
83 # don't want the overhead of making a new inner container each time.
84 # When we do insert _next, we replace it.
85 if (value := self._mapping.setdefault(key, self._next)) is self._next:
86 self._next = self._default_factory()
87 return value
89 # We don't let Mapping implement `__contains__` or `get` for us, because we
90 # don't want the side-effect of adding an empty inner container from
91 # calling `__getitem__`.
93 def __contains__(self, key: Any) -> bool:
94 return bool(self._mapping.get(key))
96 @overload
97 def get(self, key: _K) -> _V | None: ... 97 ↛ exitline 97 didn't return from function 'get' because
99 @overload
100 def get(self, key: _K, default: _T) -> _V | _T: ... 100 ↛ exitline 100 didn't return from function 'get' because
102 def get(self, key: _K, default: Any = None) -> Any:
103 if value := self._mapping.get(key):
104 return value
105 return default
107 def copy(self) -> NonemptyMapping[_K, _V]:
108 """Return a copy of the mapping.
110 This deep-copies values while referencing the ``default_factory``
111 callback and keys.
112 """
113 result = NonemptyMapping[_K, _V](self._default_factory)
114 for k, v in self._mapping.items():
115 result._mapping[k] = v.copy()
116 return result