Skip to content

Poor error message due to positional-or-keyword incompatibility #12013

@DanielNoord

Description

@DanielNoord

Bug Report

When using Callable to type a method instead of defining it (in my use case it is defined and assigned in another file) mypy doesn't seem to follow the same rules for child methods with and without parameters.
I asked on gitter and was told this is probably a bug. I also searched and was unable to find another report for this, sorry if this is still a duplicate.

To Reproduce

Use the following code:

from typing import Callable, Generator


class Parent:
    def method_without(self) -> "Parent":
        ...

    def method_with(self, param: str) -> "Parent":
        ...


class Child(Parent):
    method_without: Callable[["Child"], "Child"]
    method_with: Callable[["Child", str], "Child"]

Expected Behavior

Not sure. Either both typings of the child methods should be rejected or both should be accepted.

Actual Behavior

test.py:14: error: Incompatible types in assignment (expression has type "Callable[[str], Child]", base class "Parent" defined the type as "Callable[[str], Parent]") [assignment]

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
no_implicit_optional = True
scripts_are_modules = True
warn_unused_ignores = True
show_error_codes = True
  • Python version used: 3.10
  • Operating system and version: macOS

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions