Skip to content Skip to sidebar Skip to footer

Typing Hints Pycharm For A Class Used Within Itself

How do I properly type a class within itself? In PyCharm I am currently getting this error: This is an unresolved reference error. This normally makes sense because I wouldn't exp

Solution 1:

When using a class as a type inside that class, or anywhere where that type is not fully defined yet, you need to enclose the type in single or double quotes in your annotations:

classItem:
    ...
    defcraft(self, substrates: List['Item'], amount: int) -> List['Item']:
        ...

Sources:

https://www.python.org/dev/peps/pep-0484/#forward-references

https://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/ (String-Based Hints)

Edit: PEP 563: https://www.python.org/dev/peps/pep-0563/ improves upon this.

Post a Comment for "Typing Hints Pycharm For A Class Used Within Itself"