Skip to content
Myr

Add documentation for available status codes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

There was no way to determine what actual names were available outside of looking at the source code. They were not listed in the documentation or accessible through the interactive help. In addition, doing `pydoc requests.status_codes` displayed some pretty unhelpful information - the utf-8 encoding string was included in the module name, there was no description, and internal variables used for initialisation leaked into the module scope: DATA code = 511 codes = <lookup 'status_codes'> title = 'network_authentication' titles = ('network_authentication_required', 'network_auth', ... This change prevents the internal variables from leaking, adds a docstring (which has the side-effect of correcting the module name), and appends information on the allowed status code names to the docstring when the module is initialised. The improved module documentation is then used in the API documentation to provide another easy reference to the complete list of status codes.

authored by Zane Bitter · committed by Zane Bitter · parent acd2645 · Browse files · 2 files+34 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−16 removed

  • other:+34 added−16 removed

2 files+34 added−16 removed

modified:docs/api.rst+1 added-11 removed
107 unmodified lines
108------------------109110.. autoclass:: requests.codes111112::112.. automodule:: requests.status_codes113114 >>> requests.codes['temporary_redirect']115 307114117 >>> requests.codes.teapot118 418119120 >>> requests.codes['\o/']121 200122123124115Migrating to 1.x116----------------117118This section details the main differences between 0.x and 1.x and is meant
142 unmodified lines
modified:requests/status_codes.py+33 added-5 removed
1# -*- coding: utf-8 -*-23"""4The ``codes`` object defines a mapping from common names for HTTP statuses5to their numerical codes, accessible either as attributes or as dictionary6items.78>>> requests.codes['temporary_redirect']930710>>> requests.codes.teapot1141812>>> requests.codes['\o/']132001415Some codes have multiple names, and both upper- and lower-case versions of16the names are allowed. For example, ``codes.ok``, ``codes.OK``, and17``codes.okay`` all correspond to the HTTP status code 200.18"""1920from .structures import LookupDict2122_codes = {23
76 unmodified lines
100}101102codes = LookupDict(name='status_codes')103104def _init():87for code, titles in _codes.items():88 for title in titles:89 setattr(codes, title, code)90 if not title.startswith(('\\', '/')):91 setattr(codes, title.upper(), code)105 for code, titles in _codes.items():106 for title in titles:107 setattr(codes, title, code)108 if not title.startswith(('\\', '/')):109 setattr(codes, title.upper(), code)110111 def doc(code):112 names = ', '.join('``%s``' % n for n in _codes[code])113 return '* %d: %s' % (code, names)114115 global __doc__116 __doc__ = (__doc__ + '\n' +117 '\n'.join(doc(code) for code in sorted(_codes)))118119_init()