Python Error: 'cannot import name 'Self' from 'typing_extensions'' - Solution and Explanation
This error occurs when you attempt to import the 'Self' type from the 'typing_extensions' module, but it's absent in your current 'typing_extensions' version.
The 'Self' type is commonly used in type hints to define recursive type annotations, where a class refers to itself within its own type hint.
To resolve this error, you have two options:
-
Upgrade 'typing_extensions': Update the 'typing_extensions' package to a version that includes the 'Self' type.
-
Utilize the built-in 'typing' module: Replace the import statement:
from typing_extensions import Self
with:
from typing import TypeVar
Self = TypeVar('Self')
This defines a type variable 'Self' that you can use within your type hints.
原文地址: https://www.cveoy.top/t/topic/pa5u 著作权归作者所有。请勿转载和采集!