Uh oh!
There was an error while loading. Please reload this page.
Use AnyStr where appropriate - #999
Conversation
lovelydinosaur
commented
May 26, 2020
After this change... $ venv/bin/mypy example.py Success: no issues found in 1 source file |
yeraydiazdiaz
commented
May 26, 2020
I don't think that's the case, either using importtypingAnyStrDict=typing.Dict[typing.AnyStr, typing.AnyStr]
AnyStrMapping=typing.Mapping[typing.AnyStr, typing.AnyStr]
d: AnyStrDict= {b"foo": "bar"}
m: AnyStrMapping= {b"foo": "bar"} |
lovelydinosaur
commented
May 26, 2020
Gotcha. It’s at least “consistent within this part of the type” tho right? Eg. Keys as str or Keys as bytes, not keys as str or bytes. |
yeraydiazdiaz
commented
May 26, 2020
Apparently not 😕 , again mypy is completely fine with the code below: importtypingAnyStrDict=typing.Dict[typing.AnyStr, typing.AnyStr]
AnyStrMapping=typing.Mapping[typing.AnyStr, typing.AnyStr]
d: AnyStrDict= {"foo": "bar", b"bar": "baz"}
m: AnyStrMapping= {b"foo": "bar", "foo": "baz"}Looks like the guarantees only hold in the context of a function. From the docs:
|
lovelydinosaur
commented
May 26, 2020
Interestingly fromtypingimportUnion, Dict, Sequence, Tuple, AnyStrHeaderTypes=Union[
Dict[AnyStr, AnyStr], Sequence[Tuple[AnyStr, AnyStr]],
]
deffoo(h: HeaderTypes) ->None:
print(h)
defbar(h: Union[Dict[AnyStr, AnyStr], Sequence[Tuple[AnyStr, AnyStr]]]) ->None:
print(h)
headers= {b"content-type": b"example", "accept": "*/*"}
foo(headers) # Okbar(headers) # Error |
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Refs #993, #985
Makes more careful use of
typing.Union[str, bytes]vs.typing.AnyStr.Use
AnyStrwhere we'd expect the types to be the same across everything in the signature. Eg.typing.Mapping[AnyStr, AnyStr]means "choose either str or bytes here, but be consistent.".Resolves an issues where mypy errors with a really simple bit of
httpxusage...example.py
MyPy