Skip to content
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

Implement attr.converters.default_if_none #414

Merged
merged 5 commits into from
Jul 28, 2018
Merged

Implement attr.converters.default_if_none #414

merged 5 commits into from
Jul 28, 2018

Conversation

hynek
Copy link
Member

@hynek hynek commented Jul 28, 2018

So this is a stab at #400 which I find a reasonable usage of converters.

Typing tests fail tho and they also fail for attr.validators.optional. Am I right to suspect that this has to be fixed in mypy's plugin? Gives us a bit of a Mexican stand-off, doesn't it? It’s completely possible that I additionally got the stubs wrong so feel free to advice.

cc @euresti @chadrik

@euresti
Copy link
Contributor

euresti commented Jul 28, 2018

Yeah this is something the plugin currently doesn't support:

error: Unsupported converter, only named functions and types are currently supported

The underlying problem is that it's hard to extract the type of the function call in the mypy pipeline.

A workaround could be to write:

optional_int = optional(int)

@attr.s
class ConvCOptional:
    x: Optional[int] = attr.ib(converter=optional_int)

But that's broken also, but I think easier to fix. Now I remember, in our work code, when we started using mypy I removed all our uses of optional and just wrote our own converters.

@hynek
Copy link
Member Author

hynek commented Jul 28, 2018

I guess until mypy opens up it's plugin API (I talked to Jukka here at EuroPython and it's legit on their roadmap) we’ll have to comment stuff out, wait for mypy to release the support? Having hard-coded support is “fine” for now…


Does the signature look correct to you tho? If someone rubber-stamps it, I’ll just comment it out and we open an issue on mypy.

@euresti
Copy link
Contributor

euresti commented Jul 28, 2018

I can review the signature. As for commenting it out I'm not sure since I'm not sure when we'll fix the plugin. It might be a while.

@hynek
Copy link
Member Author

hynek commented Jul 28, 2018

Just to be clear: by special casing I mean that mypy only supports our shipped converters for now. Would that be a significant amount of work too?

Copy link
Contributor

@euresti euresti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being completely honest I wouldn't use this feature because if I passed in None somewhere I would like it to blow up, or have mypy tell me about it so that I can handle it in the caller.

But the code all looks correct and the signature is pretty much what mypy wants it to be.

@euresti
Copy link
Contributor

euresti commented Jul 28, 2018

Hmm. Maybe I could detect the converters by name and handle them specially. That might actually work.

@hynek
Copy link
Member Author

hynek commented Jul 28, 2018

Hmm. Maybe I could detect the converters by name and handle them specially. That might actually work.

Yeah, that’s what I was thinking.

@hynek hynek merged commit afd2584 into master Jul 28, 2018
@hynek hynek deleted the default-if-none branch July 28, 2018 15:03
@euresti
Copy link
Contributor

euresti commented Aug 1, 2018

FYI: python/mypy#5411 Here's one implementing optional, we'll see what the mypy folks say.

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

Successfully merging this pull request may close these issues.

2 participants