1
1
import sys
2
+ import types as python_types
2
3
import typing
3
4
from typing import Any
4
5
from typing import Dict
6
+ from typing import FrozenSet
5
7
from typing import Iterable
6
8
from typing import List
7
9
from typing import Literal
31
33
32
34
import fgpyo .util .types as types
33
35
36
+ attr : Optional [python_types .ModuleType ]
37
+ MISSING : FrozenSet [Any ]
38
+
34
39
try :
35
40
import attr
36
41
37
42
_use_attr = True
38
- from attr import NOTHING as ATTR_NOTHING
39
43
from attr import fields as get_attr_fields
40
44
from attr import fields_dict as get_attr_fields_dict
41
45
42
- Attribute = attr .Attribute
46
+ Attribute : TypeAlias = attr .Attribute # type: ignore[name-defined, no-redef]
43
47
# dataclasses and attr have internal tokens for missing values, join into a set so that we can
44
48
# check if a value is missing without knowing the type of backing class
45
- MISSING = {DATACLASSES_MISSING , ATTR_NOTHING }
46
- except ImportError :
49
+ MISSING = frozenset ( {DATACLASSES_MISSING , attr . NOTHING })
50
+ except ImportError : # pragma: no cover
47
51
_use_attr = False
48
52
attr = None
49
- ATTR_NOTHING = None
50
- Attribute = TypeVar ("Attribute" , bound = object ) # type: ignore[misc, assignment]
53
+ Attribute : TypeAlias = TypeVar ("Attribute" , bound = object ) # type: ignore[misc, assignment, no-redef] # noqa: E501
51
54
52
55
# define empty placeholders for getting attr fields as a tuple or dict. They will never be
53
56
# called because the import failed; but they're here to ensure that the function is defined in
@@ -62,17 +65,17 @@ def get_attr_fields_dict(cls: type) -> Dict[str, dataclasses.Field]: # type: ig
62
65
return {}
63
66
64
67
# for consistency with successful import of attr, create a set for missing values
65
- MISSING = {DATACLASSES_MISSING }
68
+ MISSING = frozenset ( {DATACLASSES_MISSING })
66
69
67
- if TYPE_CHECKING :
70
+ if TYPE_CHECKING : # pragma: no cover
68
71
from _typeshed import DataclassInstance as DataclassesProtocol
69
72
else :
70
73
71
74
class DataclassesProtocol (Protocol ):
72
75
__dataclasses_fields__ : Dict [str , dataclasses .Field ]
73
76
74
77
75
- if TYPE_CHECKING and _use_attr :
78
+ if TYPE_CHECKING and _use_attr : # pragma: no cover
76
79
from attr import AttrsInstance
77
80
else :
78
81
@@ -85,7 +88,7 @@ def is_attr_class(cls: type) -> bool: # type: ignore[arg-type]
85
88
return hasattr (cls , "__attrs_attrs__" )
86
89
87
90
88
- _MISSING_OR_NONE = {* MISSING , None }
91
+ _MISSING_OR_NONE : FrozenSet [ Any ] = frozenset ( {* MISSING , None })
89
92
"""Set of values that are considered missing or None for dataclasses or attr classes"""
90
93
_DataclassesOrAttrClass : TypeAlias = Union [DataclassesProtocol , AttrsInstance ]
91
94
"""
@@ -148,7 +151,7 @@ def split_at_given_level(
148
151
return out_vals
149
152
150
153
151
- NoneType = type (None )
154
+ NoneType : TypeAlias = type (None ) # type: ignore[no-redef]
152
155
153
156
154
157
def list_parser (
0 commit comments