Skip to content

Commit 8b74278

Browse files
authored
fix: Improve Accept.best_match typing (#4487)
* Improve `Accept.best_match` typing It used to be this way: ```python .best_match(['application/json'], default='application/json') # -> reveals `Optional[str]` as the return type ``` However, default is `str` and this is a typing error. Now it works as expected when passing `best_match(['application/json'], default='application/json')` and reveals `str` as the return type. Full testing is here: https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=bf3a081c25f31ddfd2da6614fb59deff * Update headers.py * Update headers.py * Update headers.py
1 parent bcea856 commit 8b74278

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

litestar/datastructures/headers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Optional,
1313
Union,
1414
cast,
15+
overload,
1516
)
1617

1718
import msgspec
@@ -440,6 +441,12 @@ def __getitem__(self, key: int) -> str:
440441
def __iter__(self) -> Iterator[str]:
441442
return map(str, self._accepted_types)
442443

444+
@overload
445+
def best_match(self, provided_types: list[str], default: None = None) -> Optional[str]: ...
446+
447+
@overload
448+
def best_match(self, provided_types: list[str], default: str) -> str: ...
449+
443450
def best_match(self, provided_types: list[str], default: Optional[str] = None) -> Optional[str]:
444451
"""Find the best matching media type for the request.
445452

0 commit comments

Comments
 (0)