You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using attr.ibute with both converter and default, wrong expectations is applied on default type (with mypy == 0.670). Mypy dev say it's an issue in attr stubs.
main.py:9: error: Incompatible types in assignment (expression has type "str", variable has type "int")
main.py:9: error: Argument "converter" has incompatible type "Callable[[Any], int]"; expected "Optional[Callable[[Any], str]]"
The result is not right, because the converter is applied on default value as well.
Of course the hotfix is to convert the default value manually.
The text was updated successfully, but these errors were encountered:
@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
Example usage
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
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.
The text was updated successfully, but these errors were encountered: