Fixes generics in mypy types (#443)
This removes errors in attrs library when using mypy setting: `disallow_any_generics = True` Fixes #441
This commit is contained in:
parent
87099af3ea
commit
a216a114fc
|
@ -23,9 +23,9 @@ from . import validators as validators
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
_C = TypeVar("_C", bound=type)
|
_C = TypeVar("_C", bound=type)
|
||||||
|
|
||||||
_ValidatorType = Callable[[Any, Attribute, _T], Any]
|
_ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
|
||||||
_ConverterType = Callable[[Any], _T]
|
_ConverterType = Callable[[Any], _T]
|
||||||
_FilterType = Callable[[Attribute, Any], bool]
|
_FilterType = Callable[[Attribute[_T], _T], bool]
|
||||||
# FIXME: in reality, if multiple validators are passed they must be in a list or tuple,
|
# FIXME: in reality, if multiple validators are passed they must be in a list or tuple,
|
||||||
# but those are invariant and so would prevent subtypes of _ValidatorType from working
|
# but those are invariant and so would prevent subtypes of _ValidatorType from working
|
||||||
# when passed in a list or tuple.
|
# when passed in a list or tuple.
|
||||||
|
@ -57,10 +57,10 @@ class Attribute(Generic[_T]):
|
||||||
metadata: Dict[Any, Any]
|
metadata: Dict[Any, Any]
|
||||||
type: Optional[Type[_T]]
|
type: Optional[Type[_T]]
|
||||||
kw_only: bool
|
kw_only: bool
|
||||||
def __lt__(self, x: Attribute) -> bool: ...
|
def __lt__(self, x: Attribute[_T]) -> bool: ...
|
||||||
def __le__(self, x: Attribute) -> bool: ...
|
def __le__(self, x: Attribute[_T]) -> bool: ...
|
||||||
def __gt__(self, x: Attribute) -> bool: ...
|
def __gt__(self, x: Attribute[_T]) -> bool: ...
|
||||||
def __ge__(self, x: Attribute) -> bool: ...
|
def __ge__(self, x: Attribute[_T]) -> bool: ...
|
||||||
|
|
||||||
# NOTE: We had several choices for the annotation to use for type arg:
|
# NOTE: We had several choices for the annotation to use for type arg:
|
||||||
# 1) Type[_T]
|
# 1) Type[_T]
|
||||||
|
@ -187,11 +187,11 @@ def attrs(
|
||||||
) -> Callable[[_C], _C]: ...
|
) -> Callable[[_C], _C]: ...
|
||||||
|
|
||||||
# TODO: add support for returning NamedTuple from the mypy plugin
|
# TODO: add support for returning NamedTuple from the mypy plugin
|
||||||
class _Fields(Tuple[Attribute, ...]):
|
class _Fields(Tuple[Attribute[Any], ...]):
|
||||||
def __getattr__(self, name: str) -> Attribute: ...
|
def __getattr__(self, name: str) -> Attribute[Any]: ...
|
||||||
|
|
||||||
def fields(cls: type) -> _Fields: ...
|
def fields(cls: type) -> _Fields: ...
|
||||||
def fields_dict(cls: type) -> Dict[str, Attribute]: ...
|
def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
|
||||||
def validate(inst: Any) -> None: ...
|
def validate(inst: Any) -> None: ...
|
||||||
|
|
||||||
# TODO: add support for returning a proper attrs class from the mypy plugin
|
# TODO: add support for returning a proper attrs class from the mypy plugin
|
||||||
|
@ -223,7 +223,7 @@ def make_class(
|
||||||
def asdict(
|
def asdict(
|
||||||
inst: Any,
|
inst: Any,
|
||||||
recurse: bool = ...,
|
recurse: bool = ...,
|
||||||
filter: Optional[_FilterType] = ...,
|
filter: Optional[_FilterType[Any]] = ...,
|
||||||
dict_factory: Type[Mapping[Any, Any]] = ...,
|
dict_factory: Type[Mapping[Any, Any]] = ...,
|
||||||
retain_collection_types: bool = ...,
|
retain_collection_types: bool = ...,
|
||||||
) -> Dict[str, Any]: ...
|
) -> Dict[str, Any]: ...
|
||||||
|
@ -232,8 +232,8 @@ def asdict(
|
||||||
def astuple(
|
def astuple(
|
||||||
inst: Any,
|
inst: Any,
|
||||||
recurse: bool = ...,
|
recurse: bool = ...,
|
||||||
filter: Optional[_FilterType] = ...,
|
filter: Optional[_FilterType[Any]] = ...,
|
||||||
tuple_factory: Type[Sequence] = ...,
|
tuple_factory: Type[Sequence[Any]] = ...,
|
||||||
retain_collection_types: bool = ...,
|
retain_collection_types: bool = ...,
|
||||||
) -> Tuple[Any, ...]: ...
|
) -> Tuple[Any, ...]: ...
|
||||||
def has(cls: type) -> bool: ...
|
def has(cls: type) -> bool: ...
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from typing import Union
|
from typing import Union, Any
|
||||||
from . import Attribute, _FilterType
|
from . import Attribute, _FilterType
|
||||||
|
|
||||||
def include(*what: Union[type, Attribute]) -> _FilterType: ...
|
def include(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...
|
||||||
def exclude(*what: Union[type, Attribute]) -> _FilterType: ...
|
def exclude(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...
|
||||||
|
|
Loading…
Reference in New Issue