File: types.py

package info (click to toggle)
python-polyfactory 2.22.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,892 kB
  • sloc: python: 11,338; makefile: 103; sh: 37
file content (31 lines) | stat: -rw-r--r-- 857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import TYPE_CHECKING, Any, NoReturn, Union

try:
    from types import NoneType, UnionType

    UNION_TYPES = {UnionType, Union}
except ImportError:
    UNION_TYPES = {Union}

    NoneType = type(None)  # type: ignore[misc]


class Frozendict(dict):
    def _immutable_error(self, *_: Any, **__: Any) -> NoReturn:
        msg = f"Unable to mutate {type(self).__name__}"
        raise TypeError(msg)

    # Override all mutation methods to prevent changes
    if not TYPE_CHECKING:
        __setitem__ = _immutable_error
        __delitem__ = _immutable_error
        clear = _immutable_error
        pop = _immutable_error
        popitem = _immutable_error
        update = _immutable_error

    def __hash__(self) -> int:  # type: ignore[override]
        return hash(tuple(self.items()))


__all__ = ("UNION_TYPES", "Frozendict", "NoneType")