serhii.net

In the middle of the desert you can say anything you want

26 Jul 2022

Python typing classmethods return type

From python - How do I type hint a method with the type of the enclosing class? - Stack Overflow:

If you have a classmethod and want to annotate the return value as that same class you’re now defining, you can actually do the logical thing!

from __future__ import annotations

class Whatever:
	# ...
	@classmethod what(cls) -> Whatever:
		return cls()
Nel mezzo del deserto posso dire tutto quello che voglio.