11
11
from typing import Type
12
12
from typing import Union
13
13
14
- if sys .version_info >= (3 , 12 ):
14
+ if sys .version_info >= (3 , 10 ):
15
15
from typing import TypeAlias
16
16
else :
17
17
from typing_extensions import TypeAlias
@@ -85,9 +85,18 @@ def is_attr_class(cls: type) -> bool: # type: ignore[arg-type]
85
85
return hasattr (cls , "__attrs_attrs__" )
86
86
87
87
88
- MISSING_OR_NONE = {* MISSING , None }
89
- DataclassesOrAttrClass = Union [DataclassesProtocol , AttrsInstance ]
88
+ _MISSING_OR_NONE = {* MISSING , None }
89
+ """Set of values that are considered missing or None for dataclasses or attr classes"""
90
+ _DataclassesOrAttrClass : TypeAlias = Union [DataclassesProtocol , AttrsInstance ]
91
+ """
92
+ TypeAlias for dataclasses or attr classes. Mostly nonsense because they are not true types, they
93
+ are traits, but there is no python trait-tester.
94
+ """
90
95
FieldType : TypeAlias = Union [dataclasses .Field , Attribute ]
96
+ """
97
+ TypeAlias for dataclass Fields or attrs Attributes. It will correspond to the correct type for the
98
+ corresponding _DataclassesOrAttrClass
99
+ """
91
100
92
101
93
102
def get_dataclasses_fields_dict (
@@ -378,7 +387,7 @@ def get_parser() -> partial:
378
387
379
388
380
389
def get_fields_dict (
381
- cls : Union [DataclassesOrAttrClass , Type [DataclassesOrAttrClass ]]
390
+ cls : Union [_DataclassesOrAttrClass , Type [_DataclassesOrAttrClass ]]
382
391
) -> Mapping [str , FieldType ]:
383
392
"""Get the fields dict from either a dataclasses or attr dataclass (or instance)"""
384
393
if is_dataclasses_class (cls ):
@@ -390,7 +399,7 @@ def get_fields_dict(
390
399
391
400
392
401
def get_fields (
393
- cls : Union [DataclassesOrAttrClass , Type [DataclassesOrAttrClass ]]
402
+ cls : Union [_DataclassesOrAttrClass , Type [_DataclassesOrAttrClass ]]
394
403
) -> Tuple [FieldType , ...]:
395
404
"""Get the fields tuple from either a dataclasses or attr dataclass (or instance)"""
396
405
if is_dataclasses_class (cls ):
@@ -401,15 +410,15 @@ def get_fields(
401
410
raise TypeError ("cls must a dataclasses or attr class" )
402
411
403
412
404
- # TypeVar to allow attr_from to be used with either an attr class or a dataclasses class
405
- AttrFromType = TypeVar ( "AttrFromType" )
413
+ _AttrFromType = TypeVar ( "_AttrFromType" )
414
+ """TypeVar to allow attr_from to be used with either an attr class or a dataclasses class"""
406
415
407
416
408
417
def attr_from (
409
- cls : Type [AttrFromType ],
418
+ cls : Type [_AttrFromType ],
410
419
kwargs : Dict [str , str ],
411
420
parsers : Optional [Dict [type , Callable [[str ], Any ]]] = None ,
412
- ) -> AttrFromType :
421
+ ) -> _AttrFromType :
413
422
"""Builds an attr or dataclasses class from key-word arguments
414
423
415
424
Args:
@@ -455,7 +464,7 @@ def attr_from(
455
464
set_value
456
465
), f"Do not know how to convert string to { attribute .type } for value: { str_value } "
457
466
else : # no value, check for a default
458
- assert attribute .default is not None or attribute_is_optional (
467
+ assert attribute .default is not None or _attribute_is_optional (
459
468
attribute
460
469
), f"No value given and no default for attribute `{ attribute .name } `"
461
470
return_value = attribute .default
@@ -468,13 +477,13 @@ def attr_from(
468
477
return cls (** return_values )
469
478
470
479
471
- def attribute_is_optional (attribute : FieldType ) -> bool :
480
+ def _attribute_is_optional (attribute : FieldType ) -> bool :
472
481
"""Returns True if the attribute is optional, False otherwise"""
473
482
return typing .get_origin (attribute .type ) is Union and isinstance (
474
483
None , typing .get_args (attribute .type )
475
484
)
476
485
477
486
478
- def attribute_has_default (attribute : FieldType ) -> bool :
487
+ def _attribute_has_default (attribute : FieldType ) -> bool :
479
488
"""Returns True if the attribute has a default value, False otherwise"""
480
- return attribute .default not in MISSING_OR_NONE or attribute_is_optional (attribute )
489
+ return attribute .default not in _MISSING_OR_NONE or _attribute_is_optional (attribute )
0 commit comments