-
-
Notifications
You must be signed in to change notification settings - Fork 484
Change test Client headers parameter to Mapping[str, str] #1534
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
base: master
Are you sure you want to change the base?
Conversation
Ping @Alexerson as author of #1529 |
I would say it is |
Thanks or the other changes. There is indeed no casting happening in the code in Django, so the current typing is matching the behavior. I can see that the real I will try to find more info to see if that's valid, or if we should tighten the type, but in that case, I'm assuming it would need to be everywhere. |
Apologies in advance for the following rant, but I just find this so weird. When I suggested changing When I point out this actually causes issues, there is total radio silence, as if there is silent disagreement. It's not the first time either. I've noticed this pressure to loosen up types in other discussions too, where I've been the only lone voice to advocate for avoiding During the last decade or so, it's finally begun to sink in to many programming communities that loose typing causes trouble. Not just Python, but also TypeScript, adoption of generics in Java and Go, null annotations in Java, and the push for more expressive type systems in general. This is what drew me into using mypy and contributing to django-stubs. Yet in this one project that aims to bring typing to Django, people regularly advocate for loosening types to accept "yolo whatever". Why is this? I honestly find this frustrating. HTTP headers are a concept defined in the HTTP text-based protocol. They are mappings of strings to strings. Why should django-stubs accept anything other than strings for headers? What does it mean for a header to have value of type If someone wants to pass a number, or string-value of other object, as an HTTP header value, I think changing Maybe there is a case to be made to accept implicit conversion from |
Even though my opinion is not completely made on the topic yet, I agree with you that types should help us write better Python, not just make us happy because we feel we've added something to our codebase. Regarding headers: I tend to agree with you, but if that's OK with you, I'll try to look at the specs to see if there is any use case for something other than |
OK, so reading a bit more how headers are constructed, I see that the values are indeed strings for ASGI: in Django, it’s done here. (I checked other implementations, and it seems to be consistent with this) For WSGI, I wonder if we should use So your PR is definitively valid for async one. I’m trying to read more about |
Well… I guess we might want to stick with Edit: and see also python/typeshed#1745 The thing is, |
We can reuse this type :) |
Right now request header values are typed as If you insist that header values are But I think that would be a big mistake. |
I might be wrong, but |
Ah actually you’re right, I read it this morning while doing my search, and thought that this is what it was, but it’s indeed enforcing that values are So, yeah, we should probably do the same thing in the test part. |
I split out the non-controversial changes as a separate PR in #1537. This discussion is now focused on whether header values should be typed |
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.
When I point out this actually causes issues, there is total radio silence, as if there is silent disagreement.
Apologies on my end. I have had little time for open source in the last few weeks. Back now.
Thank you for checking that headers
does not cast to str
. I did indeed presume it did at some point between the client and response objects, but clearly not.
Using str
LGTM!
Yet in this one project that aims to bring typing to Django, people regularly advocate for loosening types to accept "yolo whatever". Why is this? I honestly find this frustrating.
I would only like to loosen types where it will help adoption. The dream would that be adding django-stubs to an existing, mature, tested project would only reveal true errors and not require extra narrowing/casting to make things work. That was my intent here, but I could have tested before asking.
84bfa0f
to
5ed48b2
Compare
I have rebased and updated this PR. |
NOTE: Some changes that were originally part of this PR have already been merged in #1537.
headers=
parameter type fromMapping[str, Any]
toMapping[str, str]
headers
attribute fromdict[str, Any]
toMapping[str, str] | None
Related issues