Uh oh!
There was an error while loading. Please reload this page.
Make http.cookies.SimpleCookie non-generic - #10701
Conversation
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
+ steam/http.py:180: error: The type "type[SimpleCookie]" is not generic and not indexable [misc]+ steam/http.py:181: error: The type "type[SimpleCookie]" is not generic and not indexable [misc]
aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/helpers.py:942:24: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/helpers.py:942:24: note: Error code "type-arg" not covered by "type: ignore" comment+ aiohttp/helpers.py:942:24: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-type-arg for more info+ aiohttp/helpers.py:945:26: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/helpers.py:1028:43: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/cookiejar.py:66:53: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/cookiejar.py:169:22: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/cookiejar.py:235:35: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/cookiejar.py:246:25: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/connector.py:228:23: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/client_reqrep.py:377:12: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/client_reqrep.py:710:23: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]+ aiohttp/web_request.py:574:17: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg]
starlette (https://github.com/encode/starlette)
+ tests/test_responses.py:347: error: "SimpleCookie" expects no type arguments, but 1 given [type-arg] |
srittau
left a comment
There was a problem hiding this comment.
Thanks! While the primer hits indicate that this change is disruptive, looking at the hits in depth indicates that the code in question can be simplified significantly. Currently, using SimpleCookie requires an explicit type annotation to specify the generic type (which can only be str), so I think the benefits outweighs the disruption significantly.
I'll leave it open for now if other maintainers want to chime in.
I'm not too familiar with this module, so this may be a dumb question, but should even >>> from http.cookies import BaseCookie
>>> b = BaseCookie()
>>> b['foo'] =b'bar'
>>> b['foo'
<Morsel: foo=b'bar'>
>>> _.coded_value
"b'bar'"
>>> type(b['foo'].coded_value)
<class 'str'> |
flaeppe
commented
Sep 12, 2023
Yeah, I don't know either. I'm getting a feeling it might be about conversion between formats rather than conversion to e.g. bytes. But I'm only guessing here |
JelleZijlstra
commented
Sep 12, 2023
Chances are it's a Python 2 remnant: maybe in Python 2 you could have both |
AlexWaygood
commented
Sep 12, 2023
Looks like both classes were originally made generic in: But there's no explanation in that PR thread as to why |
flaeppe
commented
Sep 12, 2023
Documentation for
I think an additional thing to check up on here is this
Funny thing is though, just because you've declared Consider this program, which passes, but fromhttp.cookiesimportBaseCookie, MorselclassC(BaseCookie[int]):
...
c=C()
c["foo"] ="1"coded_value=c["foo"].coded_valuereveal_type(coded_value) # Revealed type is "builtins.int"But if you'd instead use below, types align. m=Morsel[int]()
m.set("foo", "1", 1)
c["foo"] =m |
OK cool, sounds like |
Fixes: #10700