fix: add typed overloads to Selectors.get() for proper default type inference

Type checkers now correctly infer the return type based on the default value:
- .get() → TextHandler | None
- .get("") → TextHandler | str
- .get(0) → TextHandler | int
This commit is contained in:
Karim shoair
2026-02-13 17:30:55 +02:00
parent 1c2c7c55bf
commit a62330d2f1
+9 -1
View File
@@ -23,6 +23,7 @@ from scrapling.core._types import (
List,
Tuple,
Union,
TypeVar,
Pattern,
Callable,
Literal,
@@ -51,6 +52,7 @@ _whitelisted = {
"class_": "class",
"for_": "for",
}
_T = TypeVar("_T")
# Pre-compiled selectors for efficiency
_find_all_elements = XPath(".//*")
_find_all_elements_with_spaces = XPath(
@@ -1337,7 +1339,13 @@ class Selectors(List[Selector]):
"""
return self.__class__([element for element in self if func(element)])
def get(self, default: Optional[TextHandler] = None) -> Union[TextHandler, None]:
@overload
def get(self) -> Optional[TextHandler]: ...
@overload
def get(self, default: _T) -> Union[TextHandler, _T]: ...
def get(self, default=None):
"""Returns the serialized string of the first element, or ``default`` if empty.
:param default: the default value to return if the current list is empty
"""