Skip to content

Improve documentation of attr.ib type parameter #439

@skewty

Description

@skewty

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions