Skip to content

Confusing line number reported for "Cannot use a covariant type variable as a parameter" #9929

Closed as not planned
@duncancmt

Description

@duncancmt

Bug Report

MyPy reports the incorrect line number when passing a lambda into a covariant class. This is probably related to #8191 and shouldn't report an error at all.

To Reproduce

foo.py:

from typing import Callable, Generic, TypeVar

T_co = TypeVar("T_co", covariant=True)


class Foo(Generic[T_co]):
    def __init__(self, bar: Callable[[T_co], None]) -> None:
        self.bar = bar

    def good(self) -> "Foo[T_co]":
        return Foo(self.bar)

    def bad(self) -> "Foo[T_co]":  # error here
        return Foo(lambda x: None)  # not here

    def more_obvious_bad(self) -> "Foo[T_co]":
        fn: Callable[[T_co], None] = lambda x: None  # error here
        return Foo(fn)

Actual Behavior

foo.py:13: error: Cannot use a covariant type variable as a parameter
foo.py:17: error: Cannot use a covariant type variable as a parameter
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:
$ mypy --version
mypy 0.790
  • Mypy command-line flags: mypy foo.py
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used:
$ python3 --version
Python 3.9.1
  • Operating system and version:
$ xuname
Void 5.10.2_1 aarch64-musl Unknown uptodate rrFFFFF

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions