Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
duncancmt opened this issue Jan 18, 2021 · 1 comment
Labels
bug mypy got something wrong topic-error-reporting How we report errors

Comments

@duncancmt
Copy link

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
@duncancmt duncancmt added the bug mypy got something wrong label Jan 18, 2021
@AlexWaygood AlexWaygood added the topic-error-reporting How we report errors label Apr 1, 2022
@erictraut
Copy link

The bug here is not that the error is reported on the wrong line. The bug is that it's reported at all. The variance of parameter and return types should not be enforced for inner-scoped functions and lambdas.

This issue could therefore be considered a duplicate of #8191 and closed.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-error-reporting How we report errors
Projects
None yet
Development

No branches or pull requests

4 participants