Search code examples
pythonmypypython-typing

Does mypy check Never type at all?


I was playing with Never type in mypy. If I have a function foo(x: int) I expected that when called with a value of type Never mypy would complain, but it silently typechecks the call:

from typing import Never


def foo(x: int):
    pass


def bar(x: Never):
    foo(x)  # ok, I exected a type error
    foo("foo")  # err

--- edit ---

Just for reference my solution to create a uninhabited type is this

from abc import ABC, abstractmethod, final

@final
class Never(ABC):
    @abstractmethod
    def __init__(self) -> None: ...

Solution

  • This is normal. Never is a subtype of every other type. After all, Never is the type with no values. All values of Never type are values of every other type, because there are no values of Never type.