Skip to content

Assignment through def does not satisfy ABC's Optional Callable with args #13194

Closed
@r1chardj0n3s

Description

@r1chardj0n3s

It is not possible to satisfy an ABC that has an Optional Callable with args defined using a "def" in the concrete class.

A Callable which is not optional may be defined.

An Optional Callable with no arguments may also be defined.

Reproduction of the problem using mypy version 0.961 in the mypy playground.

The cases Two and Three below should be accepted as suitable implementations of check, and they are not.

import abc
from typing import Callable, Optional

# optional callable with an argument define
class OptCallArgs(abc.ABC):
    check: Optional[Callable[[int], None]]

# assignment of a callable is fine
class One(OptCallArgs):
    check = lambda value: None

# "def" of a suitable callable is not handled
class Two(OptCallArgs):
    @staticmethod
    def check(value: int): ...

class Three(OptCallArgs):
    def check(self, value: int): ...

# callable with args has no issue
class CallArgs(abc.ABC):
    check: Callable[[int], None]

class Four(CallArgs):
    def check(self, value: int): ...

# optional callable does not have an issue if there are no args
class OptCall(abc.ABC):
    check: Optional[Callable]

class Five(OptCall):
    def check(self): ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions