Debian Codegraph @

jedi/third_party/typeshed/stdlib/enum.pyi

raw symbols SCIP
1 import _typeshed
2 import sys
3 import types
4 from _typeshed import SupportsKeysAndGetItem, Unused
5 from builtins import property as _builtins_property
6 from collections.abc import Callable, Iterable, Iterator, Mapping
7 from typing import Any, Final, Generic, Literal, SupportsIndex, TypeVar, overload
8 from typing_extensions import Self, TypeAlias, disjoint_base
9
10 __all__ = ["EnumMeta", "Enum", "IntEnum", "Flag", "IntFlag", "auto", "unique"]
11
12 if sys.version_info >= (3, 11):
13 __all__ += [
14 "CONFORM",
15 "CONTINUOUS",
16 "EJECT",
17 "EnumCheck",
18 "EnumType",
19 "FlagBoundary",
20 "KEEP",
21 "NAMED_FLAGS",
22 "ReprEnum",
23 "STRICT",
24 "StrEnum",
25 "UNIQUE",
26 "global_enum",
27 "global_enum_repr",
28 "global_flag_repr",
29 "global_str",
30 "member",
31 "nonmember",
32 "property",
33 "verify",
34 "pickle_by_enum_name",
35 "pickle_by_global_name",
36 ]
37
38 if sys.version_info >= (3, 13):
39 __all__ += ["EnumDict"]
40
41 _EnumMemberT = TypeVar("_EnumMemberT")
42 _EnumerationT = TypeVar("_EnumerationT", bound=type[Enum])
43
44 # The following all work:
45 # >>> from enum import Enum
46 # >>> from string import ascii_lowercase
47 # >>> Enum('Foo', names='RED YELLOW GREEN')
48 # <enum 'Foo'>
49 # >>> Enum('Foo', names=[('RED', 1), ('YELLOW, 2)])
50 # <enum 'Foo'>
51 # >>> Enum('Foo', names=((x for x in (ascii_lowercase[i], i)) for i in range(5)))
52 # <enum 'Foo'>
53 # >>> Enum('Foo', names={'RED': 1, 'YELLOW': 2})
54 # <enum 'Foo'>
55 _EnumNames: TypeAlias = str | Iterable[str] | Iterable[Iterable[str | Any]] | Mapping[str, Any]
56 _Signature: TypeAlias = Any # TODO: Unable to import Signature from inspect module
57
58 if sys.version_info >= (3, 11):
59 class nonmember(Generic[_EnumMemberT]):
60 value: _EnumMemberT
61 def __init__(self, value: _EnumMemberT) -> None: ...
62
63 class member(Generic[_EnumMemberT]):
64 value: _EnumMemberT
65 def __init__(self, value: _EnumMemberT) -> None: ...
66
67 class _EnumDict(dict[str, Any]):
68 if sys.version_info >= (3, 13):
69 def __init__(self, cls_name: str | None = None) -> None: ...
70 else:
71 def __init__(self) -> None: ...
72
73 def __setitem__(self, key: str, value: Any) -> None: ...
74 if sys.version_info >= (3, 11):
75 # See comment above `typing.MutableMapping.update`
76 # for why overloads are preferable to a Union here
77 #
78 # Unlike with MutableMapping.update(), the first argument is required,
79 # hence the type: ignore
80 @overload # type: ignore[override]
81 def update(self, members: SupportsKeysAndGetItem[str, Any], **more_members: Any) -> None: ...
82 @overload
83 def update(self, members: Iterable[tuple[str, Any]], **more_members: Any) -> None: ...
84 if sys.version_info >= (3, 13):
85 @property
86 def member_names(self) -> list[str]: ...
87
88 if sys.version_info >= (3, 13):
89 EnumDict = _EnumDict
90
91 # Structurally: Iterable[T], Reversible[T], Container[T] where T is the enum itself
92 class EnumMeta(type):
93 if sys.version_info >= (3, 11):
94 def __new__(
95 metacls: type[_typeshed.Self],
96 cls: str,
97 bases: tuple[type, ...],
98 classdict: _EnumDict,
99 *,
100 boundary: FlagBoundary | None = None,
101 _simple: bool = False,
102 **kwds: Any,
103 ) -> _typeshed.Self: ...
104 else:
105 def __new__(
106 metacls: type[_typeshed.Self], cls: str, bases: tuple[type, ...], classdict: _EnumDict, **kwds: Any
107 ) -> _typeshed.Self: ...
108
109 @classmethod
110 def __prepare__(metacls, cls: str, bases: tuple[type, ...], **kwds: Any) -> _EnumDict: ... # type: ignore[override]
111 def __iter__(self: type[_EnumMemberT]) -> Iterator[_EnumMemberT]: ...
112 def __reversed__(self: type[_EnumMemberT]) -> Iterator[_EnumMemberT]: ...
113 if sys.version_info >= (3, 12):
114 def __contains__(self: type[Any], value: object) -> bool: ...
115 elif sys.version_info >= (3, 11):
116 def __contains__(self: type[Any], member: object) -> bool: ...
117 elif sys.version_info >= (3, 10):
118 def __contains__(self: type[Any], obj: object) -> bool: ...
119 else:
120 def __contains__(self: type[Any], member: object) -> bool: ...
121
122 def __getitem__(self: type[_EnumMemberT], name: str) -> _EnumMemberT: ...
123 @_builtins_property
124 def __members__(self: type[_EnumMemberT]) -> types.MappingProxyType[str, _EnumMemberT]: ...
125 def __len__(self) -> int: ...
126 def __bool__(self) -> Literal[True]: ...
127 def __dir__(self) -> list[str]: ...
128
129 # Overload 1: Value lookup on an already existing enum class (simple case)
130 @overload
131 def __call__(cls: type[_EnumMemberT], value: Any, names: None = None) -> _EnumMemberT: ...
132
133 # Overload 2: Functional API for constructing new enum classes.
134 if sys.version_info >= (3, 11):
135 @overload
136 def __call__(
137 cls,
138 value: str,
139 names: _EnumNames,
140 *,
141 module: str | None = None,
142 qualname: str | None = None,
143 type: type | None = None,
144 start: int = 1,
145 boundary: FlagBoundary | None = None,
146 ) -> type[Enum]: ...
147 else:
148 @overload
149 def __call__(
150 cls,
151 value: str,
152 names: _EnumNames,
153 *,
154 module: str | None = None,
155 qualname: str | None = None,
156 type: type | None = None,
157 start: int = 1,
158 ) -> type[Enum]: ...
159
160 # Overload 3 (py312+ only): Value lookup on an already existing enum class (complex case)
161 #
162 # >>> class Foo(enum.Enum):
163 # ... X = 1, 2, 3
164 # >>> Foo(1, 2, 3)
165 # <Foo.X: (1, 2, 3)>
166 #
167 if sys.version_info >= (3, 12):
168 @overload
169 def __call__(cls: type[_EnumMemberT], value: Any, *values: Any) -> _EnumMemberT: ...
170 if sys.version_info >= (3, 14):
171 @property
172 def __signature__(cls) -> _Signature: ...
173
174 _member_names_: list[str] # undocumented
175 _member_map_: dict[str, Enum] # undocumented
176 _value2member_map_: dict[Any, Enum] # undocumented
177
178 if sys.version_info >= (3, 11):
179 # In 3.11 `EnumMeta` metaclass is renamed to `EnumType`, but old name also exists.
180 EnumType = EnumMeta
181
182 class property(types.DynamicClassAttribute):
183 def __set_name__(self, ownerclass: type[Enum], name: str) -> None: ...
184 name: str
185 clsname: str
186 member: Enum | None
187
188 _magic_enum_attr = property
189 else:
190 _magic_enum_attr = types.DynamicClassAttribute
191
192 class Enum(metaclass=EnumMeta):
193 @_magic_enum_attr
194 def name(self) -> str: ...
195 @_magic_enum_attr
196 def value(self) -> Any: ...
197 _name_: str
198 _value_: Any
199 _ignore_: str | list[str]
200 _order_: str
201 __order__: str
202 @classmethod
203 def _missing_(cls, value: object) -> Any: ...
204 @staticmethod
205 def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any: ...
206 # It's not true that `__new__` will accept any argument type,
207 # so ideally we'd use `Any` to indicate that the argument type is inexpressible.
208 # However, using `Any` causes too many false-positives for those using mypy's `--disallow-any-expr`
209 # (see #7752, #2539, mypy/#5788),
210 # and in practice using `object` here has the same effect as using `Any`.
211 def __new__(cls, value: object) -> Self: ...
212 def __dir__(self) -> list[str]: ...
213 def __hash__(self) -> int: ...
214 def __format__(self, format_spec: str) -> str: ...
215 def __reduce_ex__(self, proto: Unused) -> tuple[Any, ...]: ...
216 if sys.version_info >= (3, 11):
217 def __copy__(self) -> Self: ...
218 def __deepcopy__(self, memo: Any) -> Self: ...
219 if sys.version_info >= (3, 12) and sys.version_info < (3, 14):
220 @classmethod
221 def __signature__(cls) -> str: ...
222 if sys.version_info >= (3, 13):
223 # Value may be any type, even in special enums. Enabling Enum parsing from
224 # multiple value types
225 def _add_value_alias_(self, value: Any) -> None: ...
226 def _add_alias_(self, name: str) -> None: ...
227
228 if sys.version_info >= (3, 11):
229 class ReprEnum(Enum): ...
230
231 if sys.version_info >= (3, 12):
232 class IntEnum(int, ReprEnum):
233 _value_: int
234 @_magic_enum_attr
235 def value(self) -> int: ...
236 def __new__(cls, value: int) -> Self: ...
237
238 else:
239 if sys.version_info >= (3, 11):
240 _IntEnumBase = ReprEnum
241 else:
242 _IntEnumBase = Enum
243
244 @disjoint_base
245 class IntEnum(int, _IntEnumBase):
246 _value_: int
247 @_magic_enum_attr
248 def value(self) -> int: ...
249 def __new__(cls, value: int) -> Self: ...
250
251 def unique(enumeration: _EnumerationT) -> _EnumerationT: ...
252
253 _auto_null: Any
254
255 class Flag(Enum):
256 _name_: str | None # type: ignore[assignment]
257 _value_: int
258 _numeric_repr_: Callable[[int], str]
259 @_magic_enum_attr
260 def name(self) -> str | None: ... # type: ignore[override]
261 @_magic_enum_attr
262 def value(self) -> int: ...
263 def __contains__(self, other: Self) -> bool: ...
264 def __bool__(self) -> bool: ...
265 def __or__(self, other: Self) -> Self: ...
266 def __and__(self, other: Self) -> Self: ...
267 def __xor__(self, other: Self) -> Self: ...
268 def __invert__(self) -> Self: ...
269 if sys.version_info >= (3, 11):
270 def __iter__(self) -> Iterator[Self]: ...
271 def __len__(self) -> int: ...
272 __ror__ = __or__
273 __rand__ = __and__
274 __rxor__ = __xor__
275
276 if sys.version_info >= (3, 11):
277 class StrEnum(str, ReprEnum):
278 def __new__(cls, value: str) -> Self: ...
279 _value_: str
280 @_magic_enum_attr
281 def value(self) -> str: ...
282 @staticmethod
283 def _generate_next_value_(name: str, start: int, count: int, last_values: list[str]) -> str: ...
284
285 class EnumCheck(StrEnum):
286 CONTINUOUS = "no skipped integer values"
287 NAMED_FLAGS = "multi-flag aliases may not contain unnamed flags"
288 UNIQUE = "one name per value"
289
290 CONTINUOUS: Final = EnumCheck.CONTINUOUS
291 NAMED_FLAGS: Final = EnumCheck.NAMED_FLAGS
292 UNIQUE: Final = EnumCheck.UNIQUE
293
294 class verify:
295 def __init__(self, *checks: EnumCheck) -> None: ...
296 def __call__(self, enumeration: _EnumerationT) -> _EnumerationT: ...
297
298 class FlagBoundary(StrEnum):
299 STRICT = "strict"
300 CONFORM = "conform"
301 EJECT = "eject"
302 KEEP = "keep"
303
304 STRICT: Final = FlagBoundary.STRICT
305 CONFORM: Final = FlagBoundary.CONFORM
306 EJECT: Final = FlagBoundary.EJECT
307 KEEP: Final = FlagBoundary.KEEP
308
309 def global_str(self: Enum) -> str: ...
310 def global_enum(cls: _EnumerationT, update_str: bool = False) -> _EnumerationT: ...
311 def global_enum_repr(self: Enum) -> str: ...
312 def global_flag_repr(self: Flag) -> str: ...
313 def show_flag_values(value: int) -> list[int]: ...
314 def bin(num: SupportsIndex, max_bits: int | None = None) -> str: ...
315
316 if sys.version_info >= (3, 12):
317 # The body of the class is the same, but the base classes are different.
318 class IntFlag(int, ReprEnum, Flag, boundary=KEEP): # type: ignore[misc] # complaints about incompatible bases
319 def __new__(cls, value: int) -> Self: ...
320 def __or__(self, other: int) -> Self: ...
321 def __and__(self, other: int) -> Self: ...
322 def __xor__(self, other: int) -> Self: ...
323 def __invert__(self) -> Self: ...
324 __ror__ = __or__
325 __rand__ = __and__
326 __rxor__ = __xor__
327
328 elif sys.version_info >= (3, 11):
329 # The body of the class is the same, but the base classes are different.
330 @disjoint_base
331 class IntFlag(int, ReprEnum, Flag, boundary=KEEP): # type: ignore[misc] # complaints about incompatible bases
332 def __new__(cls, value: int) -> Self: ...
333 def __or__(self, other: int) -> Self: ...
334 def __and__(self, other: int) -> Self: ...
335 def __xor__(self, other: int) -> Self: ...
336 def __invert__(self) -> Self: ...
337 __ror__ = __or__
338 __rand__ = __and__
339 __rxor__ = __xor__
340
341 else:
342 @disjoint_base
343 class IntFlag(int, Flag): # type: ignore[misc] # complaints about incompatible bases
344 def __new__(cls, value: int) -> Self: ...
345 def __or__(self, other: int) -> Self: ...
346 def __and__(self, other: int) -> Self: ...
347 def __xor__(self, other: int) -> Self: ...
348 def __invert__(self) -> Self: ...
349 __ror__ = __or__
350 __rand__ = __and__
351 __rxor__ = __xor__
352
353 class auto:
354 _value_: Any
355 @_magic_enum_attr
356 def value(self) -> Any: ...
357 def __new__(cls) -> Self: ...
358
359 # These don't exist, but auto is basically immediately replaced with
360 # either an int or a str depending on the type of the enum. StrEnum's auto
361 # shouldn't have these, but they're needed for int versions of auto (mostly the __or__).
362 # Ideally type checkers would special case auto enough to handle this,
363 # but until then this is a slightly inaccurate helping hand.
364 def __or__(self, other: int | Self) -> Self: ...
365 def __and__(self, other: int | Self) -> Self: ...
366 def __xor__(self, other: int | Self) -> Self: ...
367 __ror__ = __or__
368 __rand__ = __and__
369 __rxor__ = __xor__
370
371 if sys.version_info >= (3, 11):
372 def pickle_by_global_name(self: Enum, proto: int) -> str: ...
373 def pickle_by_enum_name(self: _EnumMemberT, proto: int) -> tuple[Callable[..., Any], tuple[type[_EnumMemberT], str]]: ...