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

None is accepted as a type but it behaves erratically #299

Closed
JukkaL opened this issue Jul 17, 2014 · 6 comments
Closed

None is accepted as a type but it behaves erratically #299

JukkaL opened this issue Jul 17, 2014 · 6 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 17, 2014

The following fragment generates a confusing error message:

def f(x: None) -> None:
    x.f()   # Function does not return a value
@ddfisher
Copy link
Collaborator

ddfisher commented Mar 7, 2016

After we add Optional checking, maybe we can make None more consistently mean "the type containing just the value None" instead of also "the uninhabited type" and "a value you can't use".

@ddfisher
Copy link
Collaborator

Fixed by #3024.

@orenbenkiki
Copy link

In mypy version 0.501, if you write:

from typing import List
from typing import Optional
from typing import Union
from typing import Generic
from typing import TypeVar

a: List[None] = [None]
a.append(None)

b: List[Optional[None]] = [None]
b.append(None)

c: List[Union[None]] = [None]
c.append(None)

T = TypeVar('T')

class Foo(Generic[T]):
    def __init__(self, value: T) -> None:
        return

    def append(self, value: T) -> None:
        return

f: Foo[None] = Foo(None)
f.append(None)

Then mypy will complain:

example.py:7: error: List item 0 has incompatible type None
example.py:8: error: Argument 1 to "append" of "list" has incompatible type None; expected None
example.py:10: error: List item 0 has incompatible type None
example.py:11: error: Argument 1 to "append" of "list" has incompatible type None; expected None
example.py:13: error: List item 0 has incompatible type None
example.py:14: error: Argument 1 to "append" of "list" has incompatible type None; expected None
example.py:25: error: Argument 1 to "Foo" has incompatible type None; expected None
example.py:26: error: Argument 1 to "append" of "Foo" has incompatible type None; expected None

So it seems that using None as a type still doesn't behave exactly as "the type containing just the value None" - more over, it seems like there's no way to trick mypy into declaring such a type.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 4, 2017

@orenbenkiki 0.501 is pretty old. Have you a tried the latest versions? it works for me on git master.

@orenbenkiki
Copy link

Ah, it works in version 0.550. Thanks.

Version 0.501 was released in March this year, if that makes it "old.".. it is great to see mypy advancing so fast!

@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 4, 2017

Yeah, 0.501 is already old in "mypy years" :-)

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

No branches or pull requests

5 participants