How do I type hint a function that returns an instance of the current class?

Matthias Urlichs

Let's say I have these classes:

class GenericCopyable:
    def copy(self) -> GenericCopyable:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
    ... # uses the parent implementation of "copy"
    def bar(self): …

def some_code(victim: CopyableFoo):
    v = victim.copy()
    v.bar()  ### I know that this works because "v" is a CopyableFoo, but mypy doesn't

The problem is that I need the return type of CopyableFoo.copy() to be CopyableFoo, not GenericCopyable.

Is that possible?

Edited: The above is sample code to illustrate the problem. Modifying some_code or CopyableFoo in some way is certainly possible in this example; in my "real" program that'd be substantially more difficult.

Axe319

You could do this.

from typing import TypeVar
# We define T as a TypeVar bound to the base class GenericCopyable
T = TypeVar('T', bound='GenericCopyable')

class GenericCopyable:
    # we return the type T of the type of self
    # Basically returning an instance of the calling
    # type's class
    def copy(self: T) -> T:
        return type(self)()

class CopyableFoo(GenericCopyable):
    pass

foo = CopyableFoo()

bar = foo.copy()
print(bar)

This looks a little clumsy because typically we don't need to annotate self, since it's implicitly a type of the class it's bound to. However, mypy seems to be ok with it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I type-hint that a Python function returns instance of any class derived from a superclass?

How do I type hint a filename in a function?

How to hint the type of a function I do not control?

How do I type hint a question that returns a zip object?

How do I type hint a method with the type of the enclosing class?

How to type hint that a function returns another function?

How to type hint a function that returns a function?

How to hint base class function that returns an instance of derived class it is called from?

Type hint that a function never returns

How do I get the type hint as a str?

How Do I Return An Instance of the Current Class as a Value within a TreeMap?

How do I do the type-hint 'automatic injection' custom class laravel

Python - how to type hint calling a function that returns a tuple?

How to provide type hint for a function that returns an Protocol subclass in Python?

How do I write a member function that returns a type that only exists in the class?

How do I use type hint for multiple classes in a dictionary but they inherit from same parent class?

How can I correctly-type a function that returns a class in TypeScript?

How do I get a class instance of generic type T?

How do I pass abstract class as parameter for instance of type?

How may I instantiate a class of type hint in python?

How can I type hint a dynamically set class attribute in a metaclass?

How can I type hint a variable that might contain a literal class

What is the type hint of a function that returns an image?

How do I type hint a variable whose value is itself a type hint?

Type for function that takes a class as argument, and returns an instance of that class

python instance function do not find type(class)

In Python 3.5, how can I specify a function as a type hint?

How to reference the type hint of a function

Type hint to return an instance of a class, where typevar is the class type