Skip to content

gh-91996: Adding an HTTPMethod StrEnum to http #91997

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

Merged
merged 10 commits into from
May 5, 2022
Merged

Conversation

cibofo
Copy link
Contributor

@cibofo cibofo commented Apr 27, 2022

@ghost
Copy link

ghost commented Apr 27, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Every change to Python requires a NEWS entry.

Please, add it using the blurb_it Web app or the blurb command-line tool.

@ethanfurman ethanfurman self-assigned this Apr 27, 2022
@cibofo cibofo changed the title Adding an HTTPMethod str enum to http in stdlib #91996 Adding an HTTPMethod str enum to http in stdlib issue #91996 Apr 27, 2022
@ethanfurman ethanfurman changed the title Adding an HTTPMethod str enum to http in stdlib issue #91996 Adding an HTTPMethod StrEnum to http Apr 27, 2022
@cibofo cibofo changed the title Adding an HTTPMethod StrEnum to http gh-91996: Adding an HTTPMethod StrEnum to http Apr 27, 2022
Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the documentation if you want to add this enum

https://docs.python.org/3/library/http.html?highlight=httpstatus#http.HTTPStatus

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

obj.description = description
return obj

GET = 'GET', 'Transfer a current representation of the target resource.'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be in some sort of order, probably alphabetical, possibly grouped by safety: GET, HEAD, OPTIONS / DELETE, PATCH, POST, PUT / CONNECT, TRACE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same order from the RFCs, this is the usual order they are sorted (e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)

@davidism
Copy link

davidism commented Apr 28, 2022

While it's not technically a standard yet, would be good to be aware of https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html, the QUERY method to indicate a safe method (like GET) that has a body (like POST).

This also misses any WebDAV methods, which users of Flask/Werkzeug occasionally ask about.

If someone used this type to annotate a parameter, it would prevent users from using other methods unless the annotation was str | HTTPMethod.

@davidism
Copy link

Also, consider the churn that will be introduced by this. I guarantee that some user of every library that uses HTTP will open issues asking for the library's methods to support these, which will then require every maintainer to evaluate whether they want to support it or keep rejecting requests to support it forever. Since all existing libraries use strings, they will have to add more complex code to accept both.

@cibofo cibofo marked this pull request as draft April 28, 2022 16:43
@cibofo
Copy link
Contributor Author

cibofo commented Apr 30, 2022

While it's not technically a standard yet, would be good to be aware of https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html, the QUERY method to indicate a safe method (like GET) that has a body (like POST).

I think it might be better to include the QUERY method in a seperate PR since it isn't part of the http standard and might change over time.

This also misses any WebDAV methods, which users of Flask/Werkzeug occasionally ask about.

Most of the users don't really know about webDAV and it might be confusing to them to see it, since HTTPMethod is such a small enum (unlike HTTPStatus) it is very noticeable.
Do you think that it would be better to add the webDAV methods?

@cibofo
Copy link
Contributor Author

cibofo commented Apr 30, 2022

Also, consider the churn that will be introduced by this. I guarantee that some user of every library that uses HTTP will open issues asking for the library's methods to support these, which will then require every maintainer to evaluate whether they want to support it or keep rejecting requests to support it forever. Since all existing libraries use strings, they will have to add more complex code to accept both.

I think that libraries that don't conform to the new higher standards shouldn't stop the improvement of cpython.
New programs and libraries being developed shouldn't miss out on new features just because already existing libraries don't use these features.
Cpython influences the entire python community and it should provide the best practices for creating new programs.

@cibofo
Copy link
Contributor Author

cibofo commented Apr 30, 2022

Also, added documentation
I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@corona10: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from corona10 April 30, 2022 10:36
@davidism
Copy link

davidism commented Apr 30, 2022

Most of the users don't really know about webDAV and it might be confusing to them to see it, since HTTPMethod is such a small enum (unlike HTTPStatus) it is very noticeable.

You've named the enum HTTPMethod, but currently only represent the REST methods (and CONNECT and TRACE for some reason, even though they're probably even less common than WebDAV). From the RFC:

