kingkupps
kingkupps

Reputation: 3504

Type annotations for class parameters and return values

Assume I have the following:

from __future__ import annotations

class A(object):

    @classmethod
    def some_class_method(cls: ?, parameter: str) -> ?:
        return A

What should ? become if I want to make it clear that some_class_method is returning the class A and not an instance of class A.

EDIT: @chepner Brought up a good point in that ? likely is meant to refer to A or any subclass of A.

Upvotes: 2

Views: 41

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

You can't use A because it is not yet defined. Instead, you need to use 'A'. You will also need to use Type since 'A' means an instance of A:

from typing import Type

class A(object):
    @classmethod
    def some_class_method(cls: Type['A'], parameter: str) -> Type['A']:
        return A

Upvotes: 5

Related Questions