sphinx-autodoc-typehints: Cannot resolve forward reference
Code:
if TYPE_CHECKING:
from qc.model.user import User
...
@property
@abstractmethod
def owner(self) -> "User":
...
Error with set_type_checking_flag = False:
WARNING: Cannot resolve forward reference in type annotations of "qc.db.Owned.owner": name 'User' is not defined
Exception occurred:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 786, in findsource
raise OSError('could not get source code')
OSError: could not get source code
Error with set_type_checking_flag = True:
WARNING: autodoc: failed to import module 'app' from module 'qc'; the following exception was raised:
cannot import name 'User' from 'qc.model.user' (/Users/cyber/dev/jb/qc/qc-backend/qc/model/user.py)
WARNING: autodoc: failed to import module 'commands' from module 'qc'; the following exception was raised:
cannot import name 'TSTZ' from 'qc.db' (/Users/cyber/dev/jb/qc/qc-backend/qc/db/__init__.py)
... [many more]
cannot import name 'User' from 'qc.model.user' (/Users/cyber/dev/jb/qc/qc-backend/qc/model/user.py)
cannot import name 'User' from 'qc.model.user' (/Users/cyber/dev/jb/qc/qc-backend/qc/model/user.py)
cannot import name 'User' from 'qc.model.user' (/Users/cyber/dev/jb/qc/qc-backend/qc/model/user.py)
WARNING: autodoc: failed to import module 'test' from module 'qc'; the following exception was raised:
cannot import name 'User' from 'qc.model.user' (/Users/cyber/dev/jb/qc/qc-backend/qc/model/user.py)
Using sphinx-apidoc and python 3.7.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 28
Type checking seems to work normally;
mypyruns without any problem. I am importing the module usingTYPE_CHECKING. The expression should be evaluatable ifTYPE_CHECKINGis enabled. Reading #72, I’m unclear how to fix this.Duplicate of #72. See the discussion over there. Typeguard must be able to evaluate the expression in the annotation.