Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the type hint for a method?

What is the right type hint for a method? There is typing.Callable, but I’m looking for a method type hint, and typing.Callable[[Self, ...], ...] is not working.

I’ve tried this but it doesn’t work:

class _Object:
    """An empty object for method type."""

    def method(self) -> None:
        """
        A method.

        :return None: Nothing.
        """
        return None


MethodType: Type = type(_Object().method)

MethodType is a type and not a type alias. Which type should I use then? I’m using Python 3.11.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

types.MethodType would be the annotation to use for "specifically a method object", but most of the time, typing.Callable will be more useful.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading