Skip to content

Improve documentation of attr.ib type parameter #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skewty opened this issue Aug 29, 2018 · 1 comment
Closed

Improve documentation of attr.ib type parameter #439

skewty opened this issue Aug 29, 2018 · 1 comment

Comments

@skewty
Copy link

skewty commented Aug 29, 2018

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.

@hynek
Copy link
Member

hynek commented Aug 29, 2018

The docs do say, that it’s equivalent to an PEP 526 type annotation tho. The upcoming version has a whole section on it: http://www.attrs.org/en/latest/types.html

I suppose it would be helpful to link there from the parameter.

@hynek hynek closed this as completed in 15643ae Sep 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants