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

Conversation

Projects
None yet
2 participants
@hynek
Member

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

This comment has been minimized.

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

This comment has been minimized.

Member

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

This comment has been minimized.

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

This comment has been minimized.

Member

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?

@euresti

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

This comment has been minimized.

Contributor

euresti commented Jul 28, 2018

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

@hynek

This comment has been minimized.

Member

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 added some commits Jul 28, 2018

@hynek hynek merged commit afd2584 into master Jul 28, 2018

4 checks passed

codecov/patch 100% of diff hit (target 100%)
Details
codecov/project 100% (target 100%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@hynek hynek deleted the default-if-none branch Jul 28, 2018

@euresti

This comment has been minimized.

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