|
| 1 | +import enum |
1 | 2 | import sys
|
2 | 3 | import types
|
3 | 4 | from builtins import type as Type # alias to avoid name clashes with fields named "type"
|
4 |
| -from typing import Any, Callable, Generic, Iterable, Mapping, Protocol, TypeVar, overload |
| 5 | +from typing import Any, Callable, Generic, Iterable, Literal, Mapping, Protocol, TypeVar, overload |
5 | 6 |
|
6 | 7 | if sys.version_info >= (3, 9):
|
7 | 8 | from types import GenericAlias
|
8 | 9 |
|
9 | 10 | _T = TypeVar("_T")
|
10 | 11 | _T_co = TypeVar("_T_co", covariant=True)
|
11 | 12 |
|
12 |
| -class _MISSING_TYPE: ... |
| 13 | +# define _MISSING_TYPE as an enum within the type stubs, |
| 14 | +# even though that is not really its type at runtime |
| 15 | +# this allows us to use Literal[MISSING] |
| 16 | +# for background, see: |
| 17 | +# https://github.com/python/typeshed/pull/5900#issuecomment-895513797 |
| 18 | +class _MISSING_TYPE(enum.Enum): |
| 19 | + MISSING = enum.auto() |
13 | 20 |
|
14 |
| -MISSING: _MISSING_TYPE |
| 21 | +MISSING = _MISSING_TYPE.MISSING |
15 | 22 |
|
16 | 23 | if sys.version_info >= (3, 10):
|
17 | 24 | class KW_ONLY: ...
|
@@ -72,15 +79,15 @@ class _DefaultFactory(Protocol[_T_co]):
|
72 | 79 | class Field(Generic[_T]):
|
73 | 80 | name: str
|
74 | 81 | type: Type[_T]
|
75 |
| - default: _T |
76 |
| - default_factory: _DefaultFactory[_T] |
| 82 | + default: _T | Literal[MISSING] |
| 83 | + default_factory: _DefaultFactory[_T] | Literal[MISSING] |
77 | 84 | repr: bool
|
78 | 85 | hash: bool | None
|
79 | 86 | init: bool
|
80 | 87 | compare: bool
|
81 | 88 | metadata: types.MappingProxyType[Any, Any]
|
82 | 89 | if sys.version_info >= (3, 10):
|
83 |
| - kw_only: bool |
| 90 | + kw_only: bool | Literal[MISSING] |
84 | 91 | def __init__(
|
85 | 92 | self,
|
86 | 93 | default: _T,
|
|
0 commit comments