From 7043ec23db10c338687ecdb79bcc04c3cc5920c3 Mon Sep 17 00:00:00 2001 From: Kevin Mustelier Date: Sat, 19 Oct 2024 09:46:45 -0500 Subject: [PATCH] Improve typing for BS4 element.Tag's `get` and `get_attribute_list`. (#12840) --- stubs/beautifulsoup4/bs4/element.pyi | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/stubs/beautifulsoup4/bs4/element.pyi b/stubs/beautifulsoup4/bs4/element.pyi index 6e9cf6d5f9ab..cf5d39a4d4ef 100644 --- a/stubs/beautifulsoup4/bs4/element.pyi +++ b/stubs/beautifulsoup4/bs4/element.pyi @@ -27,6 +27,7 @@ class ContentMetaAttributeValue(AttributeValueWithCharsetSubstitution): def __new__(cls, original_value): ... def encode(self, encoding: str) -> str: ... # type: ignore[override] # incompatible with str +_T = TypeVar("_T") _PageElementT = TypeVar("_PageElementT", bound=PageElement) _SimpleStrainable: TypeAlias = str | bool | None | bytes | Pattern[str] | Callable[[str], bool] | Callable[[Tag], bool] _Strainable: TypeAlias = _SimpleStrainable | Iterable[_SimpleStrainable] @@ -276,8 +277,16 @@ class Tag(PageElement): def clear(self, decompose: bool = False) -> None: ... def smooth(self) -> None: ... def index(self, element: PageElement) -> int: ... - def get(self, key: str, default: str | list[str] | None = None) -> str | list[str] | None: ... - def get_attribute_list(self, key: str, default: str | list[str] | None = None) -> list[str]: ... + @overload + def get(self, key: str, default: None = None) -> str | list[str] | None: ... + @overload + def get(self, key: str, default: _T) -> str | list[str] | _T: ... + @overload + def get_attribute_list(self, key: str, default: None = None) -> list[str | None]: ... + @overload + def get_attribute_list(self, key: str, default: list[_T]) -> list[str | _T]: ... + @overload + def get_attribute_list(self, key: str, default: _T) -> list[str | _T]: ... def has_attr(self, key: str) -> bool: ... def __hash__(self) -> int: ... def __getitem__(self, key: str) -> str | list[str]: ...