[BUG] Dependencies imported from file with python annotations don't work
See original GitHub issueDescribe the bug
When there is a file that contains python annotations import at the top and contains some async function returning parameters dependency injection doesn’t work.
To Reproduce
Create file deps.py
:
from __future__ import annotation
async def test(foo: Optional[str] = None):
return foo
And then in the other file containing route definition:
from .deps import test
@api.get("/foo")
async def foo_route(foo: Optional[str] = Depends(test)):
return None
Trying to run the app with such configuration results in error:
...
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 179, in get_dependant
param_field = get_param_field(param=param, default_schema=params.Query)
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 265, in get_param_field
schema=schema,
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/pydantic/fields.py", line 112, in __init__
self.prepare()
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/pydantic/fields.py", line 178, in prepare
self._populate_validators()
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/pydantic/fields.py", line 269, in _populate_validators
*(get_validators() if get_validators else list(find_validators(self.type_, self.model_config))),
File "/Users/user/.local/share/virtualenvs/pilgrim-api-A9HJbenQ/lib/python3.7/site-packages/pydantic/validators.py", line 470, in find_validators
raise RuntimeError(f'error checking inheritance of {type_!r} (type: {display_as_type(type_)})') from e
RuntimeError: error checking inheritance of 'Optional[str]' (type: str)
Expected behavior It runs and works.
Environment:
- OS: macOS
- FastAPI Version: 0.35
- Python: 3.7.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Avoiding circular imports with type annotations in situations ...
If the circular dependency is introduced not only by type annotations (e.g. your type aliases), the resolving may become really tricky. If you ......
Read more >How to Fix ImportError: Cannot Import Name in Python - Rollbar
This error generally occurs when a class cannot be imported due to one of the following reasons: The imported class is in a...
Read more >Make it easier to work with circular module dependencies #481
Mypy complains about not being able to infer a type because of a circular dependency. Mypy type checks okay but at runtime something...
Read more >Annotation issues at runtime - mypy 0.991 documentation
Since code inside if TYPE_CHECKING: is not executed at runtime, it provides a convenient way to tell mypy something without the code being...
Read more >importlib — The implementation of import — Python 3.11.1 ...
A dotted name does not have its parents implicitly imported as that requires ... the loader to work with source and bytecode files;...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t know if this is the ultimate source of the error, but FastAPI has some issues with
from __future__ import annotations
(and string-annotations in general), and I’ve seen it cause the specific error message you are receiving before. Try removing the future import and see if it fixes the problem?Thanks for the help here everyone! 👏 🙇
Thanks for reporting back and closing the issue @LKay 👍