Fastapi: Add __all__ to __init__.py files to silence mypy(strict) errors

Created on 24 Oct 2020  路  8Comments  路  Source: tiangolo/fastapi

Strict mypy mode gives such errors:

base/api/users/controllers.py:4: error: Module 'fastapi' has no attribute 'Depends'
base/api/users/controllers.py:4: error: Module 'fastapi' has no attribute 'HTTPException'

on such import statement:

from fastapi import Depends, HTTPException

Tried using

from fastapi import Depends as Depends
from fastapi import HTTPException as HTTPException

as per recommendations in https://github.com/tiangolo/typer/issues/112 discussion. But the errors remain.

It seems that adding __all__ to the __init__.py files for the stuff that's reexported is a way to go (as per https://github.com/python/mypy/issues/7042 discussion).

Thanks for considering this!

question

Most helpful comment

Thanks for the discussion here everyone!

I added support for mypy --strict in #2547 with a lot of type annotation improvements.

It is available in FastAPI 0.63.0 :tada:

All 8 comments

Related with #1309 and #1537

This is what @tiangolo thinks about adding __all__ definitions

I actually prefer not to have the __all__ as it adds code duplication that can get out of sync easily. I would prefer import x as x, as any errors in that would be detected by the editor.

Hi @ycd . Thanks a lot for explanation. I had the feeling this might be by design. What got me confused was that from fastapi import Depends as Depends still generates mypy error warning but probably that's already some issues with mypy(config) at my side. Will try to get that resolved and will close the issue afterwards.

I've tried creating a fresh project with a single .py file:

1st version:

from fastapi.testclient import TestClient
print(TestClient)

2nd version:

from fastapi.testclient import TestClient as TestClient
print(TestClient)

On both scenarios I get the same result. Not using --strict mode all is good:

$ mypy --show-error-codes script.py 
Success: no issues found in 1 source file

Whilst enabling strict mode compains on both cases:

$ mypy --strict --show-error-codes script.py 
script.py:1: error: Module 'fastapi.testclient' has no attribute 'TestClient'  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

My mypi.ini looks as follows:

[mypy]
disallow_untyped_defs = True
warn_unused_ignores = True
warn_redundant_casts = True

Is it expected behaviour? Or is it something wrong with my mypy config? If that's expected as of now, is there anything else to be done except placing # type: ignore[attr-defined] on each import from fastapi?

I follow the mypy suggestion on Pydantic side:

mypy --ignore-missing-imports --follow-imports=skip --strict-optional <file>

I think it makes sense to use this configuration as FastAPI is based on it.

Thanks, @Kludex . Me myself have ended up adding # type: ignore[attr-defined] . Am closing the issue as I think both alternatives have been discussed and both are good enough :) Thanks everyone for jumping in.

Does that mean that we'll always have to add # type: ignore[attr-defined]? Or will there be a solution that does not require this comment for every. single. import?

Thanks a lot for the lib!

Thanks for the discussion here everyone!

I added support for mypy --strict in #2547 with a lot of type annotation improvements.

It is available in FastAPI 0.63.0 :tada:

Thank you so much, @tiangolo !

Was this page helpful?
0 / 5 - 0 ratings