'Can you annotate return type when value is instance of cls?

Given a class with a helper method for initialization:

class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> ?:
        str_arg = str(int_arg)
        return cls(str_arg)

Is it possible to annotate the return type of the from_int method?

I'v tried both cls and TrivialClass but PyCharm flags them as unresolved references which sounds reasonable at that point in time.



Solution 1:[1]

From Python 3.7 you can use __future__.annotations:

from __future__ import annotations


class TrivialClass:
    # ...

    @classmethod
    def from_int(cls, int_arg: int) -> TrivialClass:
        # ...
        return cls(...)

Edit: you can't subclass TrivialClass without overriding the classmethod, but if you don't require this then I think it's neater than a forward reference.

Solution 2:[2]

A simple way to annotate the return type is to use a string as the annotation for the return value of the class method:

# test.py
class TrivialClass:
  def __init__(self, str_arg: str) -> None:
    self.string_attribute = str_arg

  @classmethod
  def from_int(cls, int_arg: int) -> 'TrivialClass':
    str_arg = str(int_arg)
    return cls(str_arg)

This passes mypy 0.560 and no errors from python:

$ mypy test.py --disallow-untyped-defs --disallow-untyped-calls
$ python test.py

Solution 3:[3]

In Python 3.11 there will be a nicer way to do this using the new Self type:

class TrivialClass:
    def __init__(self, str_arg: str):
        self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> Self:
        str_arg = str(int_arg)
        return cls(str_arg)

This is going to work for sub classes as well.

This is described in PEP 673. The example used in the section Use in Classmethod Signatures is exactly equivalent to this question.

I plan to accept this answer once Python 3.11 is released (2022-10-03 as of now).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jean-François Corbett
Solution 3