2021-12-27 08:29:09 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-05-05 08:24:53 +00:00
|
|
|
import attr
|
2022-11-30 15:04:57 +00:00
|
|
|
import attrs
|
2021-05-05 08:24:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.define()
|
|
|
|
class Define:
|
|
|
|
a: str
|
|
|
|
b: int
|
|
|
|
|
|
|
|
|
2023-08-20 10:03:53 +00:00
|
|
|
reveal_type(Define.__init__) # noqa: F821
|
2021-05-05 08:24:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.define()
|
|
|
|
class DefineConverter:
|
|
|
|
with_converter: int = attr.field(converter=int)
|
|
|
|
|
|
|
|
|
2023-08-20 10:03:53 +00:00
|
|
|
reveal_type(DefineConverter.__init__) # noqa: F821
|
2021-05-05 08:24:53 +00:00
|
|
|
|
2023-06-03 10:12:03 +00:00
|
|
|
DefineConverter(with_converter=b"42")
|
|
|
|
|
2021-05-05 08:24:53 +00:00
|
|
|
|
|
|
|
@attr.frozen()
|
|
|
|
class Frozen:
|
|
|
|
a: str
|
|
|
|
|
|
|
|
|
|
|
|
d = Frozen("a")
|
|
|
|
d.a = "new"
|
|
|
|
|
2023-08-20 10:03:53 +00:00
|
|
|
reveal_type(d.a) # noqa: F821
|
2021-05-05 08:24:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.define(frozen=True)
|
|
|
|
class FrozenDefine:
|
|
|
|
a: str
|
|
|
|
|
|
|
|
|
|
|
|
d2 = FrozenDefine("a")
|
|
|
|
d2.a = "new"
|
|
|
|
|
2023-08-20 10:03:53 +00:00
|
|
|
reveal_type(d2.a) # noqa: F821
|
2022-11-30 15:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Field-aliasing works
|
|
|
|
@attrs.define
|
|
|
|
class AliasedField:
|
|
|
|
_a: int = attrs.field(alias="_a")
|
|
|
|
|
|
|
|
|
|
|
|
af = AliasedField(42)
|
|
|
|
|
2023-08-20 10:03:53 +00:00
|
|
|
reveal_type(af.__init__) # noqa: F821
|
2022-12-02 06:45:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
# unsafe_hash is accepted
|
|
|
|
@attrs.define(unsafe_hash=True)
|
|
|
|
class Hashable:
|
|
|
|
pass
|