-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Make http.cookies.SimpleCookie
non-generic
#10701
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
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]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'> |
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 |
Chances are it's a Python 2 remnant: maybe in Python 2 you could have both |
Looks like both classes were originally made generic in: But there's no explanation in that PR thread as to why |
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 from http.cookies import BaseCookie, Morsel
class C(BaseCookie[int]):
...
c = C()
c["foo"] = "1"
coded_value = c["foo"].coded_value
reveal_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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes: #10700