Python 类型提示:类定义本身的类型提示

Iny*_*Kim 0 python class type-hinting

class Session:
    

    @staticmethod
    def load_from_json(json_path:str) -> Session:
        pass
Run Code Online (Sandbox Code Playgroud)

上面抛出一个NameError: name 'Session' is not defined. 在其定义中使用自身作为类型提示是不可能的吗?

hus*_*sic 5

使用:

def ... -> 'Session':
Run Code Online (Sandbox Code Playgroud)

使用时Session没有定义,所以必须使用字符串(Python 3.8)。