|
@@ -20,7 +20,7 @@ from typing import (
|
|
|
from . import converters as converters
|
|
|
from . import exceptions as exceptions
|
|
|
from . import filters as filters
|
|
|
-from . import setters as setters
|
|
|
+from . import setters as setters
|
|
|
from . import validators as validators
|
|
|
from ._version_info import VersionInfo
|
|
|
|
|
@@ -41,26 +41,26 @@ _C = TypeVar("_C", bound=type)
|
|
|
|
|
|
_EqOrderType = Union[bool, Callable[[Any], Any]]
|
|
|
_ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
|
|
|
-_ConverterType = Callable[[Any], Any]
|
|
|
+_ConverterType = Callable[[Any], Any]
|
|
|
_FilterType = Callable[[Attribute[_T], _T], bool]
|
|
|
_ReprType = Callable[[Any], str]
|
|
|
_ReprArgType = Union[bool, _ReprType]
|
|
|
-_OnSetAttrType = Callable[[Any, Attribute[Any], Any], Any]
|
|
|
-_OnSetAttrArgType = Union[
|
|
|
- _OnSetAttrType, List[_OnSetAttrType], setters._NoOpType
|
|
|
-]
|
|
|
+_OnSetAttrType = Callable[[Any, Attribute[Any], Any], Any]
|
|
|
+_OnSetAttrArgType = Union[
|
|
|
+ _OnSetAttrType, List[_OnSetAttrType], setters._NoOpType
|
|
|
+]
|
|
|
_FieldTransformer = Callable[[type, List[Attribute[Any]]], List[Attribute[Any]]]
|
|
|
-# 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 when passed 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 when passed in a list or tuple.
|
|
|
_ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
|
|
|
|
|
|
# _make --
|
|
|
|
|
|
NOTHING: object
|
|
|
|
|
|
-# NOTE: Factory lies about its return type to make this possible:
|
|
|
-# `x: List[int] # = Factory(list)`
|
|
|
+# NOTE: Factory lies about its return type to make this possible:
|
|
|
+# `x: List[int] # = Factory(list)`
|
|
|
# Work around mypy issue #4554 in the common case by using an overload.
|
|
|
if sys.version_info >= (3, 8):
|
|
|
from typing import Literal
|
|
@@ -112,19 +112,19 @@ class Attribute(Generic[_T]):
|
|
|
order: _EqOrderType
|
|
|
hash: Optional[bool]
|
|
|
init: bool
|
|
|
- converter: Optional[_ConverterType]
|
|
|
+ converter: Optional[_ConverterType]
|
|
|
metadata: Dict[Any, Any]
|
|
|
type: Optional[Type[_T]]
|
|
|
kw_only: bool
|
|
|
- on_setattr: _OnSetAttrType
|
|
|
+ on_setattr: _OnSetAttrType
|
|
|
|
|
|
def evolve(self, **changes: Any) -> "Attribute[Any]": ...
|
|
|
|
|
|
# NOTE: We had several choices for the annotation to use for type arg:
|
|
|
# 1) Type[_T]
|
|
|
# - Pros: Handles simple cases correctly
|
|
|
-# - Cons: Might produce less informative errors in the case of conflicting
|
|
|
-# TypeVars e.g. `attr.ib(default='bad', type=int)`
|
|
|
+# - Cons: Might produce less informative errors in the case of conflicting
|
|
|
+# TypeVars e.g. `attr.ib(default='bad', type=int)`
|
|
|
# 2) Callable[..., _T]
|
|
|
# - Pros: Better error messages than #1 for conflicting TypeVars
|
|
|
# - Cons: Terrible error messages for validator checks.
|
|
@@ -142,8 +142,8 @@ class Attribute(Generic[_T]):
|
|
|
# This makes this type of assignments possible:
|
|
|
# x: int = attr(8)
|
|
|
#
|
|
|
-# This form catches explicit None or no default but with no other arguments
|
|
|
-# returns Any.
|
|
|
+# This form catches explicit None or no default but with no other arguments
|
|
|
+# returns Any.
|
|
|
@overload
|
|
|
def attrib(
|
|
|
default: None = ...,
|
|
@@ -159,11 +159,11 @@ def attrib(
|
|
|
kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
) -> Any: ...
|
|
|
|
|
|
-# This form catches an explicit None or no default and infers the type from the
|
|
|
-# other arguments.
|
|
|
+# This form catches an explicit None or no default and infers the type from the
|
|
|
+# other arguments.
|
|
|
@overload
|
|
|
def attrib(
|
|
|
default: None = ...,
|
|
@@ -174,12 +174,12 @@ def attrib(
|
|
|
init: bool = ...,
|
|
|
metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
type: Optional[Type[_T]] = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
factory: Optional[Callable[[], _T]] = ...,
|
|
|
kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
) -> _T: ...
|
|
|
|
|
|
# This form catches an explicit default argument.
|
|
@@ -193,12 +193,12 @@ def attrib(
|
|
|
init: bool = ...,
|
|
|
metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
type: Optional[Type[_T]] = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
factory: Optional[Callable[[], _T]] = ...,
|
|
|
kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
) -> _T: ...
|
|
|
|
|
|
# This form covers type=non-Type: e.g. forward references (str), Any
|
|
@@ -212,85 +212,85 @@ def attrib(
|
|
|
init: bool = ...,
|
|
|
metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
type: object = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
factory: Optional[Callable[[], _T]] = ...,
|
|
|
kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
) -> Any: ...
|
|
|
@overload
|
|
|
-def field(
|
|
|
- *,
|
|
|
- default: None = ...,
|
|
|
- validator: None = ...,
|
|
|
- repr: _ReprArgType = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
- converter: None = ...,
|
|
|
- factory: None = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
- eq: Optional[bool] = ...,
|
|
|
- order: Optional[bool] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
-) -> Any: ...
|
|
|
-
|
|
|
-# This form catches an explicit None or no default and infers the type from the
|
|
|
-# other arguments.
|
|
|
-@overload
|
|
|
-def field(
|
|
|
- *,
|
|
|
- default: None = ...,
|
|
|
- validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
- repr: _ReprArgType = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
- factory: Optional[Callable[[], _T]] = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
+def field(
|
|
|
+ *,
|
|
|
+ default: None = ...,
|
|
|
+ validator: None = ...,
|
|
|
+ repr: _ReprArgType = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
+ converter: None = ...,
|
|
|
+ factory: None = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
+ eq: Optional[bool] = ...,
|
|
|
+ order: Optional[bool] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+) -> Any: ...
|
|
|
+
|
|
|
+# This form catches an explicit None or no default and infers the type from the
|
|
|
+# other arguments.
|
|
|
+@overload
|
|
|
+def field(
|
|
|
+ *,
|
|
|
+ default: None = ...,
|
|
|
+ validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
+ repr: _ReprArgType = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
+ factory: Optional[Callable[[], _T]] = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
-) -> _T: ...
|
|
|
-
|
|
|
-# This form catches an explicit default argument.
|
|
|
-@overload
|
|
|
-def field(
|
|
|
- *,
|
|
|
- default: _T,
|
|
|
- validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
- repr: _ReprArgType = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
- factory: Optional[Callable[[], _T]] = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+) -> _T: ...
|
|
|
+
|
|
|
+# This form catches an explicit default argument.
|
|
|
+@overload
|
|
|
+def field(
|
|
|
+ *,
|
|
|
+ default: _T,
|
|
|
+ validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
+ repr: _ReprArgType = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
+ factory: Optional[Callable[[], _T]] = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
-) -> _T: ...
|
|
|
-
|
|
|
-# This form covers type=non-Type: e.g. forward references (str), Any
|
|
|
-@overload
|
|
|
-def field(
|
|
|
- *,
|
|
|
- default: Optional[_T] = ...,
|
|
|
- validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
- repr: _ReprArgType = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
- converter: Optional[_ConverterType] = ...,
|
|
|
- factory: Optional[Callable[[], _T]] = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+) -> _T: ...
|
|
|
+
|
|
|
+# This form covers type=non-Type: e.g. forward references (str), Any
|
|
|
+@overload
|
|
|
+def field(
|
|
|
+ *,
|
|
|
+ default: Optional[_T] = ...,
|
|
|
+ validator: Optional[_ValidatorArgType[_T]] = ...,
|
|
|
+ repr: _ReprArgType = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ metadata: Optional[Mapping[Any, Any]] = ...,
|
|
|
+ converter: Optional[_ConverterType] = ...,
|
|
|
+ factory: Optional[Callable[[], _T]] = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
-) -> Any: ...
|
|
|
-@overload
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+) -> Any: ...
|
|
|
+@overload
|
|
|
@__dataclass_transform__(order_default=True, field_descriptors=(attrib, field))
|
|
|
def attrs(
|
|
|
maybe_cls: _C,
|
|
@@ -310,10 +310,10 @@ def attrs(
|
|
|
auto_exc: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- auto_detect: bool = ...,
|
|
|
+ auto_detect: bool = ...,
|
|
|
collect_by_mro: bool = ...,
|
|
|
- getstate_setstate: Optional[bool] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ getstate_setstate: Optional[bool] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
field_transformer: Optional[_FieldTransformer] = ...,
|
|
|
) -> _C: ...
|
|
|
@overload
|
|
@@ -336,64 +336,64 @@ def attrs(
|
|
|
auto_exc: bool = ...,
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
- auto_detect: bool = ...,
|
|
|
+ auto_detect: bool = ...,
|
|
|
collect_by_mro: bool = ...,
|
|
|
- getstate_setstate: Optional[bool] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ getstate_setstate: Optional[bool] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
field_transformer: Optional[_FieldTransformer] = ...,
|
|
|
) -> Callable[[_C], _C]: ...
|
|
|
-@overload
|
|
|
+@overload
|
|
|
@__dataclass_transform__(field_descriptors=(attrib, field))
|
|
|
-def define(
|
|
|
- maybe_cls: _C,
|
|
|
- *,
|
|
|
- these: Optional[Dict[str, Any]] = ...,
|
|
|
- repr: bool = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- slots: bool = ...,
|
|
|
- frozen: bool = ...,
|
|
|
- weakref_slot: bool = ...,
|
|
|
- str: bool = ...,
|
|
|
- auto_attribs: bool = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
- cache_hash: bool = ...,
|
|
|
- auto_exc: bool = ...,
|
|
|
- eq: Optional[bool] = ...,
|
|
|
- order: Optional[bool] = ...,
|
|
|
- auto_detect: bool = ...,
|
|
|
- getstate_setstate: Optional[bool] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+def define(
|
|
|
+ maybe_cls: _C,
|
|
|
+ *,
|
|
|
+ these: Optional[Dict[str, Any]] = ...,
|
|
|
+ repr: bool = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ slots: bool = ...,
|
|
|
+ frozen: bool = ...,
|
|
|
+ weakref_slot: bool = ...,
|
|
|
+ str: bool = ...,
|
|
|
+ auto_attribs: bool = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
+ cache_hash: bool = ...,
|
|
|
+ auto_exc: bool = ...,
|
|
|
+ eq: Optional[bool] = ...,
|
|
|
+ order: Optional[bool] = ...,
|
|
|
+ auto_detect: bool = ...,
|
|
|
+ getstate_setstate: Optional[bool] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
field_transformer: Optional[_FieldTransformer] = ...,
|
|
|
-) -> _C: ...
|
|
|
-@overload
|
|
|
+) -> _C: ...
|
|
|
+@overload
|
|
|
@__dataclass_transform__(field_descriptors=(attrib, field))
|
|
|
-def define(
|
|
|
- maybe_cls: None = ...,
|
|
|
- *,
|
|
|
- these: Optional[Dict[str, Any]] = ...,
|
|
|
- repr: bool = ...,
|
|
|
- hash: Optional[bool] = ...,
|
|
|
- init: bool = ...,
|
|
|
- slots: bool = ...,
|
|
|
- frozen: bool = ...,
|
|
|
- weakref_slot: bool = ...,
|
|
|
- str: bool = ...,
|
|
|
- auto_attribs: bool = ...,
|
|
|
- kw_only: bool = ...,
|
|
|
- cache_hash: bool = ...,
|
|
|
- auto_exc: bool = ...,
|
|
|
- eq: Optional[bool] = ...,
|
|
|
- order: Optional[bool] = ...,
|
|
|
- auto_detect: bool = ...,
|
|
|
- getstate_setstate: Optional[bool] = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+def define(
|
|
|
+ maybe_cls: None = ...,
|
|
|
+ *,
|
|
|
+ these: Optional[Dict[str, Any]] = ...,
|
|
|
+ repr: bool = ...,
|
|
|
+ hash: Optional[bool] = ...,
|
|
|
+ init: bool = ...,
|
|
|
+ slots: bool = ...,
|
|
|
+ frozen: bool = ...,
|
|
|
+ weakref_slot: bool = ...,
|
|
|
+ str: bool = ...,
|
|
|
+ auto_attribs: bool = ...,
|
|
|
+ kw_only: bool = ...,
|
|
|
+ cache_hash: bool = ...,
|
|
|
+ auto_exc: bool = ...,
|
|
|
+ eq: Optional[bool] = ...,
|
|
|
+ order: Optional[bool] = ...,
|
|
|
+ auto_detect: bool = ...,
|
|
|
+ getstate_setstate: Optional[bool] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
field_transformer: Optional[_FieldTransformer] = ...,
|
|
|
-) -> Callable[[_C], _C]: ...
|
|
|
+) -> Callable[[_C], _C]: ...
|
|
|
+
|
|
|
+mutable = define
|
|
|
+frozen = define # they differ only in their defaults
|
|
|
|
|
|
-mutable = define
|
|
|
-frozen = define # they differ only in their defaults
|
|
|
-
|
|
|
# TODO: add support for returning NamedTuple from the mypy plugin
|
|
|
class _Fields(Tuple[Attribute[Any], ...]):
|
|
|
def __getattr__(self, name: str) -> Attribute[Any]: ...
|
|
@@ -401,16 +401,16 @@ class _Fields(Tuple[Attribute[Any], ...]):
|
|
|
def fields(cls: type) -> _Fields: ...
|
|
|
def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
|
|
|
def validate(inst: Any) -> None: ...
|
|
|
-def resolve_types(
|
|
|
- cls: _C,
|
|
|
- globalns: Optional[Dict[str, Any]] = ...,
|
|
|
- localns: Optional[Dict[str, Any]] = ...,
|
|
|
+def resolve_types(
|
|
|
+ cls: _C,
|
|
|
+ globalns: Optional[Dict[str, Any]] = ...,
|
|
|
+ localns: Optional[Dict[str, Any]] = ...,
|
|
|
attribs: Optional[List[Attribute[Any]]] = ...,
|
|
|
-) -> _C: ...
|
|
|
+) -> _C: ...
|
|
|
|
|
|
# TODO: add support for returning a proper attrs class from the mypy plugin
|
|
|
-# we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
|
|
|
-# [attr.ib()])` is valid
|
|
|
+# we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
|
|
|
+# [attr.ib()])` is valid
|
|
|
def make_class(
|
|
|
name: str,
|
|
|
attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
|
|
@@ -431,15 +431,15 @@ def make_class(
|
|
|
eq: Optional[_EqOrderType] = ...,
|
|
|
order: Optional[_EqOrderType] = ...,
|
|
|
collect_by_mro: bool = ...,
|
|
|
- on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
+ on_setattr: Optional[_OnSetAttrArgType] = ...,
|
|
|
field_transformer: Optional[_FieldTransformer] = ...,
|
|
|
) -> type: ...
|
|
|
|
|
|
# _funcs --
|
|
|
|
|
|
# TODO: add support for returning TypedDict from the mypy plugin
|
|
|
-# FIXME: asdict/astuple do not honor their factory args. Waiting on one of
|
|
|
-# these:
|
|
|
+# FIXME: asdict/astuple do not honor their factory args. Waiting on one of
|
|
|
+# these:
|
|
|
# https://github.com/python/mypy/issues/4236
|
|
|
# https://github.com/python/typing/issues/253
|
|
|
def asdict(
|