Mypy: How to take a Type as function argument and invoke a class method on that type

balajeerc

The issue I'm facing is best explained with the following example:

from abc import ABC, abstractclassmethod
from typing import Type


class SomeInterface(ABC):
    """Interface definition"""

    @abstractclassmethod
    def say_something(cls, stuff_to_say: str) -> None:
        """Some class method to be implemented"""


class TheImplementation(SomeInterface):
    """Implementation of above interface"""

    @classmethod
    def say_something(cls, stuff_to_say: str) -> None:
        """Implementation of class method in interface"""
        print(stuff_to_say)


def do_something_with_type(input_class_type: Type[SomeInterface]) -> None:
    """Function that takes a class type as argument to call `say_something` on it"""
    input_class_type.say_something("hi")


do_something_with_type(TheImplementation)

Note that the above code is valid Python, which executes and prints the right string "hi".

However, mypy shows the following errors:

tests/annotation_test.py:28: error: Too few arguments for "say_something" of "SomeInterface"
tests/annotation_test.py:28: error: Argument 1 to "say_something" of "SomeInterface" has incompatible type "str"; expected "SomeInterface"

What am I doing wrong here? From reading the documentation I sense that input_class_type argument to do_something_with_type needs to be annotated differently but am not sure how exactly to go about this.

user2357112 supports Monica

It looks like mypy doesn't understand abstractclassmethod. Stacking classmethod and abstractmethod instead should work:

class SomeInterface(ABC):
    @classmethod
    @abstractmethod
    def say_something(cls, stuff_to_say: str) -> None:
        raise NotImplementedError

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get mypy to accept subtype of generic type as a method argument

Mocking Method that Takes a Class<?> Type Argument with JMock

How can I get the parameterized-type class from a parameterized-type method argument?

Higher order function: "Cannot invoke 'map' with an argument list of type '((_) -> _)'"

How to make variadic template class method take function pointer as argument with type derived from function template?

Cannot invoke Method with an argument list of type in swift

How to invoke a method an the child class when argument is of type parent class in C++?

mypy: Cannot infer type argument 1 of "map"

How to restrict type of a function argument

Mypy: annotating a variable with a class type

Python - how to pass to a function argument type of a class object (typing)

How to type a function argument with a function argument type in a nested object in TypeScript?

How do I invoke function that accepts a table type argument and returns a table?

TypeScript child class: how to set argument type and return type of method defined in the parent class

MyPy type hints for a function defined with named parameters but can take **kwargs?

How to pass to class method argument of class type in python?

Indicate that function type checks in mypy

Invoke method on generic type?

How to invoke static method of type parameter in TypeScript

Cannot invoke <method> with and argument list of type '(String)'

Alamofire: Cannot invoke 'URLRequest' with an argument list of type '(Method, NSURL)'

Java Method.invoke() throwing IllegalArgumentException: argument type mismatch

Cannot invoke function with an argument list of type '()'

How to invoke a method on a private static type (or class) in another assembly in .net?

Type hint for argument of type A in method of class A

How to call a Java function in JNI with custom class type argument

Why can't a generic member function in a class implementing an interface take an argument of the type of the class (instead of the interface)?

mypy: How to declare the return type of a method returning self in a generic class?

Overriding a method, mypy throws an "incompatible with super type" error when changing return type to child class type