Since v2 and the typing stubs, the get method of the Headers data structure always returns a Optional[str]. However in some cases we can know statically that it will return a str. Indeed when a default string parameter is given, then the function always return a str.
Here's a preview of my code before v2:
auth_token = flask.request.headers.get('Authorization', '').replace('Bearer ', '')
Now that werkzeug is typed, the .replace will raise an typing error when analyzed with mypy.
error: Item "None" of "Optional[str]" has no attribute "replace"
I can fix it like this:
auth_token = typing.cast(str, flask.request.headers.get('Authorization', '')).replace('Bearer ', '')
or even like this
auth_header = flask.request.headers.get('Authorization', '')
if auth_header:
auth_token = auth_header.replace('Bearer ', '')
else:
auth_token = ''
But I feel like all of those patches shouldn't be required. I'll send a type fix.
Since v2 and the typing stubs, the
getmethod of theHeadersdata structure always returns aOptional[str]. However in some cases we can know statically that it will return astr. Indeed when a default string parameter is given, then the function always return astr.Here's a preview of my code before v2:
Now that
werkzeugis typed, the.replacewill raise an typing error when analyzed withmypy.I can fix it like this:
or even like this
But I feel like all of those patches shouldn't be required. I'll send a type fix.