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.

mypy: Variance error when using middleware with new-style app setup

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
JayH5commented, Jan 1, 2020

@taybin for now you can work around this by adding a type declaration to your list of middleware:

import typing

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware

t_middleware = typing.List[typing.Optional[Middleware]]
middleware: t_middleware = [Middleware(HTTPSRedirectMiddleware)]
app = Starlette(middleware=middleware)
0reactions
taybincommented, Jan 1, 2020

@dmontagu the fix hasn’t been published in a versioned release yet.

Read more comments on GitHub >

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

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