mypy: Variance error when using middleware with new-style app setup
See original GitHub issueWhen using the new-style app setup in Starlette 0.13, where properties of the app are set in the app constructor, mypy can produce an error when middleware is configured. The error is like:
app.py:6: error: Argument "middleware" to "Starlette" has incompatible type "List[Middleware]"; expected "Optional[List[Optional[Middleware]]]"
app.py:6: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance
app.py:6: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)
This can be reproduced with this minimal app:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
middleware = [Middleware(HTTPSRedirectMiddleware)]
app = Starlette(middleware=middleware)
Putting this in a file called app.py
and then running mypy app.py
(no custom mypy config) produces the above error.
Given that this is the recommended way to configure middleware, mypy should accept the above code.
❯ pip freeze
mypy==0.740
mypy-extensions==0.4.3
starlette==0.13.0
typed-ast==1.4.0
typing-extensions==3.7.4.1
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Common issues and solutions - mypy 0.991 documentation
If installation fails, you've probably hit one of these issues: Mypy needs ... To only ignore errors with a specific error code, use...
Read more >New mypy error after updating to numpy 1.22 - Stack Overflow
x: np.ndarray v: float # mypy complains: 'error: Value of type ... and had to use # type: ignore to suppress # mypy...
Read more >error logging Code Example - Code Grepper
For now, this is how I like to set up logging in Python: ... supplied with REST Assured or you can use one...
Read more >[FIXED] Python super() raises TypeError - PythonFixing
Issue When I use VSCode interactive Python, sometimes I get this message: Output exceeds ... [FIXED] AttributeError: module 'selenium.webdriver' ...
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
@taybin for now you can work around this by adding a type declaration to your list of middleware:
@dmontagu the fix hasn’t been published in a versioned release yet.