Skip to content

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

Description

@JukkaL

The following fragment generates a confusing error message:

def f(x: None) -> None:
    x.f()   # Function does not return a value

Activity

  1. ddfisher commented on Mar 7, 2016

    @ddfisher
    Collaborator

    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".

  2. added this to the milestone on Aug 4, 2016
  3. removed this from the milestone on Mar 29, 2017
  4. ddfisher commented on Jun 28, 2017

    @ddfisher
    Collaborator

    Fixed by #3024.

  5. orenbenkiki commented on Dec 4, 2017

    @orenbenkiki

    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.

  6. JukkaL commented on Dec 4, 2017

    @JukkaL
    CollaboratorAuthor

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

  7. orenbenkiki commented on Dec 4, 2017

    @orenbenkiki

    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!

  8. JukkaL commented on Dec 4, 2017

    @JukkaL
    CollaboratorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions