This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot declare method or static function parameter or return type as self of the class
Type: behavior Stage: resolved
Components: Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, parsampsh, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-11-29 12:58 by parsampsh, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg382061 - (view) Author: parsa mpsh (parsampsh) * Date: 2020-11-29 12:58
python raises error `Undefined class X` when you declaring parameter type or return type of method as the self of class:

class MyClass:
    def mymethod(self, a: MyClass): # parameter type is class self
        pass

# or

class MyClass:
    def mymethod(self) -> MyClass: # return type is class self
        pass
# or both of them

error `Undefined class ...` will be raised.
msg382064 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-29 14:17
Add at the top of your script:

from __future__ import annotations
msg382066 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-11-29 14:35
You can already write such code in 3.10. In earlier Python versions you can either write annotation explicitly as string or (since 3.7) use and import suggested by Irit.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86669
2020-11-29 14:35:04serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg382066

resolution: out of date
stage: resolved
2020-11-29 14:17:58iritkatrielsetnosy: + iritkatriel
messages: + msg382064
2020-11-29 12:58:55parsampshcreate