question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Dependencies imported from file with python annotations don't work

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dmontagucommented, Aug 19, 2019

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?

0reactions
tiangolocommented, Apr 10, 2020

Thanks for the help here everyone! 👏 🙇

Thanks for reporting back and closing the issue @LKay 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found