Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enhancehttp.HTTPStatus withis_* properties that indicate the HTTP status category #95149

Closed
Labels
type-featureA feature request or enhancement
@alexei

Description

@alexei

Feature or enhancement

Add five properties tohttp.HTTPStatus that indicate the category the status belongs to:

classHTTPStatus:    ...@propertydefis_informational(self):return100<=self<200@propertydefis_success(self):return200<=self<300@propertydefis_redirection(self):return300<=self<400@propertydefis_client_error(self):return400<=self<500@propertydefis_server_error(self):return500<=self<600

Pitch

Programs that deal with HTTP requests usually need to deal with the various status codes. Oftentimes, knowing the broad category is sufficient to make quick decisions. For example, a client needs to handle responses differently depending on the status code. Assumingstatus is anHTTPStatus, instead of writing:

if200<=status<300:handle_response(response)elif300<=status<400:handle_redirect(response)elif400<=status<500:raiseClientError(status)elif500<=status<600:maybe_retry(response.request)

One could more elegantly write:

ifstatus.is_success:handle_response(response)elifstatus.is_redirection:handle_redirect(response)elifstatus.is_client_error:raiseClientError(status)elifstatus.is_server_error:maybe_retry(response.request)

Enums are safer, more readable and more expressive than magic numbers. I think these small additions would only strengthenHTTPStatus.

Let me know if this interests you, and I can open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp