Skip to content

[Request]: Warn for None types then using Union[None, ...] #7258

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
gcoimbra opened this issue Jul 23, 2019 · 4 comments
Closed

[Request]: Warn for None types then using Union[None, ...] #7258

gcoimbra opened this issue Jul 23, 2019 · 4 comments

Comments

@gcoimbra
Copy link

I think it'll be a good idea to warn the developer to check for None types when he/she indicate that the types of the variable can be None. Like this:

def func(var: Union[None, int]) -> int:
    return (var + 1)
                 ^ - Operation could be done on a None.

Or something like that...

@JelleZijlstra
Copy link
Member

That code should already throw an error in mypy. Are you seeing that it doesn't?

@gcoimbra
Copy link
Author

gcoimbra commented Jul 24, 2019

That code should already throw an error in mypy. Are you seeing that it doesn't?

Yes. It's true, but when you do type check like this:

from typing import *
def func(var: Union[None, int]) -> int:
    if type(var) is int:
        return (var + 1)
    else:
        return 0

It still prints an error:

m.py:4: error: Unsupported operand types for + ("None" and "int")
m.py:4: note: Left operand is of type "Optional[int]"

Then this is, instead of a feature request, an bugfix?

EDIT:
fix number of lines in code with import

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 24, 2019

Mypy doesn't recognize type(x) is int as a valid type check, since var could be an instance of a subclass of int and it would evaluate to false. If you use isinstance(var, int), everything should work as expected.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 24, 2019

Created #7260 to discuss what we should do about this (if anything).

@JukkaL JukkaL closed this as completed Jul 24, 2019
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

3 participants