Thanks to visit codestin.com
Credit goes to github.com

Skip to content

mypy incorrectly marks a @staticmethod definition of an attribute A to be incompatible with A's signature in a parent class. #12569

Description

@pawelrubin

Bug Report
When a class C defines an attribute a to be a union type with Callable as one of its types (a: Callable | ...), mypy incorrectly marks a @staticmethod definition of a in a child class that inherits from C to be incompatible with a's signature in C.

Worth noting that the following code produces no errors:

from typing import Callable


class Parent:
    foo: Callable[[str], str]

class Child2(Parent):
    @staticmethod
    def foo(x: str) -> str:
        return x.lower()

but the following code does

from typing import Callable


class Parent:
    foo: Callable[[str], str] | str 


class Child1(Parent):
    foo = "bar"  # OK


class Child2(Parent):
    @staticmethod
    def foo(x: str) -> str:  # Signature of "foo" incompatible with supertype "Parent"
        return x.lower()

Expected Behavior
No errors.

Actual Behavior

example.py: note: In class "Child2":
example.py:14: error: Signature of "foo" incompatible with supertype "Parent"  [override]
Found 1 error in 1 file (checked 1 source file)

Environment

  • Mypy version used: 0.941
  • Mypy command-line flags: --strict, --show-error-codes, --show-error-context
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.3
  • Operating system and version: MacOS 12.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-callsFunction calls, *args, **kwargs, defaultstopic-descriptorsProperties, class vs. instance attributes

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions