After reading through the documentation it is still unclear to me what the type parameter of attr.ib does. Is it only for type hinting? There is no mention of how it is leveraged by the validator and converter parameters. It seems to be a mystery field with no relationship to any other feature or code.
Given the following code:
import attr
@attr.s
class Foo:
bar = attr.ib(type=str)
I would expect the following to not be valid: foo = Foo(12)
I also expect the following to not be valid: attr.validate(foo)
Since both are "valid", what value did that type parameter give me?
My expectation would be that when the type= field is provided a default isinstance() validator is applied. Additionally, it wouldn't be a stretch to assume a default converter is applied.
import attr
@attr.s
class Bar:
x = attr.ib(type=int)
One might expect: bar = Bar('12') to be valid since int('12') is valid.
After reading through the documentation it is still unclear to me what the
typeparameter ofattr.ibdoes. Is it only for type hinting? There is no mention of how it is leveraged by thevalidatorandconverterparameters. It seems to be a mystery field with no relationship to any other feature or code.Given the following code:
I would expect the following to not be valid:
foo = Foo(12)I also expect the following to not be valid:
attr.validate(foo)Since both are "valid", what value did that type parameter give me?
My expectation would be that when the
type=field is provided a defaultisinstance()validator is applied. Additionally, it wouldn't be a stretch to assume a default converter is applied.One might expect:
bar = Bar('12')to be valid sinceint('12')is valid.