Skip to content

Allow subclassing Callable #4031

Closed
Closed

Description

@ilevkivskyi

This is not a priority, but probably we could allow subclassing Callable, some people might find this "nicer", than defining a __call__ method (e.g. if this is an ABC or a protocol). For example this already works at runtime:

class C(Callable[[int], int]):
    attr: str

This would be implemented by returning CallableType for C with a fallback to an instance of the original TypeInfo (like for tulpes, see also #3831).

Activity

  1. msullivan commented on Jan 28, 2020

    @msullivan
    Collaborator

    The protocol thing works fine

  2. bfontaine commented on Feb 14, 2024

    @bfontaine

    Why was this closed? Protocols are not sufficient because they don’t inherit callable properties like __name__ or __doc__:

    from typing import Protocol
    
    class MyFunction(Protocol):
        def __call__(self, x: int) -> int: ...
    
    def print_name(fn: MyFunction):
        print(fn.__name__)

    This fails with mypy 1.8.0:

    test.py:9: error: "MyFunction" has no attribute "__name__"  [attr-defined]
    Found 1 error in 1 file (checked 1 source file)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions