-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Werkzeug EnvironHeaders
is not considered a Mapping
#3569
Comments
Do you have an example that you think should type check but doesn't? |
Here is small example: from flask import request
import typing as t
T = t.TypeVar('T')
Z = t.TypeVar('Z')
def get(kv: t.Mapping[T, Z], key: T) -> Z:
return kv[key]
get(request.headers, 'a_header') On which mypy errors with: |
For posterity, here is a custom protocol that works for me in one particular place:
This is not a general solution to the problem! But I just spent at least an hour banging my head against this until I got it working. Hopefully this can save someone else some time in the future. I just had to change my code from
to
and now mypy is happy. |
typing.Mapping is not a protocol, which has caused problems in the past. (E.g. python#3569, see also python#3576.) This introduces a few narrow protocols to _typeshed.pyi that can be used in place of Mapping. Not all uses of Mapping can be replaced. For example, cgi.FieldStorage explictly checks whether the supplied headers argument is a Mapping instance.
A brief addendum on why we can't just derive from collections.abc import Mapping
from werkzeug.datastructures import Headers
isinstance(Headers(), Mapping) # False Deriving from cgi import FieldStorage
from werkzeug.datastructures import Headers
FieldStorage(headers=Headers()) # throws a TypeError |
typing.Mapping is not a protocol, which has caused problems in the past. (E.g. #3569, see also #3576.) This introduces a few narrow protocols to _typeshed.pyi that can be used in place of Mapping. Not all uses of Mapping can be replaced. For example, cgi.FieldStorage explictly checks whether the supplied headers argument is a Mapping instance.
Closing as the werkzeug stubs will be removed in a few months. |
It seems that the
EnvironHeaders
class ofwerkzeug
(and probably the base classHeaders
too) is not compatible withMapping
, however it looks like it should be compatible (it implements all needed methods, and functions like one).The text was updated successfully, but these errors were encountered: