File: type_def.py

package info (click to toggle)
dataclass-wizard 0.39.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,112 kB
  • sloc: python: 19,560; makefile: 126; javascript: 23
file content (237 lines) | stat: -rw-r--r-- 6,109 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
__all__ = [
    'Buffer',
    'Unpack',
    'PyForwardRef',
    'PyProtocol',
    'PyDeque',
    'PyTypedDict',
    'PyRequired',
    'PyNotRequired',
    'PyReadOnly',
    'PyLiteralString',
    'FrozenKeys',
    'DefFactory',
    'NoneType',
    'ExplicitNullType',
    'ExplicitNull',
    'JSONList',
    'JSONObject',
    'ListOfJSONObject',
    'JSONValue',
    'FileType',
    'EnvFileType',
    'StrCollection',
    'ParseFloat',
    'Encoder',
    'FileEncoder',
    'Decoder',
    'FileDecoder',
    'NUMBERS',
    'T',
    'E',
    'U',
    'M',
    'NT',
    'DT',
    'DD',
    'N',
    'S',
    'LT',
    'LSQ',
    'FREF',
    'dataclass_transform',
]

from collections import deque, defaultdict
from datetime import date, time, datetime
from enum import Enum
from os import PathLike
from typing import (
    Any, TypeVar, Sequence, Mapping,
    Union, NamedTuple, Callable, AnyStr, TextIO, BinaryIO,
    Deque as PyDeque,
    ForwardRef as PyForwardRef,
    Protocol as PyProtocol,
    TypedDict as PyTypedDict, Iterable, Collection,
)
from uuid import UUID

from .constants import PY310_OR_ABOVE, PY311_OR_ABOVE, PY313_OR_ABOVE, PY312_OR_ABOVE

# The class of the `None` singleton, cached for re-usability
if PY310_OR_ABOVE:
    # https://docs.python.org/3/library/types.html#types.NoneType
    from types import NoneType
else:
    # "Cannot assign to a type"
    NoneType = type(None)  # type: ignore[misc]

# Type check for numeric types - needed because `bool` is technically
# a Number.
NUMBERS = int, float

# Generic type
T = TypeVar('T')
TT = TypeVar('TT')

# Enum subclass type
E = TypeVar('E', bound=Enum)

# UUID subclass type
U = TypeVar('U', bound=UUID)

# Mapping type
M = TypeVar('M', bound=Mapping)

# NamedTuple type
NT = TypeVar('NT', bound=NamedTuple)

# Date, time, or datetime type
DT = TypeVar('DT', date, time, datetime)

# DefaultDict type
DD = TypeVar('DD', bound=defaultdict)

# Numeric type
N = Union[int, float]

# Sequence type
S = TypeVar('S', bound=Sequence)

# List or Tuple type
LT = TypeVar('LT', list, tuple)

# List, Set, or Deque (Double ended queue) type
LSQ = TypeVar('LSQ', list, set, frozenset, deque)

# A fixed set of key names
FrozenKeys = frozenset[str]

# Default factory type, assuming a no-args constructor
DefFactory = Callable[[], T]

# Valid collection types in JSON.
JSONList = list[Any]
JSONObject = dict[str, Any]
ListOfJSONObject = list[JSONObject]

# Valid value types in JSON.
JSONValue = Union[None, str, bool, int, float, JSONList, JSONObject]

# File-type argument, compatible with the type of `file` for `open`
FileType = Union[str, bytes, PathLike, int]

# DotEnv file-type argument (string, tuple of string, boolean, or None)
EnvFileType = Union[bool, FileType, Iterable[FileType], None]

# Type for a string or a collection of strings.
StrCollection = Union[str, Collection[str]]

# Python 3.11 introduced `Required` and `NotRequired` wrappers for
# `TypedDict` fields (PEP 655). Python 3.9+ users can import the
# wrappers from `typing_extensions`.

if PY313_OR_ABOVE:  # pragma: no cover
    from collections.abc import Buffer

    from typing import (Unpack,
                        Required as PyRequired,
                        NotRequired as PyNotRequired,
                        ReadOnly as PyReadOnly,
                        LiteralString as PyLiteralString,
                        dataclass_transform)
elif PY311_OR_ABOVE:  # pragma: no cover
    if PY312_OR_ABOVE:
        from collections.abc import Buffer
    else:
        from typing_extensions import Buffer

    from typing import (Unpack,
                        Required as PyRequired,
                        NotRequired as PyNotRequired,
                        LiteralString as PyLiteralString,
                        dataclass_transform)
    from typing_extensions import ReadOnly as PyReadOnly
else:
    from typing_extensions import (Unpack,
                                   Buffer,
                                   Required as PyRequired,
                                   NotRequired as PyNotRequired,
                                   ReadOnly as PyReadOnly,
                                   LiteralString as PyLiteralString,
                                   dataclass_transform)

# Forward references can be either strings or explicit `ForwardRef` objects.
# noinspection SpellCheckingInspection
FREF = TypeVar('FREF', str, PyForwardRef)


class ExplicitNullType:
    __slots__ = ()  # Saves memory by preventing the creation of instance dictionaries

    # Class-level instance variable for singleton control
    _instance: "ExplicitNullType | None" = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(ExplicitNullType, cls).__new__(cls)
        return cls._instance

    def __bool__(self):
        return False

    def __repr__(self):
        return '<ExplicitNull>'


# Create the singleton instance
ExplicitNull = ExplicitNullType()

# Type annotations
ParseFloat = Callable[[str], Any]


class Encoder(PyProtocol):
    """
    Represents an encoder for Python object -> JSON, e.g. analogous to
    `json.dumps`
    """

    def __call__(self, obj: Union[JSONObject, JSONList],
                 /,
                 *args,
                 **kwargs) -> str:
        ...


class FileEncoder(PyProtocol):
    """
    Represents an encoder for Python object -> JSON file, e.g. analogous to
    `json.dump`
    """

    def __call__(self, obj: Union[JSONObject, JSONList],
                 file: Union[TextIO, BinaryIO],
                 **kwargs) -> AnyStr:
        ...


class Decoder(PyProtocol):
    """
    Represents a decoder for JSON -> Python object, e.g. analogous to
    `json.loads`
    """

    def __call__(self, s: AnyStr,
                 **kwargs) -> Union[JSONObject, ListOfJSONObject]:
        ...


class FileDecoder(PyProtocol):
    """
    Represents a decoder for JSON file -> Python object, e.g. analogous to
    `json.load`
    """
    def __call__(self, file: Union[TextIO, BinaryIO],
                 **kwargs) -> Union[JSONObject, ListOfJSONObject]:
        ...