microversion-parse 1.0.1__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. microversion_parse-2.1.0/.pre-commit-config.yaml +38 -0
  2. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/.zuul.yaml +1 -1
  3. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/AUTHORS +7 -0
  4. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/ChangeLog +28 -0
  5. microversion_parse-2.1.0/PKG-INFO +144 -0
  6. microversion_parse-2.1.0/doc/source/conf.py +131 -0
  7. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/__init__.py +78 -50
  8. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/middleware.py +36 -11
  9. microversion_parse-2.1.0/microversion_parse/tests/__init__.py +0 -0
  10. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/test_extract_version.py +49 -29
  11. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/test_get_version.py +57 -42
  12. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/test_headers_from_wsgi_environ.py +27 -22
  13. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/test_middleware.py +6 -4
  14. microversion_parse-2.1.0/microversion_parse/tests/test_webob.py +95 -0
  15. microversion_parse-2.1.0/microversion_parse.egg-info/PKG-INFO +144 -0
  16. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse.egg-info/SOURCES.txt +5 -1
  17. microversion_parse-2.1.0/microversion_parse.egg-info/pbr.json +1 -0
  18. microversion_parse-2.1.0/pyproject.toml +71 -0
  19. microversion_parse-2.1.0/releasenotes/notes/remove-py39-2d2966941e7ef634.yaml +5 -0
  20. microversion_parse-2.1.0/setup.cfg +7 -0
  21. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/setup.py +2 -1
  22. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/test-requirements.txt +0 -1
  23. microversion_parse-2.1.0/tox.ini +60 -0
  24. microversion_parse-1.0.1/PKG-INFO +0 -143
  25. microversion_parse-1.0.1/doc/source/conf.py +0 -285
  26. microversion_parse-1.0.1/microversion_parse/tests/test_webob.py +0 -80
  27. microversion_parse-1.0.1/microversion_parse.egg-info/PKG-INFO +0 -143
  28. microversion_parse-1.0.1/microversion_parse.egg-info/pbr.json +0 -1
  29. microversion_parse-1.0.1/setup.cfg +0 -28
  30. microversion_parse-1.0.1/tox.ini +0 -48
  31. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/.stestr.conf +0 -0
  32. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/LICENSE +0 -0
  33. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/Makefile +0 -0
  34. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/README.rst +0 -0
  35. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/doc/requirements.txt +0 -0
  36. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/doc/source/index.rst +0 -0
  37. /microversion_parse-1.0.1/microversion_parse/tests/__init__.py → /microversion_parse-2.1.0/microversion_parse/py.typed +0 -0
  38. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/gabbits/middleware.yaml +0 -0
  39. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse/tests/gabbits/simple.yaml +0 -0
  40. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse.egg-info/dependency_links.txt +0 -0
  41. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse.egg-info/not-zip-safe +0 -0
  42. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse.egg-info/requires.txt +0 -0
  43. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/microversion_parse.egg-info/top_level.txt +0 -0
  44. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/releasenotes/notes/drop-python27-support-5b9a4e0ce63b730e.yaml +0 -0
  45. {microversion_parse-1.0.1 → microversion_parse-2.1.0}/requirements.txt +0 -0
@@ -0,0 +1,38 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v6.0.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ - id: mixed-line-ending
7
+ args: ['--fix', 'lf']
8
+ exclude: '.*\.(svg)$'
9
+ - id: fix-byte-order-marker
10
+ - id: check-executables-have-shebangs
11
+ - id: check-merge-conflict
12
+ - id: debug-statements
13
+ - id: check-yaml
14
+ files: .*\.(yaml|yml)$
15
+ - repo: https://github.com/astral-sh/ruff-pre-commit
16
+ rev: v0.14.5
17
+ hooks:
18
+ - id: ruff-check
19
+ args: ['--fix', '--unsafe-fixes']
20
+ - id: ruff-format
21
+ - repo: https://opendev.org/openstack/hacking
22
+ rev: 8.0.0
23
+ hooks:
24
+ - id: hacking
25
+ additional_dependencies: []
26
+ exclude: '^(doc|releasenotes|tools)/.*$'
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v1.18.2
29
+ hooks:
30
+ - id: mypy
31
+ additional_dependencies:
32
+ - types-WebOb
33
+ # keep this in-sync with '[mypy] exclude' in 'pyproject.toml'
34
+ exclude: |
35
+ (?x)(
36
+ doc/.*
37
+ | releasenotes/.*
38
+ )
@@ -1,4 +1,4 @@
1
1
  - project:
2
2
  templates:
3
- - openstack-python3-ussuri-jobs
3
+ - openstack-python3-jobs
4
4
  - check-requirements
@@ -1,9 +1,16 @@
1
1
  Andreas Jaeger <aj@suse.com>
2
2
  Chris Dent <cdent@anticdent.org>
3
3
  Chris Dent <chris.dent@gmail.com>
4
+ Daniel Bengtsson <dbengt@redhat.com>
5
+ Ghanshyam <gmann@ghanshyammann.com>
4
6
  Hervé Beraud <hberaud@redhat.com>
5
7
  Louis Taylor <louis@kragniz.eu>
6
8
  Moisés Guimarães de Medeiros <moguimar@redhat.com>
7
9
  PollyZ <Polly.Zhou@rackspace.com>
10
+ Stephen Finucane <stephenfin@redhat.com>
11
+ Takashi Kajinami <kajinamit@oss.nttdata.com>
12
+ Takashi Kajinami <tkajinam@redhat.com>
8
13
  Vieri <15050873171@163.com>
9
14
  huang.zhiping <huang.zhiping@99cloud.net>
15
+ niuke <niuke19970315@163.com>
16
+ niuke <niuke@inspur.com>
@@ -1,6 +1,34 @@
1
1
  CHANGES
2
2
  =======
3
3
 
4
+ 2.1.0
5
+ -----
6
+
7
+ * gitignore: Ignore pbr-generated artifacts
8
+ * Add typing
9
+ * pre-commit: Bump versions
10
+ * ruff: Use more specific name to enable pyupgrade rule
11
+ * Add ruff
12
+ * pre-commit: Bump dependencies
13
+ * Migrate setup configuration to pyproject.toml
14
+ * Drop Python 3.9 support
15
+
16
+ 2.0.0
17
+ -----
18
+
19
+ * Bump hacking
20
+ * Update python classifier in setup.cfg
21
+ * Fix warnings in doc build
22
+ * setup.cfg: Replace dashes with underscores
23
+ * Revert "Moves supported python runtimes from version 3.8 to 3.10"
24
+ * Moves supported python runtimes from version 3.8 to 3.10
25
+ * remove unicode from code
26
+ * Drop python3.6/3.7 support in testing runtime
27
+ * Move flake8 as a pre-commit local target
28
+ * Use py3 as the default runtime for tox
29
+ * Use Python 3.8 as the default runtime for tox
30
+ * Adding pre-commit
31
+
4
32
  1.0.1
5
33
  -----
6
34
 
@@ -0,0 +1,144 @@
1
+ Metadata-Version: 2.1
2
+ Name: microversion_parse
3
+ Version: 2.1.0
4
+ Summary: OpenStack microversion header parser
5
+ Author-email: OpenStack <openstack-discuss@lists.openstack.org>
6
+ License: Apache-2.0
7
+ Project-URL: Repository, https://opendev.org/openstack/microversion-parse
8
+ Classifier: Environment :: OpenStack
9
+ Classifier: Intended Audience :: Information Technology
10
+ Classifier: Intended Audience :: System Administrators
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Operating System :: POSIX :: Linux
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/x-rst
21
+ License-File: LICENSE
22
+ Requires-Dist: WebOb>=1.2.3
23
+
24
+ microversion_parse
25
+ ==================
26
+
27
+ A small set of functions to manage OpenStack `microversion`_ headers that can
28
+ be used in middleware, application handlers and decorators to effectively
29
+ manage microversions.
30
+
31
+ Also included, in the ``middleware`` module, is a ``MicroversionMiddleware``
32
+ that will process incoming microversion headers.
33
+
34
+ get_version
35
+ -----------
36
+
37
+ A simple parser for OpenStack microversion headers::
38
+
39
+ import microversion_parse
40
+
41
+ # headers is a dict of headers with folded (comma-separated
42
+ # values) or a list of header, value tuples
43
+ version = microversion_parse.get_version(
44
+ headers, service_type='compute',
45
+ legacy_headers=['x-openstack-nova-api-version'])
46
+
47
+ # If headers are not already available, a dict of headers
48
+ # can be extracted from the WSGI environ
49
+ headers = microversion_parse.headers_from_wsgi_environ(environ)
50
+ version = microversion_parse.get_version(
51
+ headers, service_type='placement')
52
+
53
+ It processes microversion headers with the standard form::
54
+
55
+ OpenStack-API-Version: compute 2.1
56
+
57
+ In that case, the response will be '2.1'.
58
+
59
+ If provided with a ``legacy_headers`` argument, this is treated as
60
+ a list of additional headers to check for microversions. Some examples of
61
+ headers include::
62
+
63
+ OpenStack-telemetry-api-version: 2.1
64
+ OpenStack-nova-api-version: 2.1
65
+ X-OpenStack-nova-api-version: 2.1
66
+
67
+ If a version string cannot be found, ``None`` will be returned. If
68
+ the input is incorrect usual Python exceptions (ValueError,
69
+ TypeError) are allowed to raise to the caller.
70
+
71
+ parse_version_string
72
+ --------------------
73
+
74
+ A function to turn a version string into a ``Version``, a comparable
75
+ ``namedtuple``::
76
+
77
+ version_tuple = microversion_parse.parse_version_string('2.1')
78
+
79
+ If the provided string is not a valid microversion string, ``TypeError``
80
+ is raised.
81
+
82
+ extract_version
83
+ ---------------
84
+
85
+ Combines ``get_version`` and ``parse_version_string`` to find and validate
86
+ a microversion for a given service type in a collection of headers::
87
+
88
+ version_tuple = microversion_parse.extract_version(
89
+ headers, # a representation of headers, as accepted by get_version
90
+ service_type, # service type identify to match in headers
91
+ versions_list, # an ordered list of strings of version numbers that
92
+ # are the valid versions presented by this service
93
+ )
94
+
95
+ ``latest`` will be translated to whatever the max version is in versions_list.
96
+
97
+ If the found version is not in versions_list a ``ValueError`` is raised.
98
+
99
+ Note that ``extract_version`` does not support ``legacy_headers``.
100
+
101
+ MicroversionMiddleware
102
+ ----------------------
103
+
104
+ A WSGI middleware that can wrap an application that needs to be microversion
105
+ aware. The application will get a WSGI environ with a
106
+ 'SERVICE_TYPE.microversion' key that has a value of the microversion found at
107
+ an 'openstack-api-version' header that matches SERVICE_TYPE. If no header is
108
+ found, the minimum microversion will be set. If the special keyword 'latest' is
109
+ used, the maximum microversion will be set.
110
+
111
+ If the requested microversion is not available a 406 response is returned.
112
+
113
+ If there is an error parsing a provided header, a 400 response is returned.
114
+
115
+ Otherwise the application is called.
116
+
117
+ The middleware is configured when it is created. Three parameters are required:
118
+
119
+ app
120
+ The next WSGI middleware or application in the stack.
121
+
122
+ service_type
123
+ The service type of the application, used to identify microversion headers.
124
+
125
+ versions_list
126
+ An ordered list of legitimate microversions (as strings) for the application.
127
+ It's assumed that any application that is using microversions will have such
128
+ a list for its own housekeeping and documentation.
129
+
130
+ One named parameter is optional:
131
+
132
+ json_error_formatter
133
+ A Webob error formatter that can be used to structure the response when JSON
134
+ is expected.
135
+
136
+ For example::
137
+
138
+ def app():
139
+ app = middleware.MicroversionMiddleware(
140
+ MyWSGIApp(), 'cats', ['1.0', '1.1', '1.2'])
141
+ return app
142
+
143
+
144
+ .. _microversion: http://specs.openstack.org/openstack/api-wg/guidelines/microversion_specification.html
@@ -0,0 +1,131 @@
1
+ # Copyright (C) 2020 Red Hat, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+ # not use this file except in compliance with the License. You may obtain
5
+ # a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+ # License for the specific language governing permissions and limitations
13
+ # under the License.
14
+ #
15
+ # microversion-parse documentation build configuration file, created by
16
+ # sphinx-quickstart on Thu Mar 31 11:40:03 2016.
17
+ #
18
+ # This file is execfile()d with the current directory set to its
19
+ # containing dir.
20
+ #
21
+ # Note that not all possible configuration values are present in this
22
+ # autogenerated file.
23
+ #
24
+ # All configuration values have a default; values that are commented out
25
+ # serve to show the default.
26
+
27
+ # -- General configuration ------------------------------------------------
28
+
29
+ # Add any Sphinx extension module names here, as strings. They can be
30
+ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
31
+ # ones.
32
+ extensions = [
33
+ 'sphinx.ext.autodoc',
34
+ 'sphinx.ext.intersphinx',
35
+ 'openstackdocstheme',
36
+ ]
37
+
38
+ # Add any paths that contain templates here, relative to this directory.
39
+ templates_path = ['_templates']
40
+
41
+ # The suffix(es) of source filenames.
42
+ # You can specify multiple suffix as a list of string:
43
+ # source_suffix = ['.rst', '.md']
44
+ source_suffix = '.rst'
45
+
46
+ # The master toctree document.
47
+ master_doc = 'index'
48
+
49
+ # General information about the project.
50
+ project = 'microversion-parse'
51
+ copyright = '2016, OpenStack'
52
+ author = 'OpenStack'
53
+
54
+ # openstackdocstheme options
55
+ repository_name = 'openstack/microversion-parse'
56
+ bug_project = 'microversion-parse'
57
+ bug_tag = ''
58
+ html_theme = 'openstackdocs'
59
+
60
+ # The language for content autogenerated by Sphinx. Refer to documentation
61
+ # for a list of supported languages.
62
+ #
63
+ # This is also used if you do content translation via gettext catalogs.
64
+ # Usually you set "language" from the command line for these cases.
65
+ # language = None
66
+
67
+ # List of patterns, relative to source directory, that match files and
68
+ # directories to ignore when looking for source files.
69
+ exclude_patterns = []
70
+
71
+ # The name of the Pygments (syntax highlighting) style to use.
72
+ pygments_style = 'sphinx'
73
+
74
+ # If true, `todo` and `todoList` produce output, else they produce nothing.
75
+ todo_include_todos = False
76
+
77
+ # -- Options for HTML output ----------------------------------------------
78
+
79
+ # Output file base name for HTML help builder.
80
+ htmlhelp_basename = 'microversion-parsedoc'
81
+
82
+ # -- Options for LaTeX output ---------------------------------------------
83
+
84
+ latex_elements = {}
85
+ # Grouping the document tree into LaTeX files. List of tuples
86
+ # (source start file, target name, title,
87
+ # author, documentclass [howto, manual, or own class]).
88
+ latex_documents = [
89
+ (
90
+ master_doc,
91
+ 'microversion-parse.tex',
92
+ 'microversion-parse Documentation',
93
+ 'OpenStack',
94
+ 'manual',
95
+ ),
96
+ ]
97
+
98
+ # -- Options for manual page output ---------------------------------------
99
+
100
+ # One entry per manual page. List of tuples
101
+ # (source start file, name, description, authors, manual section).
102
+ man_pages = [
103
+ (
104
+ master_doc,
105
+ 'microversion-parse',
106
+ 'microversion-parse Documentation',
107
+ [author],
108
+ 1,
109
+ )
110
+ ]
111
+
112
+
113
+ # -- Options for Texinfo output -------------------------------------------
114
+
115
+ # Grouping the document tree into Texinfo files. List of tuples
116
+ # (source start file, target name, title, author,
117
+ # dir menu entry, description, category)
118
+ texinfo_documents = [
119
+ (
120
+ master_doc,
121
+ 'microversion-parse',
122
+ 'microversion-parse Documentation',
123
+ author,
124
+ 'microversion-parse',
125
+ 'One line description of project.',
126
+ 'Miscellaneous',
127
+ ),
128
+ ]
129
+
130
+ # Example configuration for intersphinx: refer to the Python standard library.
131
+ intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}
@@ -12,7 +12,11 @@
12
12
  # limitations under the License.
13
13
 
14
14
  import collections
15
+ from collections.abc import Iterable, MutableMapping, Sequence
16
+ from typing import Any, TYPE_CHECKING
15
17
 
18
+ if TYPE_CHECKING:
19
+ from typing_extensions import Self
16
20
 
17
21
  ENVIRON_HTTP_HEADER_FMT = 'http_{}'
18
22
  STANDARD_HEADER = 'openstack-api-version'
@@ -24,19 +28,25 @@ class Version(collections.namedtuple('Version', 'major minor')):
24
28
  Since it is a tuple, it is automatically comparable.
25
29
  """
26
30
 
27
- def __new__(cls, major, minor):
31
+ max_version: tuple[int, int]
32
+ min_version: tuple[int, int]
33
+
34
+ def __new__(cls, major: int, minor: int) -> 'Self':
28
35
  """Add min and max version attributes to the tuple."""
29
- self = super(Version, cls).__new__(cls, major, minor)
36
+ self = super().__new__(cls, major, minor)
30
37
  self.max_version = (-1, 0)
31
38
  self.min_version = (-1, 0)
32
39
  return self
33
40
 
34
- def __str__(self):
35
- return '%s.%s' % (self.major, self.minor)
41
+ def __str__(self) -> str:
42
+ return f'{self.major}.{self.minor}'
36
43
 
37
- def matches(self, min_version=None, max_version=None):
38
- """Is this version within min_version and max_version.
39
- """
44
+ def matches(
45
+ self,
46
+ min_version: tuple[int, int] | None = None,
47
+ max_version: tuple[int, int] | None = None,
48
+ ) -> bool:
49
+ """Is this version within min_version and max_version."""
40
50
  # NOTE(cdent): min_version and max_version are expected
41
51
  # to be set by the code that is creating the Version, if
42
52
  # they are known.
@@ -47,31 +57,34 @@ class Version(collections.namedtuple('Version', 'major minor')):
47
57
  return min_version <= self <= max_version
48
58
 
49
59
 
50
- def get_version(headers, service_type, legacy_headers=None):
60
+ def get_version(
61
+ headers: Iterable[tuple[str, str]] | MutableMapping[str, str],
62
+ service_type: str,
63
+ legacy_headers: Iterable[str] | None = None,
64
+ ) -> str | None:
51
65
  """Parse a microversion out of headers
52
66
 
53
- :param headers: The headers of a request, dict or list
54
- :param service_type: The service type being looked for in the headers
55
- :param legacy_headers: Other headers to look at for a version
56
- :returns: a version string or "latest"
57
- :raises: ValueError
58
-
59
- If headers is not a dict we assume is an iterator of
60
- tuple-like headers, which we will fold into a dict.
67
+ If headers is not a dict we assume is an iterator of tuple-like headers,
68
+ which we will fold into a dict.
61
69
 
62
- The flow is that we first look for the new standard singular
63
- header:
70
+ The flow is that we first look for the new standard singular header:
64
71
 
65
- * openstack-api-version: <service> <version>
72
+ * ``openstack-api-version: <service> <version>``
66
73
 
67
74
  If that's not present we fall back to the headers listed in
68
- legacy_headers. These often look like this:
75
+ ``legacy_headers``. These often look like this:
76
+
77
+ * ``openstack-<service>-api-version: <version>``
78
+ * ``openstack-<legacy>-api-version: <version>``
79
+ * ``x-openstack-<legacy>-api-version: <version>``
69
80
 
70
- * openstack-<service>-api-version: <version>
71
- * openstack-<legacy>-api-version: <version>
72
- * x-openstack-<legacy>-api-version: <version>
81
+ Folded headers are joined by ``,``.
73
82
 
74
- Folded headers are joined by ','.
83
+ :param headers: The headers of a request, dict or list
84
+ :param service_type: The service type being looked for in the headers
85
+ :param legacy_headers: Other headers to look at for a version
86
+ :returns: a version string or "latest"
87
+ :raises: ValueError
75
88
  """
76
89
 
77
90
  folded_headers = fold_headers(headers)
@@ -87,7 +100,9 @@ def get_version(headers, service_type, legacy_headers=None):
87
100
  return None
88
101
 
89
102
 
90
- def check_legacy_headers(headers, legacy_headers):
103
+ def check_legacy_headers(
104
+ headers: MutableMapping[str, str], legacy_headers: Iterable[str]
105
+ ) -> str | None:
91
106
  """Gather values from old headers."""
92
107
  for legacy_header in legacy_headers:
93
108
  try:
@@ -98,7 +113,9 @@ def check_legacy_headers(headers, legacy_headers):
98
113
  return None
99
114
 
100
115
 
101
- def check_standard_header(headers, service_type):
116
+ def check_standard_header(
117
+ headers: MutableMapping[str, str], service_type: str
118
+ ) -> str | None:
102
119
  """Parse the standard header to get value for service."""
103
120
  try:
104
121
  header = _extract_header_value(headers, STANDARD_HEADER)
@@ -112,8 +129,12 @@ def check_standard_header(headers, service_type):
112
129
  except (KeyError, ValueError):
113
130
  return None
114
131
 
132
+ return None
133
+
115
134
 
116
- def fold_headers(headers):
135
+ # we accept Any even though we know this will be a list of 2-item tuples or a
136
+ # dict, in order to avoid reworking logic
137
+ def fold_headers(headers: Any) -> MutableMapping[str, str]:
117
138
  """Turn a list of headers into a folded dict."""
118
139
  # If it behaves like a dict, return it. Webob uses objects which
119
140
  # are not dicts, but behave like them.
@@ -121,6 +142,7 @@ def fold_headers(headers):
121
142
  return dict((k.lower(), v) for k, v in headers.items())
122
143
  except AttributeError:
123
144
  pass
145
+
124
146
  header_dict = collections.defaultdict(list)
125
147
  for header, value in headers:
126
148
  header_dict[header.lower()].append(value.strip())
@@ -132,35 +154,40 @@ def fold_headers(headers):
132
154
  return folded_headers
133
155
 
134
156
 
135
- def headers_from_wsgi_environ(environ):
157
+ def headers_from_wsgi_environ(
158
+ environ: MutableMapping[str, str],
159
+ ) -> dict[str, str]:
136
160
  """Extract all the HTTP_ keys and values from environ to a new dict.
