8000 Add __all__ to __init__.py files to silence mypy(strict) errors · Issue #2237 · fastapi/fastapi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __all__ to __init__.py files to silence mypy(strict) errors #2237

Closed
toinbis opened this issue Oct 24, 2020 · 8 comments
Closed

Add __all__ to __init__.py files to silence mypy(strict) errors #2237

toinbis opened this issue Oct 24, 2020 · 8 comments
Labels
question Question or problem question-migrate

Comments

@toinbis
Copy link
toinbis commented Oct 24, 2020

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 fastapi/typer#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 python/mypy#7042 discussion).

Thanks for considering this!

@toinbis toinbis added the question Question or problem label Oct 24, 2020
@ycd
Copy link
Contributor
ycd commented Oct 24, 2020

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.

@toinbis
Copy link
Author
toinbis commented Oct 24, 2020

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.

@toinbis
Copy link
Author
toinbis commented Oct 24, 2020

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?

@Kludex
Copy link
Member
Kludex commented Oct 25, 2020

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.

@toinbis
Copy link
Author
toinbis commented Oct 27, 2020

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.

@toinbis toinbis closed this as completed Oct 27, 2020
@charlax
Copy link
charlax commented Dec 1, 2020

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!

@tiangolo
Copy link
Member

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 🎉

@toinbis
Copy link
Author
toinbis commented Dec 21, 2020

Thank you so much, @tiangolo !

@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7182 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

5 participants
0