File: types.py

package info (click to toggle)
python-utils 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 396 kB
  • sloc: python: 2,135; makefile: 19; sh: 5
file content (181 lines) | stat: -rw-r--r-- 4,096 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
"""
This module provides type definitions and utility functions for type hinting.

It includes:
- Shorthand for commonly used types such as Optional and Union.
- Type aliases for various data structures and common types.
- Importing all types from the `typing` and `typing_extensions` modules.
- Importing specific types from the `types` module.

The module also configures Pyright to ignore wildcard import warnings.
"""
# pyright: reportWildcardImportFromLibrary=false
# ruff: noqa: F405

import datetime
import decimal
from re import Match, Pattern
from types import *  # pragma: no cover  # noqa: F403
from typing import *  # pragma: no cover  # noqa: F403

# import * does not import these in all Python versions
# Quickhand for optional because it gets so much use. If only Python had
# support for an optional type shorthand such as `SomeType?` instead of
# `Optional[SomeType]`.
# Since the Union operator is only supported for Python 3.10, we'll create a
# shorthand for it.
from typing import (
    IO,
    BinaryIO,
    Optional as O,  # noqa: N817
    TextIO,
    Union as U,  # noqa: N817
)

from typing_extensions import *  # type: ignore[no-redef,assignment] # noqa: F403

Scope = Dict[str, Any]
OptionalScope = O[Scope]
Number = U[int, float]
DecimalNumber = U[Number, decimal.Decimal]
ExceptionType = Type[Exception]
ExceptionsType = U[Tuple[ExceptionType, ...], ExceptionType]
StringTypes = U[str, bytes]

delta_type = U[datetime.timedelta, int, float]
timestamp_type = U[
    datetime.timedelta,
    datetime.date,
    datetime.datetime,
    str,
    int,
    float,
    None,
]

__all__ = [
    'IO',
    'TYPE_CHECKING',
    # ABCs (from collections.abc).
    'AbstractSet',
    # The types from the typing module.
    # Super-special typing primitives.
    'Annotated',
    'Any',
    # One-off things.
    'AnyStr',
    'AsyncContextManager',
    'AsyncGenerator',
    'AsyncGeneratorType',
    'AsyncIterable',
    'AsyncIterator',
    'Awaitable',
    # Other concrete types.
    'BinaryIO',
    'BuiltinFunctionType',
    'BuiltinMethodType',
    'ByteString',
    'Callable',
    # Concrete collection types.
    'ChainMap',
    'ClassMethodDescriptorType',
    'ClassVar',
    'CodeType',
    'Collection',
    'Concatenate',
    'Container',
    'ContextManager',
    'Coroutine',
    'CoroutineType',
    'Counter',
    'DecimalNumber',
    'DefaultDict',
    'Deque',
    'Dict',
    'DynamicClassAttribute',
    'Final',
    'ForwardRef',
    'FrameType',
    'FrozenSet',
    # Types from the `types` module.
    'FunctionType',
    'Generator',
    'GeneratorType',
    'Generic',
    'GetSetDescriptorType',
    'Hashable',
    'ItemsView',
    'Iterable',
    'Iterator',
    'KeysView',
    'LambdaType',
    'List',
    'Literal',
    'Mapping',
    'MappingProxyType',
    'MappingView',
    'Match',
    'MemberDescriptorType',
    'MethodDescriptorType',
    'MethodType',
    'MethodWrapperType',
    'ModuleType',
    'MutableMapping',
    'MutableSequence',
    'MutableSet',
    'NamedTuple',  # Not really a type.
    'NewType',
    'NoReturn',
    'Number',
    'Optional',
    'OptionalScope',
    'OrderedDict',
    'ParamSpec',
    'ParamSpecArgs',
    'ParamSpecKwargs',
    'Pattern',
    'Protocol',
    # Structural checks, a.k.a. protocols.
    'Reversible',
    'Sequence',
    'Set',
    'SimpleNamespace',
    'Sized',
    'SupportsAbs',
    'SupportsBytes',
    'SupportsComplex',
    'SupportsFloat',
    'SupportsIndex',
    'SupportsIndex',
    'SupportsInt',
    'SupportsRound',
    'Text',
    'TextIO',
    'TracebackType',
    'TracebackType',
    'Tuple',
    'Type',
    'TypeAlias',
    'TypeGuard',
    'TypeVar',
    'TypedDict',  # Not really a type.
    'Union',
    'ValuesView',
    'WrapperDescriptorType',
    'cast',
    'coroutine',
    'delta_type',
    'final',
    'get_args',
    'get_origin',
    'get_type_hints',
    'is_typeddict',
    'new_class',
    'no_type_check',
    'no_type_check_decorator',
    'overload',
    'prepare_class',
    'resolve_bases',
    'runtime_checkable',
    'timestamp_type',
]