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

rewrite NoneType to None #5

Closed
carljm opened this issue Oct 27, 2017 · 4 comments · Fixed by #75
Closed

rewrite NoneType to None #5

carljm opened this issue Oct 27, 2017 · 4 comments · Fixed by #75

Comments

@carljm
Copy link
Contributor

carljm commented Oct 27, 2017

No description provided.

@carljm carljm changed the title never generate an annotation with NoneType, only None rewrite NoneType to None Dec 1, 2017
@m1schka
Copy link

m1schka commented Dec 22, 2017

not sure if it's related but I also have applied the types and it generated NoneType as a parameter, which is unknown to standard python and not getting added to the import (the only option would be to import it from monkeytype but I actually don't want monkeytype imports in my business code).

@iamrajhans
Copy link
Contributor

@carljm can i take this one

@carljm
Copy link
Contributor Author

carljm commented Dec 22, 2017

@m1schka Yeah, that's this bug. We have

elif anno is NoneType:
but it seems it's not effective (at least not in all cases). The correct fix here is that we should never generate NoneType at all in an annotation, it should always be translated to None.

@iamrajhans Sure, thank you!

@johnarnold
Copy link
Contributor

I wrote a test case for this. It happens when types like Optional[str, NoneType] or even more complex occur.
My example:

    def test_union_nonetype_annotation(self):
        """NoneType should always be rendered as None"""
        sig = Signature.from_callable(UpdateSignatureHelper.has_annos)
        sig = update_signature_args(sig, {'a': Dict[str, Dict[str, Union[str, NoneType]]]}, has_self=False,
                                    ignore_existing_annotations=True)
        stub = FunctionStub('test', sig, FunctionKind.MODULE)
        expected = 'def test(a: Dict[str, Dict[str, Union[str, None]]], b) -> int: ...'
        assert stub.render() == expected

All the magic happens in render_annotations():
https://github.com/Instagram/MonkeyType/blob/master/monkeytype/stubs.py#L333

I'll submit a PR since this keeps biting me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants