From 2c7cde240b2782f92283aa2d88f809456db9888b Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Mon, 11 Nov 2024 15:45:07 +0900 Subject: [PATCH] Improve type hint even more (#143) * Reflect that defaults cannot be returned when strict=True * Use ... for the value of default --- selectolax/lexbor.pyi | 30 ++++++++++++++++++++++++------ selectolax/parser.pyi | 23 ++++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/selectolax/lexbor.pyi b/selectolax/lexbor.pyi index cfdb2f0..6ace54f 100644 --- a/selectolax/lexbor.pyi +++ b/selectolax/lexbor.pyi @@ -1,4 +1,4 @@ -from typing import Iterator, TypeVar, NoReturn, overload +from typing import Any, Iterator, Literal, TypeVar, NoReturn, overload DefaultT = TypeVar("DefaultT") @@ -18,7 +18,7 @@ class LexborAttributes: @overload def get(self, key: str, default: DefaultT) -> DefaultT | str | None: ... @overload - def get(self, key: str, default: None = None) -> str | None: ... + def get(self, key: str, default: None = ...) -> str | None: ... @overload def sget(self, key: str, default: str | DefaultT) -> str | DefaultT: ... @overload @@ -73,9 +73,18 @@ class LexborNode: self, deep: bool = True, separator: str = "", strip: bool = False ) -> str: ... def css(self, query: str) -> list[LexborNode]: ... + @overload + def css_first( + self, query: str, default: Any = ..., strict: Literal[True] = ... + ) -> LexborNode: ... + @overload + def css_first( + self, query: str, default: DefaultT, strict: bool = False + ) -> LexborNode | DefaultT: ... + @overload def css_first( - self, query: str, default: DefaultT | None = None, strict: bool = False - ) -> LexborNode | DefaultT | None: ... + self, query: str, default: None = ..., strict: bool = False + ) -> LexborNode | None: ... def any_css_matches(self, selectors: tuple[str]) -> bool: ... def css_matches(self, selector: str) -> bool: ... @property @@ -124,9 +133,18 @@ class LexborHTMLParser: @property def html(self) -> str | None: ... def css(self, query: str) -> list[LexborNode]: ... + @overload + def css_first( + self, query: str, default: Any = ..., strict: Literal[True] = ... + ) -> LexborNode: ... + @overload + def css_first( + self, query: str, default: DefaultT, strict: bool = False + ) -> LexborNode | DefaultT: ... + @overload def css_first( - self, query: str, default: DefaultT | None = None, strict: bool = False - ) -> LexborNode | DefaultT | None : ... + self, query: str, default: None = ..., strict: bool = False + ) -> LexborNode | None: ... def strip_tags(self, tags: list[str], recursive: bool = False) -> None: ... def select(self, query: str | None = None) -> LexborSelector | None: ... def any_css_matches(self, selectors: tuple[str]) -> bool: ... diff --git a/selectolax/parser.pyi b/selectolax/parser.pyi index 15cb9fd..e08da4f 100644 --- a/selectolax/parser.pyi +++ b/selectolax/parser.pyi @@ -1,4 +1,4 @@ -from typing import Iterator, TypeVar, Literal, overload +from typing import Any, Iterator, TypeVar, Literal, overload DefaultT = TypeVar("DefaultT") @@ -18,7 +18,7 @@ class _Attributes: @overload def get(self, key: str, default: DefaultT) -> DefaultT | str | None: ... @overload - def get(self, key: str, default: None = None) -> str | None: ... + def get(self, key: str, default: None = ...) -> str | None: ... @overload def sget(self, key: str, default: str | DefaultT) -> str | DefaultT: ... @overload @@ -145,12 +145,16 @@ class Node: """Returns True if CSS selector matches a node.""" ... @overload + def css_first( + self, query: str, default: Any = ..., strict: Literal[True] = ... + ) -> Node: ... + @overload def css_first( self, query: str, default: DefaultT, strict: bool = False ) -> Node | DefaultT: ... @overload def css_first( - self, query: str, default: None = None, strict: bool = False + self, query: str, default: None = ..., strict: bool = False ) -> Node | None: """Evaluate CSS selector against current node and its child nodes.""" ... @@ -231,9 +235,18 @@ class HTMLParser: Matches pattern query against HTML tree.""" ... + @overload + def css_first( + self, query: str, default: Any = ..., strict: Literal[True] = ... + ) -> Node: ... + @overload def css_first( - self, query: str, default: DefaultT | None = None, strict: bool = False - ) -> DefaultT | Node: + self, query: str, default: DefaultT, strict: bool = False + ) -> Node | DefaultT: ... + @overload + def css_first( + self, query: str, default: None = ..., strict: bool = False + ) -> Node | None: """Same as css but returns only the first match.""" ... @property