137
161
 
138
- Note that this does not change the keys in any way in the returned
139
- dict. Nor is the incoming environ modified.
162
+ Note that this does not change the keys in any way in the returned dict.
163
+ Nor is the incoming environ modified.
140
164
 
141
165
  :param environ: A PEP 3333 compliant WSGI environ dict.
142
166
  """
143
167
  return {key: environ[key] for key in environ if key.startswith('HTTP_')}
144
168
 
145
169
 
146
- def _extract_header_value(headers, header_name):
170
+ def _extract_header_value(
171
+ headers: MutableMapping[str, str], header_name: str
172
+ ) -> str:
147
173
  """Get the value of a header.
148
174
 
149
- The provided headers is a dict. If a key doesn't exist for
150
- header_name, try using the WSGI environ form of the name.
175
+ The provided headers is a dict. If a key doesn't exist for ``header_name``,
176
+ try using the WSGI environ form of the name.
151
177
 
152
- Raises KeyError if neither key is found.
178
+ :raises: KeyError if neither key is found.
153
179
  """
154
180
  try:
155
181
  value = headers[header_name]
156
182
  except KeyError:
157
183
  wsgi_header_name = ENVIRON_HTTP_HEADER_FMT.format(
158
- header_name.replace('-', '_'))
184
+ header_name.replace('-', '_')
185
+ )
159
186
  value = headers[wsgi_header_name]
160
187
  return value
161
188
 
162
189
 
163
- def parse_version_string(version_string):
190
+ def parse_version_string(version_string: str) -> Version:
164
191
  """Turn a version string into a Version
165
192
 
166
193
  :param version_string: A string of two numerals, X.Y.
@@ -173,28 +200,29 @@ def parse_version_string(version_string):
173
200
  # ValueError, TypeError or AttributeError when the incoming
174
201
  # data is poorly formed but will, however, naturally adapt to
175
202
  # extraneous whitespace.
176
- return Version(*(int(value) for value
177
- in version_string.split('.', 1)))
203
+ return Version(*(int(value) for value in version_string.split('.', 1)))
178
204
  except (ValueError, TypeError, AttributeError) as exc:
179
- raise TypeError('invalid version string: %s; %s' % (
180
- version_string, exc))
205
+ raise TypeError(f'invalid version string: {version_string}; {exc}')
181
206
 
182
207
 
183
- def extract_version(headers, service_type, versions_list):
208
+ def extract_version(
209
+ headers: Iterable[tuple[str, str]] | MutableMapping[str, str],
210
+ service_type: str,
211
+ versions_list: Sequence[str],
212
+ ) -> Version:
184
213
  """Extract the microversion from the headers.
185
214
 
186
- There may be multiple headers and some which don't match our
187
- service.
215
+ There may be multiple headers and some which don't match our service.
188
216
 
189
- If no version is found then the extracted version is the minimum
190
- available version.
217
+ If no version is found then the extracted version is the minimum available
218
+ version.
191
219
 
192
220
  :param headers: Request headers as dict list or WSGI environ
193
- :param service_type: The service_type as a string
221
+ :param service_type: The service type as a string
194
222
  :param versions_list: List of all possible microversions as strings,
195
- sorted from earliest to latest version.
196
- :returns: a Version with the optional min_version and max_version
197
- attributes set.
223
+ sorted from earliest to latest version.
224
+ :returns: a :class:`~Version` with the optional ``min_version`` and
225
+ ``max_version`` attributes set.
198
226
  :raises: ValueError
199
227
  """
200
228
  found_version = get_version(headers, service_type=service_type)
@@ -213,4 +241,4 @@ def extract_version(headers, service_type, versions_list):
213
241
  # to administratively disable a version if we really need to.
214
242
  if str(request_version) in versions_list:
215
243
  return request_version
216
- raise ValueError('Unacceptable version header: %s' % version_string)
244
+ raise ValueError(f'Unacceptable version header: {version_string}')