Open Funth0mas opened 5 years ago
import attr import typing
def to_int(x: typing.Any) -> int: return int(x)
@attr.s class C: a: int = attr.ib(converter=to_int, default="1") # Manually specify the default as a string a: int = attr.ib(converter=to_int, default=1) # Manually specify the default as an integer
obj1 = C() # 'a' will have a default value of 1 as an integer obj2 = C("42") # 'a' will be converted to an integer using the converter
print(obj1.a) print(obj2.a)
When using
attr.
ibute with bothconverter
anddefault
, wrong expectations is applied ondefault
type (with mypy == 0.670). Mypy dev say it's an issue in attr stubs.The valid code
results in:
The result is not right, because the
converter
is applied ondefault
value as well.Of course the hotfix is to convert the default value manually.