From 625aaba621115657859a1c6ae889f4ced28834e6 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Sun, 16 May 2021 22:45:21 +0200 Subject: [PATCH] Enhance type of headers.get method. When given a default value that is not None, the get method always returns a string. --- CHANGES.rst | 1 + src/werkzeug/datastructures.pyi | 2 ++ src/werkzeug/sansio/request.py | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 85ccae878..f8a2d0f93 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,6 +14,7 @@ Unreleased decorated with it report the correct type. :issue:`2113` - Fix multipart parsing bug when boundary contains special regex characters. :issue:`2125` +- Enhance type annotation for ``headers.get``. :issue:`2128` Version 2.0.0 diff --git a/src/werkzeug/datastructures.pyi b/src/werkzeug/datastructures.pyi index 7279d3a73..292e1a262 100644 --- a/src/werkzeug/datastructures.pyi +++ b/src/werkzeug/datastructures.pyi @@ -220,6 +220,8 @@ class Headers(Dict[str, str]): def __getitem__(self, key: str, _get_mode: Literal[True] = ...) -> str: ... def __eq__(self, other: object) -> bool: ... @overload # type: ignore + def get(self, key: str, default: str) -> str: ... + @overload def get(self, key: str, default: Optional[str] = None) -> Optional[str]: ... @overload def get( diff --git a/src/werkzeug/sansio/request.py b/src/werkzeug/sansio/request.py index 6f2bceb16..2c21a2134 100644 --- a/src/werkzeug/sansio/request.py +++ b/src/werkzeug/sansio/request.py @@ -491,7 +491,7 @@ def user_agent(self) -> UserAgent: Werkzeug 2.1. A ``UserAgent`` subclass must be set to parse data from the string. """ - return self.user_agent_class(t.cast(str, self.headers.get("User-Agent", ""))) + return self.user_agent_class(self.headers.get("User-Agent", "")) # Authorization