Additional methods, outside the scope of this specification, have been standardized for use in HTTP. All such methods ought to be registered within the "Hypertext Transfer Protocol (HTTP) Method Registry" maintained by IANA, as defined in Section 8.1.

https://www.iana.org/assignments/http-methods/http-methods.xhtml lists many more registered HTTP methods. Despite the RFC saying methods should be registered there, that's not a requirement, and in the end an HTTP method can be any string.

I think that libraries that don't conform to the new higher standards shouldn't stop the improvement in cpython.

This isn't a "higher standard", it's just a different value for the same thing. This needs to be very clearly an improvement, not just a change.

@cibofo
Copy link
Contributor Author

cibofo commented Apr 30, 2022

You've named the enum _HTTP_Method, but currently only represent the REST methods (and CONNECT and TRACE for some reason, even though they're probably even less common than WebDAV).

Correct me if I am wrong but WebDAV is an extension to HTTP, not actual part of the standard, CONNECT and TRACE are.
Anyway, what WebDAV methods do you think should be added?

This isn't a "higher standard", it's just a different value for the same thing. This needs to be very clearly an improvement, not just a change.

The code is more secure when using an enum, as magic strings are prone to mistypes and therefore errors,
I (and many more) also think that they are more convenient than just using strings.
So a more secure and convenient way to write code isn't a higher standard?

@davidism
Copy link

davidism commented Apr 30, 2022

Convenient maybe, but it requires an import and a longer reference to type, compared to a string. Secure no. Mistypes of http methods result in 405 errors, not security issues.

@cibofo
Copy link
Contributor Author

cibofo commented Apr 30, 2022

Convenient maybe, but it requires an import and a longer reference to type, compared to a string. Secure no. Mistypes of http methods result in 405 errors, not security issues.

Secure is not the right word, I meant less bugs

@cibofo cibofo marked this pull request as ready for review April 30, 2022 14:55
@cibofo
Copy link
Contributor Author

cibofo commented May 2, 2022

I would appreciate your response

@davidism
Copy link

davidism commented May 2, 2022

Sorry, I'm not a cpython maintainer. Some of them are at a PyCon sprint right now so I'm sure their notifications are overflowing more than usual.

@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label May 4, 2022
@ethanfurman
Copy link
Member

@cibofo instead of merging main into your branch, rebase your branch.

@cibofo
Copy link
Contributor Author

cibofo commented May 5, 2022

@ethanfurman Done

@ethanfurman
Copy link
Member

@cibofo Please create a new PR that addes the other 31 or so registered methods. Thanks!

@cibofo
Copy link
Contributor Author

cibofo commented May 5, 2022

@ethanfurman You got it! gonna work on it tomorrow

AlexWaygood added a commit to python/typeshed that referenced this pull request May 5, 2022
python/cpython#91997

`description` isn't actually read-only at runtime, but I don't think there's any other way of telling type checkers "this is an attribute that the members have, not a member itself". And pretending it's a property is already what we do for `HTTPStatus`, which has the same issue.
@merwok
Copy link
Member

merwok commented May 6, 2022

instead of merging main into your branch, rebase your branch.

That is contrary to usual workflow: https://devguide.python.org/pullrequest/

JelleZijlstra added a commit to python/typeshed that referenced this pull request May 7, 2022
hauntsaninja pushed a commit to python/typeshed that referenced this pull request May 7, 2022
python/cpython#91997

`description` isn't actually read-only at runtime, but I don't think there's any other way of telling type checkers "this is an attribute that the members have, not a member itself". And pretending it's a property is already what we do for `HTTPStatus`, which has the same issue.
@ethanfurman
Copy link
Member

instead of merging main into your branch, rebase your branch.

That is contrary to usual workflow: https://devguide.python.org/pullrequest/

Huh. Git Bootcamp uses rebase, although for older PRs. The couple times I tried merging in main it was a disaster, but I was also learning how to use git and github, so it could have easily been something else.

I'll keep using rebase for my own work -- it makes me happy. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants