2021-12-27 08:29:09 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2017-09-17 14:22:49 +00:00
|
|
|
"""
|
|
|
|
Tests for PEP-526 type annotations.
|
|
|
|
"""
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
import sys
|
2017-11-08 10:15:21 +00:00
|
|
|
import types
|
2017-10-26 10:55:34 +00:00
|
|
|
import typing
|
|
|
|
|
2017-09-17 14:22:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-10-02 17:32:10 +00:00
|
|
|
import attr
|
2017-09-17 14:22:49 +00:00
|
|
|
|
2024-08-06 11:37:35 +00:00
|
|
|
from attr._compat import PY_3_14_PLUS
|
2021-02-22 11:56:44 +00:00
|
|
|
from attr._make import _is_class_var
|
2017-11-08 10:15:21 +00:00
|
|
|
from attr.exceptions import UnannotatedAttributeError
|
|
|
|
|
2017-09-17 14:22:49 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
def assert_init_annotations(cls, **annotations):
|
|
|
|
"""
|
|
|
|
Assert cls.__init__ has the correct annotations.
|
|
|
|
"""
|
|
|
|
__tracebackhide__ = True
|
|
|
|
|
|
|
|
annotations["return"] = type(None)
|
|
|
|
|
|
|
|
assert annotations == typing.get_type_hints(cls.__init__)
|
|
|
|
|
|
|
|
|
2017-10-06 12:38:23 +00:00
|
|
|
class TestAnnotations:
|
2017-09-17 14:22:49 +00:00
|
|
|
"""
|
|
|
|
Tests for types derived from variable annotations (PEP-526).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_basic_annotations(self):
|
|
|
|
"""
|
|
|
|
Sets the `Attribute.type` attr from basic type annotations.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-10-02 17:32:10 +00:00
|
|
|
@attr.s
|
2017-10-06 12:38:23 +00:00
|
|
|
class C:
|
2017-10-02 17:32:10 +00:00
|
|
|
x: int = attr.ib()
|
|
|
|
y = attr.ib(type=str)
|
|
|
|
z = attr.ib()
|
|
|
|
|
|
|
|
assert int is attr.fields(C).x.type
|
|
|
|
assert str is attr.fields(C).y.type
|
|
|
|
assert None is attr.fields(C).z.type
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(C, x=int, y=str)
|
2017-09-17 14:22:49 +00:00
|
|
|
|
|
|
|
def test_catches_basic_type_conflict(self):
|
|
|
|
"""
|
2017-10-06 12:38:23 +00:00
|
|
|
Raises ValueError if type is specified both ways.
|
2017-09-17 14:22:49 +00:00
|
|
|
"""
|
|
|
|
with pytest.raises(ValueError) as e:
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2017-10-02 17:32:10 +00:00
|
|
|
@attr.s
|
2017-09-17 14:22:49 +00:00
|
|
|
class C:
|
2017-10-02 17:32:10 +00:00
|
|
|
x: int = attr.ib(type=int)
|
|
|
|
|
2017-10-06 12:38:23 +00:00
|
|
|
assert (
|
|
|
|
"Type annotation and type argument cannot both be present",
|
|
|
|
) == e.value.args
|
2017-09-17 14:22:49 +00:00
|
|
|
|
|
|
|
def test_typing_annotations(self):
|
|
|
|
"""
|
|
|
|
Sets the `Attribute.type` attr from typing annotations.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-10-02 17:32:10 +00:00
|
|
|
@attr.s
|
2017-10-06 12:38:23 +00:00
|
|
|
class C:
|
2017-10-02 17:32:10 +00:00
|
|
|
x: typing.List[int] = attr.ib()
|
|
|
|
y = attr.ib(type=typing.Optional[str])
|
2017-09-17 14:22:49 +00:00
|
|
|
|
2017-10-02 17:32:10 +00:00
|
|
|
assert typing.List[int] is attr.fields(C).x.type
|
|
|
|
assert typing.Optional[str] is attr.fields(C).y.type
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(C, x=typing.List[int], y=typing.Optional[str])
|
2017-10-06 12:38:23 +00:00
|
|
|
|
|
|
|
def test_only_attrs_annotations_collected(self):
|
|
|
|
"""
|
|
|
|
Annotations that aren't set to an attr.ib are ignored.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-10-06 12:38:23 +00:00
|
|
|
@attr.s
|
|
|
|
class C:
|
|
|
|
x: typing.List[int] = attr.ib()
|
|
|
|
y: int
|
|
|
|
|
|
|
|
assert 1 == len(attr.fields(C))
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(C, x=typing.List[int])
|
2017-11-08 10:15:21 +00:00
|
|
|
|
2022-06-24 09:37:40 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[:2] < (3, 11),
|
|
|
|
reason="Incompatible behavior on older Pythons",
|
|
|
|
)
|
2017-11-08 10:15:21 +00:00
|
|
|
def test_auto_attribs(self, slots):
|
|
|
|
"""
|
|
|
|
If *auto_attribs* is True, bare annotations are collected too.
|
|
|
|
Defaults work and class variables are ignored.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2017-11-08 10:15:21 +00:00
|
|
|
@attr.s(auto_attribs=True, slots=slots)
|
|
|
|
class C:
|
|
|
|
cls_var: typing.ClassVar[int] = 23
|
|
|
|
a: int
|
|
|
|
x: typing.List[int] = attr.Factory(list)
|
|
|
|
y: int = 2
|
|
|
|
z: int = attr.ib(default=3)
|
|
|
|
foo: typing.Any = None
|
|
|
|
|
|
|
|
i = C(42)
|
|
|
|
assert "C(a=42, x=[], y=2, z=3, foo=None)" == repr(i)
|
|
|
|
|
2022-03-21 07:47:47 +00:00
|
|
|
attr_names = {a.name for a in C.__attrs_attrs__}
|
2017-11-08 10:15:21 +00:00
|
|
|
assert "a" in attr_names # just double check that the set works
|
|
|
|
assert "cls_var" not in attr_names
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
attr.resolve_types(C)
|
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert int is attr.fields(C).a.type
|
2017-11-08 10:15:21 +00:00
|
|
|
|
|
|
|
assert attr.Factory(list) == attr.fields(C).x.default
|
2024-06-28 07:41:56 +00:00
|
|
|
assert typing.List[int] is attr.fields(C).x.type
|
2017-11-08 10:15:21 +00:00
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert int is attr.fields(C).y.type
|
2017-11-08 10:15:21 +00:00
|
|
|
assert 2 == attr.fields(C).y.default
|
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert int is attr.fields(C).z.type
|
2017-11-08 10:15:21 +00:00
|
|
|
|
|
|
|
assert typing.Any == attr.fields(C).foo.type
|
|
|
|
|
|
|
|
# Class body is clean.
|
|
|
|
if slots is False:
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
C.y
|
|
|
|
|
|
|
|
assert 2 == i.y
|
|
|
|
else:
|
|
|
|
assert isinstance(C.y, types.MemberDescriptorType)
|
|
|
|
|
|
|
|
i.y = 23
|
|
|
|
assert 23 == i.y
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(
|
|
|
|
C,
|
|
|
|
a=int,
|
|
|
|
x=typing.List[int],
|
|
|
|
y=int,
|
|
|
|
z=int,
|
2022-06-24 09:37:40 +00:00
|
|
|
foo=typing.Any,
|
2021-02-22 11:56:44 +00:00
|
|
|
)
|
2018-04-06 08:36:29 +00:00
|
|
|
|
2017-11-08 10:15:21 +00:00
|
|
|
def test_auto_attribs_unannotated(self, slots):
|
|
|
|
"""
|
|
|
|
Unannotated `attr.ib`s raise an error.
|
|
|
|
"""
|
|
|
|
with pytest.raises(UnannotatedAttributeError) as e:
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2017-11-08 10:15:21 +00:00
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class C:
|
|
|
|
v = attr.ib()
|
|
|
|
x: int
|
|
|
|
y = attr.ib()
|
|
|
|
z: str
|
|
|
|
|
|
|
|
assert (
|
|
|
|
"The following `attr.ib`s lack a type annotation: v, y.",
|
|
|
|
) == e.value.args
|
2017-11-14 05:27:49 +00:00
|
|
|
|
|
|
|
def test_auto_attribs_subclassing(self, slots):
|
|
|
|
"""
|
2018-08-29 16:52:47 +00:00
|
|
|
Attributes from base classes are inherited, it doesn't matter if the
|
2017-11-14 05:27:49 +00:00
|
|
|
subclass has annotations or not.
|
|
|
|
|
|
|
|
Ref #291
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-11-14 05:27:49 +00:00
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class A:
|
|
|
|
a: int = 1
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-11-14 05:27:49 +00:00
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class B(A):
|
|
|
|
b: int = 2
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
@attr.resolve_types
|
2017-11-14 05:27:49 +00:00
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class C(A):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert "B(a=1, b=2)" == repr(B())
|
|
|
|
assert "C(a=1)" == repr(C())
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(A, a=int)
|
|
|
|
assert_init_annotations(B, a=int, b=int)
|
|
|
|
assert_init_annotations(C, a=int)
|
2018-04-06 08:36:29 +00:00
|
|
|
|
|
|
|
def test_converter_annotations(self):
|
|
|
|
"""
|
2020-12-13 15:30:06 +00:00
|
|
|
An unannotated attribute with an annotated converter gets its
|
|
|
|
annotation from the converter.
|
2018-04-06 08:36:29 +00:00
|
|
|
"""
|
|
|
|
|
2020-12-13 15:30:06 +00:00
|
|
|
def int2str(x: int) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class A:
|
|
|
|
a = attr.ib(converter=int2str)
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(A, a=int)
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def int2str_(x: int, y: str = ""):
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class A:
|
|
|
|
a = attr.ib(converter=int2str_)
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(A, a=int)
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def test_converter_attrib_annotations(self):
|
|
|
|
"""
|
|
|
|
If a converter is provided, an explicit type annotation has no
|
|
|
|
effect on an attribute's type annotation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def int2str(x: int) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class A:
|
|
|
|
a: str = attr.ib(converter=int2str)
|
|
|
|
b = attr.ib(converter=int2str, type=str)
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(A, a=int, b=int)
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def test_non_introspectable_converter(self):
|
|
|
|
"""
|
|
|
|
A non-introspectable converter doesn't cause a crash.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class A:
|
|
|
|
a = attr.ib(converter=print)
|
|
|
|
|
|
|
|
def test_nullary_converter(self):
|
|
|
|
"""
|
2022-07-03 04:52:29 +00:00
|
|
|
A converter with no arguments doesn't cause a crash.
|
2020-12-13 15:30:06 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def noop():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@attr.s
|
2018-04-06 08:36:29 +00:00
|
|
|
class A:
|
2020-12-13 15:30:06 +00:00
|
|
|
a = attr.ib(converter=noop)
|
2018-04-06 08:36:29 +00:00
|
|
|
|
2018-06-10 17:40:07 +00:00
|
|
|
assert A.__init__.__annotations__ == {"return": None}
|
2018-04-06 08:36:29 +00:00
|
|
|
|
2020-12-13 15:30:06 +00:00
|
|
|
def test_pipe(self):
|
|
|
|
"""
|
|
|
|
pipe() uses the input annotation of its first argument and the
|
|
|
|
output annotation of its last argument.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def int2str(x: int) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
def strlen(y: str) -> int:
|
|
|
|
return len(y)
|
|
|
|
|
|
|
|
def identity(z):
|
|
|
|
return z
|
|
|
|
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(int2str).converter.__annotations__ == {
|
2020-12-13 15:30:06 +00:00
|
|
|
"val": int,
|
|
|
|
"return": str,
|
|
|
|
}
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(
|
|
|
|
int2str, strlen
|
|
|
|
).converter.__annotations__ == {
|
2020-12-13 15:30:06 +00:00
|
|
|
"val": int,
|
|
|
|
"return": int,
|
|
|
|
}
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(
|
|
|
|
identity, strlen
|
|
|
|
).converter.__annotations__ == {"return": int}
|
|
|
|
assert attr.converters.pipe(
|
|
|
|
int2str, identity
|
|
|
|
).converter.__annotations__ == {"val": int}
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def int2str_(x: int, y: int = 0) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(int2str_).converter.__annotations__ == {
|
2020-12-13 15:30:06 +00:00
|
|
|
"val": int,
|
|
|
|
"return": str,
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_pipe_empty(self):
|
|
|
|
"""
|
|
|
|
pipe() with no converters is annotated like the identity.
|
|
|
|
"""
|
|
|
|
|
|
|
|
p = attr.converters.pipe()
|
2024-07-28 13:55:24 +00:00
|
|
|
|
|
|
|
assert "val" in p.converter.__annotations__
|
|
|
|
|
|
|
|
t = p.converter.__annotations__["val"]
|
|
|
|
|
2020-12-13 15:30:06 +00:00
|
|
|
assert isinstance(t, typing.TypeVar)
|
2024-07-28 13:55:24 +00:00
|
|
|
assert p.converter.__annotations__ == {"val": t, "return": t}
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def test_pipe_non_introspectable(self):
|
|
|
|
"""
|
|
|
|
pipe() doesn't crash when passed a non-introspectable converter.
|
|
|
|
"""
|
|
|
|
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(print).converter.__annotations__ == {}
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def test_pipe_nullary(self):
|
|
|
|
"""
|
|
|
|
pipe() doesn't crash when passed a nullary converter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def noop():
|
|
|
|
pass
|
|
|
|
|
2024-07-28 13:55:24 +00:00
|
|
|
assert attr.converters.pipe(noop).converter.__annotations__ == {}
|
2020-12-13 15:30:06 +00:00
|
|
|
|
|
|
|
def test_optional(self):
|
|
|
|
"""
|
|
|
|
optional() uses the annotations of the converter it wraps.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def int2str(x: int) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
def int_identity(x: int):
|
|
|
|
return x
|
|
|
|
|
|
|
|
def strify(x) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
def identity(x):
|
|
|
|
return x
|
|
|
|
|
|
|
|
assert attr.converters.optional(int2str).__annotations__ == {
|
|
|
|
"val": typing.Optional[int],
|
|
|
|
"return": typing.Optional[str],
|
|
|
|
}
|
|
|
|
assert attr.converters.optional(int_identity).__annotations__ == {
|
|
|
|
"val": typing.Optional[int]
|
|
|
|
}
|
|
|
|
assert attr.converters.optional(strify).__annotations__ == {
|
|
|
|
"return": typing.Optional[str]
|
|
|
|
}
|
|
|
|
assert attr.converters.optional(identity).__annotations__ == {}
|
|
|
|
|
|
|
|
def int2str_(x: int, y: int = 0) -> str:
|
|
|
|
return str(x)
|
|
|
|
|
|
|
|
assert attr.converters.optional(int2str_).__annotations__ == {
|
|
|
|
"val": typing.Optional[int],
|
|
|
|
"return": typing.Optional[str],
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_optional_non_introspectable(self):
|
|
|
|
"""
|
|
|
|
optional() doesn't crash when passed a non-introspectable
|
|
|
|
converter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
assert attr.converters.optional(print).__annotations__ == {}
|
|
|
|
|
|
|
|
def test_optional_nullary(self):
|
|
|
|
"""
|
|
|
|
optional() doesn't crash when passed a nullary converter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def noop():
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert attr.converters.optional(noop).__annotations__ == {}
|
|
|
|
|
2022-06-24 09:37:40 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[:2] < (3, 11),
|
|
|
|
reason="Incompatible behavior on older Pythons",
|
2021-02-22 11:56:44 +00:00
|
|
|
)
|
|
|
|
def test_annotations_strings(self, slots):
|
2018-04-06 08:36:29 +00:00
|
|
|
"""
|
|
|
|
String annotations are passed into __init__ as is.
|
2021-02-22 11:56:44 +00:00
|
|
|
|
2022-08-05 07:48:00 +00:00
|
|
|
The strings keep changing between releases.
|
2018-04-06 08:36:29 +00:00
|
|
|
"""
|
2021-02-22 11:56:44 +00:00
|
|
|
import typing as t
|
|
|
|
|
|
|
|
from typing import ClassVar
|
2018-06-10 17:40:07 +00:00
|
|
|
|
2018-04-06 08:36:29 +00:00
|
|
|
@attr.s(auto_attribs=True, slots=slots)
|
|
|
|
class C:
|
2021-02-22 11:56:44 +00:00
|
|
|
cls_var1: "typing.ClassVar[int]" = 23
|
|
|
|
cls_var2: "ClassVar[int]" = 23
|
|
|
|
cls_var3: "t.ClassVar[int]" = 23
|
2018-06-10 17:40:07 +00:00
|
|
|
a: "int"
|
|
|
|
x: "typing.List[int]" = attr.Factory(list)
|
|
|
|
y: "int" = 2
|
|
|
|
z: "int" = attr.ib(default=3)
|
|
|
|
foo: "typing.Any" = None
|
2018-04-06 08:36:29 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
attr.resolve_types(C, locals(), globals())
|
|
|
|
|
|
|
|
assert_init_annotations(
|
|
|
|
C,
|
|
|
|
a=int,
|
|
|
|
x=typing.List[int],
|
|
|
|
y=int,
|
|
|
|
z=int,
|
2022-06-24 09:37:40 +00:00
|
|
|
foo=typing.Any,
|
2021-02-22 11:56:44 +00:00
|
|
|
)
|
2018-08-11 04:40:01 +00:00
|
|
|
|
2021-03-20 17:02:59 +00:00
|
|
|
def test_typing_extensions_classvar(self, slots):
|
|
|
|
"""
|
|
|
|
If ClassVar is coming from typing_extensions, it is recognized too.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True, slots=slots)
|
|
|
|
class C:
|
2023-08-20 10:03:53 +00:00
|
|
|
cls_var: "typing_extensions.ClassVar" = 23 # noqa: F821
|
2021-03-20 17:02:59 +00:00
|
|
|
|
|
|
|
assert_init_annotations(C)
|
|
|
|
|
2018-08-11 04:40:01 +00:00
|
|
|
def test_keyword_only_auto_attribs(self):
|
|
|
|
"""
|
|
|
|
`kw_only` propagates to attributes defined via `auto_attribs`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True, kw_only=True)
|
|
|
|
class C:
|
|
|
|
x: int
|
|
|
|
y: int
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
C(0, 1)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
C(x=0)
|
|
|
|
|
|
|
|
c = C(x=0, y=1)
|
|
|
|
|
|
|
|
assert c.x == 0
|
|
|
|
assert c.y == 1
|
2018-08-27 11:33:40 +00:00
|
|
|
|
2018-08-29 16:52:47 +00:00
|
|
|
def test_base_class_variable(self):
|
2018-08-27 11:33:40 +00:00
|
|
|
"""
|
2018-08-29 16:52:47 +00:00
|
|
|
Base class' class variables can be overridden with an attribute
|
2018-08-27 11:33:40 +00:00
|
|
|
without resorting to using an explicit `attr.ib()`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Base:
|
|
|
|
x: int = 42
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
|
|
class C(Base):
|
|
|
|
x: int
|
|
|
|
|
|
|
|
assert 1 == C(1).x
|
2019-07-23 11:19:10 +00:00
|
|
|
|
|
|
|
def test_removes_none_too(self):
|
|
|
|
"""
|
|
|
|
Regression test for #523: make sure defaults that are set to None are
|
|
|
|
removed too.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
|
|
class C:
|
|
|
|
x: int = 42
|
|
|
|
y: typing.Any = None
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
C.x
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
C.y
|
2019-10-15 04:56:12 +00:00
|
|
|
|
|
|
|
def test_non_comparable_defaults(self):
|
|
|
|
"""
|
|
|
|
Regression test for #585: objects that are not directly comparable
|
|
|
|
(for example numpy arrays) would cause a crash when used as
|
|
|
|
default values of an attrs auto-attrib class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class NonComparable:
|
|
|
|
def __eq__(self, other):
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
|
|
class C:
|
|
|
|
x: typing.Any = NonComparable()
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
def test_basic_resolve(self):
|
|
|
|
"""
|
|
|
|
Resolve the `Attribute.type` attr from basic type annotations.
|
|
|
|
Unannotated types are ignored.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class C:
|
|
|
|
x: "int" = attr.ib()
|
|
|
|
y = attr.ib(type=str)
|
|
|
|
z = attr.ib()
|
|
|
|
|
|
|
|
attr.resolve_types(C)
|
|
|
|
|
|
|
|
assert int is attr.fields(C).x.type
|
|
|
|
assert str is attr.fields(C).y.type
|
|
|
|
assert None is attr.fields(C).z.type
|
|
|
|
|
2023-03-18 18:31:15 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[:2] < (3, 9),
|
|
|
|
reason="Incompatible behavior on older Pythons",
|
|
|
|
)
|
|
|
|
def test_extra_resolve(self):
|
|
|
|
"""
|
|
|
|
`get_type_hints` returns extra type hints.
|
|
|
|
"""
|
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
globals = {"Annotated": Annotated}
|
|
|
|
|
|
|
|
@attr.define
|
|
|
|
class C:
|
|
|
|
x: 'Annotated[float, "test"]'
|
|
|
|
|
|
|
|
attr.resolve_types(C, globals)
|
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert Annotated[float, "test"] is attr.fields(C).x.type
|
2023-03-18 18:31:15 +00:00
|
|
|
|
|
|
|
@attr.define
|
|
|
|
class D:
|
|
|
|
x: 'Annotated[float, "test"]'
|
|
|
|
|
|
|
|
attr.resolve_types(D, globals, include_extras=False)
|
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert float is attr.fields(D).x.type
|
2023-03-18 18:31:15 +00:00
|
|
|
|
2020-07-22 09:43:07 +00:00
|
|
|
def test_resolve_types_auto_attrib(self, slots):
|
|
|
|
"""
|
|
|
|
Types can be resolved even when strings are involved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class A:
|
|
|
|
a: typing.List[int]
|
|
|
|
b: typing.List["int"]
|
|
|
|
c: "typing.List[int]"
|
|
|
|
|
|
|
|
# Note: I don't have to pass globals and locals here because
|
|
|
|
# int is a builtin and will be available in any scope.
|
|
|
|
attr.resolve_types(A)
|
|
|
|
|
|
|
|
assert typing.List[int] == attr.fields(A).a.type
|
|
|
|
assert typing.List[int] == attr.fields(A).b.type
|
|
|
|
assert typing.List[int] == attr.fields(A).c.type
|
|
|
|
|
|
|
|
def test_resolve_types_decorator(self, slots):
|
|
|
|
"""
|
|
|
|
Types can be resolved using it as a decorator.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.resolve_types
|
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class A:
|
|
|
|
a: typing.List[int]
|
|
|
|
b: typing.List["int"]
|
|
|
|
c: "typing.List[int]"
|
|
|
|
|
|
|
|
assert typing.List[int] == attr.fields(A).a.type
|
|
|
|
assert typing.List[int] == attr.fields(A).b.type
|
|
|
|
assert typing.List[int] == attr.fields(A).c.type
|
|
|
|
|
|
|
|
def test_self_reference(self, slots):
|
|
|
|
"""
|
|
|
|
References to self class using quotes can be resolved.
|
|
|
|
"""
|
2024-08-06 11:37:35 +00:00
|
|
|
if PY_3_14_PLUS and not slots:
|
|
|
|
pytest.xfail("References are changing a lot in 3.14.")
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class A:
|
|
|
|
a: "A"
|
2023-08-20 10:03:53 +00:00
|
|
|
b: typing.Optional["A"] # will resolve below -- noqa: F821
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
attr.resolve_types(A, globals(), locals())
|
|
|
|
|
|
|
|
assert A == attr.fields(A).a.type
|
|
|
|
assert typing.Optional[A] == attr.fields(A).b.type
|
|
|
|
|
|
|
|
def test_forward_reference(self, slots):
|
|
|
|
"""
|
|
|
|
Forward references can be resolved.
|
|
|
|
"""
|
2024-08-06 11:37:35 +00:00
|
|
|
if PY_3_14_PLUS and not slots:
|
|
|
|
pytest.xfail("Forward references are changing a lot in 3.14.")
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class A:
|
2023-08-20 10:03:53 +00:00
|
|
|
a: typing.List["B"] # will resolve below -- noqa: F821
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
@attr.s(slots=slots, auto_attribs=True)
|
|
|
|
class B:
|
|
|
|
a: A
|
|
|
|
|
|
|
|
attr.resolve_types(A, globals(), locals())
|
2021-02-22 11:56:44 +00:00
|
|
|
attr.resolve_types(B, globals(), locals())
|
|
|
|
|
|
|
|
assert typing.List[B] == attr.fields(A).a.type
|
|
|
|
assert A == attr.fields(B).a.type
|
2020-07-22 09:43:07 +00:00
|
|
|
|
|
|
|
assert typing.List[B] == attr.fields(A).a.type
|
|
|
|
assert A == attr.fields(B).a.type
|
2021-02-19 05:50:41 +00:00
|
|
|
|
|
|
|
def test_init_type_hints(self):
|
|
|
|
"""
|
|
|
|
Forward references in __init__ can be automatically resolved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class C:
|
|
|
|
x = attr.ib(type="typing.List[int]")
|
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
assert_init_annotations(C, x=typing.List[int])
|
2021-02-19 05:50:41 +00:00
|
|
|
|
|
|
|
def test_init_type_hints_fake_module(self):
|
|
|
|
"""
|
|
|
|
If you somehow set the __module__ to something that doesn't exist
|
|
|
|
you'll lose __init__ resolution.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class C:
|
|
|
|
x = attr.ib(type="typing.List[int]")
|
|
|
|
|
|
|
|
C.__module__ = "totally fake"
|
|
|
|
C = attr.s(C)
|
|
|
|
|
|
|
|
with pytest.raises(NameError):
|
|
|
|
typing.get_type_hints(C.__init__)
|
2021-02-22 11:56:44 +00:00
|
|
|
|
2021-09-19 19:04:55 +00:00
|
|
|
def test_inheritance(self):
|
|
|
|
"""
|
|
|
|
Subclasses can be resolved after the parent is resolved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.define()
|
|
|
|
class A:
|
|
|
|
n: "int"
|
|
|
|
|
|
|
|
@attr.define()
|
|
|
|
class B(A):
|
|
|
|
pass
|
|
|
|
|
|
|
|
attr.resolve_types(A)
|
|
|
|
attr.resolve_types(B)
|
|
|
|
|
2024-06-28 07:41:56 +00:00
|
|
|
assert int is attr.fields(A).n.type
|
|
|
|
assert int is attr.fields(B).n.type
|
2021-09-19 19:04:55 +00:00
|
|
|
|
|
|
|
def test_resolve_twice(self):
|
|
|
|
"""
|
|
|
|
You can call resolve_types as many times as you like.
|
|
|
|
This test is here mostly for coverage.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@attr.define()
|
|
|
|
class A:
|
|
|
|
n: "int"
|
|
|
|
|
|
|
|
attr.resolve_types(A)
|
2024-06-28 07:41:56 +00:00
|
|
|
|
|
|
|
assert int is attr.fields(A).n.type
|
|
|
|
|
2021-09-19 19:04:55 +00:00
|
|
|
attr.resolve_types(A)
|
2024-06-28 07:41:56 +00:00
|
|
|
|
|
|
|
assert int is attr.fields(A).n.type
|
2021-09-19 19:04:55 +00:00
|
|
|
|
2021-02-22 11:56:44 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"annot",
|
|
|
|
[
|
|
|
|
typing.ClassVar,
|
|
|
|
"typing.ClassVar",
|
|
|
|
"'typing.ClassVar[dict]'",
|
|
|
|
"t.ClassVar[int]",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_is_class_var(annot):
|
|
|
|
"""
|
|
|
|
ClassVars are detected, even if they're a string or quoted.
|
|
|
|
"""
|
|
|
|
assert _is_class_var(annot)
|