Skip to content

Error in subclass of generic base at None #1956

@gnprice

Description

@gnprice

Under --strict-optional, when inheriting from a generic base class applied to None, method implementations on the subclass can produce spurious errors like so:

$ cat /tmp/foo.py
from typing import Generic, TypeVar

T = TypeVar('T')

class Base(Generic[T]):
  def f(self) -> T:
    pass

class SubNone(Base[None]):
  def f(self) -> None:
    pass

class SubInt(Base[int]):
  def f(self) -> int:
    return 1

$ mypy /tmp/foo.py --strict-optional
/tmp/foo.py: note: In class "SubNone":
/tmp/foo.py:10: error: Return type of "f" incompatible with supertype "Base"

Note there's no error when the same thing is done with int instead of None. There's also no error without --strict-optional.

This issue causes about 109 error messages when type-checking mypy itself under --strict-optional, in various subclasses of NodeVisitor[None] and TypeVisitor[None].

I haven't tried hard to reduce this repro -- it's possible that some of the elements of it are unnecessary.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions