Relax _ConverterType to work with chained converters

I guess we could add a separate type for chained converters but I don't think
that's worth it.
This commit is contained in:
Hynek Schlawack 2020-07-20 12:57:37 +02:00
parent 5885c4a412
commit a74c32bcde
3 changed files with 11 additions and 11 deletions

View File

@ -1,2 +1,4 @@
Added ``attr.converters.chain()``. Added ``attr.converters.chain()``.
The feature allows combining multiple conversion callbacks into one. The feature allows combining multiple conversion callbacks into one.
As part of this feature, we had to relax the type information for converter callables.

View File

@ -38,7 +38,7 @@ _T = TypeVar("_T")
_C = TypeVar("_C", bound=type) _C = TypeVar("_C", bound=type)
_ValidatorType = Callable[[Any, Attribute[_T], _T], Any] _ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
_ConverterType = Callable[[Any], _T] _ConverterType = Callable[[Any], Any]
_FilterType = Callable[[Attribute[_T], _T], bool] _FilterType = Callable[[Attribute[_T], _T], bool]
_ReprType = Callable[[Any], str] _ReprType = Callable[[Any], str]
_ReprArgType = Union[bool, _ReprType] _ReprArgType = Union[bool, _ReprType]
@ -76,7 +76,7 @@ class Attribute(Generic[_T]):
order: bool order: bool
hash: Optional[bool] hash: Optional[bool]
init: bool init: bool
converter: Optional[_ConverterType[_T]] converter: Optional[_ConverterType]
metadata: Dict[Any, Any] metadata: Dict[Any, Any]
type: Optional[Type[_T]] type: Optional[Type[_T]]
kw_only: bool kw_only: bool
@ -136,7 +136,7 @@ def attrib(
init: bool = ..., init: bool = ...,
metadata: Optional[Mapping[Any, Any]] = ..., metadata: Optional[Mapping[Any, Any]] = ...,
type: Optional[Type[_T]] = ..., type: Optional[Type[_T]] = ...,
converter: Optional[_ConverterType[_T]] = ..., converter: Optional[_ConverterType] = ...,
factory: Optional[Callable[[], _T]] = ..., factory: Optional[Callable[[], _T]] = ...,
kw_only: bool = ..., kw_only: bool = ...,
eq: Optional[bool] = ..., eq: Optional[bool] = ...,
@ -155,7 +155,7 @@ def attrib(
init: bool = ..., init: bool = ...,
metadata: Optional[Mapping[Any, Any]] = ..., metadata: Optional[Mapping[Any, Any]] = ...,
type: Optional[Type[_T]] = ..., type: Optional[Type[_T]] = ...,
converter: Optional[_ConverterType[_T]] = ..., converter: Optional[_ConverterType] = ...,
factory: Optional[Callable[[], _T]] = ..., factory: Optional[Callable[[], _T]] = ...,
kw_only: bool = ..., kw_only: bool = ...,
eq: Optional[bool] = ..., eq: Optional[bool] = ...,
@ -174,7 +174,7 @@ def attrib(
init: bool = ..., init: bool = ...,
metadata: Optional[Mapping[Any, Any]] = ..., metadata: Optional[Mapping[Any, Any]] = ...,
type: object = ..., type: object = ...,
converter: Optional[_ConverterType[_T]] = ..., converter: Optional[_ConverterType] = ...,
factory: Optional[Callable[[], _T]] = ..., factory: Optional[Callable[[], _T]] = ...,
kw_only: bool = ..., kw_only: bool = ...,
eq: Optional[bool] = ..., eq: Optional[bool] = ...,

View File

@ -3,11 +3,9 @@ from . import _ConverterType
_T = TypeVar("_T") _T = TypeVar("_T")
def chain(*validators: _ConverterType[_T]) -> _ConverterType[_T]: ... def chain(*validators: _ConverterType) -> _ConverterType: ...
def optional( def optional(converter: _ConverterType) -> _ConverterType: ...
converter: _ConverterType[_T],
) -> _ConverterType[Optional[_T]]: ...
@overload @overload
def default_if_none(default: _T) -> _ConverterType[_T]: ... def default_if_none(default: _T) -> _ConverterType: ...
@overload @overload
def default_if_none(*, factory: Callable[[], _T]) -> _ConverterType[_T]: ... def default_if_none(*, factory: Callable[[], _T]) -> _ConverterType: ...