diff --git a/falcon/vendor/mimeparse/mimeparse.py b/falcon/vendor/mimeparse/mimeparse.py index 0218553cf..6485fcccd 100755 --- a/falcon/vendor/mimeparse/mimeparse.py +++ b/falcon/vendor/mimeparse/mimeparse.py @@ -1,4 +1,9 @@ import cgi +from typing import Dict +from typing import Iterable +from typing import Iterator +from typing import List +from typing import Tuple __version__ = '1.6.0' __author__ = 'Joe Gregorio' @@ -6,12 +11,14 @@ __license__ = 'MIT License' __credits__ = '' +Range = Tuple[str, str, Dict[str, str]] + class MimeTypeParseException(ValueError): pass -def parse_mime_type(mime_type): +def parse_mime_type(mime_type: str) -> Range: """Parses a mime-type into its component parts. Carves up a mime-type and returns a tuple of the (type, subtype, params) @@ -39,7 +46,7 @@ def parse_mime_type(mime_type): return (type.strip(), subtype.strip(), params) -def parse_media_range(range): +def parse_media_range(range: str) -> Range: """Parse a media-range into its component parts. Carves up a media range and returns a tuple of the (type, subtype, @@ -56,7 +63,7 @@ def parse_media_range(range): :rtype: (str,str,dict) """ (type, subtype, params) = parse_mime_type(range) - params.setdefault('q', params.pop('Q', None)) # q is case insensitive + params.setdefault('q', params.pop('Q', '')) # q is case insensitive try: if not params['q'] or not 0 <= float(params['q']) <= 1: params['q'] = '1' @@ -66,7 +73,9 @@ def parse_media_range(range): return (type, subtype, params) -def quality_and_fitness_parsed(mime_type, parsed_ranges): +def quality_and_fitness_parsed( + mime_type: str , parsed_ranges: List[Range] +) -> Tuple[float, float]: """Find the best match for a mime-type amongst parsed media-ranges. Find the best match for a given mime-type against a list of media_ranges @@ -77,8 +86,8 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges): :rtype: (float,int) """ - best_fitness = -1 - best_fit_q = 0 + best_fitness = -1.0 + best_fit_q = 0.0 (target_type, target_subtype, target_params) = \ parse_media_range(mime_type) @@ -98,7 +107,7 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges): if type_match and subtype_match: # 100 points if the type matches w/o a wildcard - fitness = type == target_type and 100 or 0 + fitness: float = type == target_type and 100 or 0 # 10 points if the subtype matches w/o a wildcard fitness += subtype == target_subtype and 10 or 0 @@ -115,12 +124,12 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges): if fitness > best_fitness: best_fitness = fitness - best_fit_q = params['q'] + best_fit_q = float(params['q']) - return float(best_fit_q), best_fitness + return best_fit_q, best_fitness -def quality_parsed(mime_type, parsed_ranges): +def quality_parsed(mime_type: str, parsed_ranges: List[Range]) -> float: """Find the best match for a mime-type amongst parsed media-ranges. Find the best match for a given mime-type against a list of media_ranges @@ -135,7 +144,7 @@ def quality_parsed(mime_type, parsed_ranges): return quality_and_fitness_parsed(mime_type, parsed_ranges)[0] -def quality(mime_type, ranges): +def quality(mime_type: str, ranges: str) -> float: """Return the quality ('q') of a mime-type against a list of media-ranges. Returns the quality 'q' of a mime-type when compared against the @@ -152,7 +161,7 @@ def quality(mime_type, ranges): return quality_parsed(mime_type, parsed_ranges) -def best_match(supported, header): +def best_match(supported: Iterable[str], header: str) -> str: """Return mime-type with the highest quality ('q') from list of candidates. Takes a list of supported mime-types and finds the best match for all the @@ -184,7 +193,7 @@ def best_match(supported, header): return weighted_matches[-1][0][0] and weighted_matches[-1][2] or '' -def _filter_blank(i): +def _filter_blank(i: Iterable[str]) -> Iterator[str]: """Return all non-empty items in the list.""" for s in i: if s.strip(): diff --git a/pyproject.toml b/pyproject.toml index 78afc0730..ab9144786 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,7 @@ "falcon.middleware", "falcon.redirects", "falcon.stream", + "falcon.vendor.*", ] disallow_untyped_defs = true