Skip to content

Values with the type annotation of type aren't valid as types #11286

Closed

Description

@KotlinIsland
NoneType1: type[None] = type(None)
NoneType2 = type(None)

none1: NoneType1 = None  # error: Variable "NoneType1" is not valid as a type
none2: NoneType2 = None  # no error

Pyright handles this correctly.

Activity

  1. erictraut commented on Oct 7, 2021

    @erictraut

    I think this is a problem in pyright rather than mypy. In your example, NoneType1 and NoneType2 are variables and not valid type aliases, so they shouldn't be allowed as type annotations. Arguably, uses of both NoneType1 and NoneType2 within an annotation should result in an error.

    If you want to define a type alias that represents the type of the None object, it should be defined like this:

    NoneType1 = type[None]
    
    none1: NoneType1 = type(None)

    This works fine in mypy (and in pyright).

  2. KotlinIsland commented on Oct 7, 2021

    @KotlinIsland
    ContributorAuthor

    What I really want is something that can be used as a type alias for the type of None, and is also the type of None(NoneType).

  3. KotlinIsland commented on Oct 7, 2021

    @KotlinIsland
    ContributorAuthor

    Mypy should report that using NoneType sucks because of this typing mess. I'll raise that instead.

  4. KotlinIsland commented on Oct 7, 2021

    @KotlinIsland
    ContributorAuthor

    Closed in favor of #11288.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions