Skip to content

Cannot override a class's callable variable with a method #11475

@Jesse205

Description

@Jesse205

Describe the bug

When a base class has a Callable variable without a Self parameter, overriding this variable in a subclass causes an error, which is inconsistent with the behavior of mypy and ty.

Code or Screenshots

from collections.abc import Callable
from typing import override


class A:
    hello: Callable[[], None] = lambda: print("hello")


class B(A):
    @override
    def hello(self) -> None:
        print("hi")


b = B()
b.hello()
# Output: hi
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> pyright main4.py
g:\programming\python\lab\report_callback_exception\main4.py
  g:\programming\python\lab\report_callback_exception\main4.py:11:9 - error: Method "hello" overrides class "A" in an incompatible manner
    Positional parameter count mismatch; base method has 0, but override has 1 (reportIncompatibleMethodOverride)
1 error, 0 warnings, 0 informations
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> mypy main4.py        
Success: no issues found in 1 source file
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> ty check main4.py
All checks passed!

VS Code extension or command-line

  • mypy=2.1.0
  • pyright=1.1.409
  • ty=0.0.40

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions