Skip to content

Commit

Permalink
Improve type hint even more (#143)
Browse files Browse the repository at this point in the history
* Reflect that defaults cannot be returned when strict=True

* Use ... for the value of default
  • Loading branch information
ilotoki0804 authored Nov 11, 2024
1 parent be79016 commit 2c7cde2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
30 changes: 24 additions & 6 deletions selectolax/lexbor.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterator, TypeVar, NoReturn, overload
from typing import Any, Iterator, Literal, TypeVar, NoReturn, overload

DefaultT = TypeVar("DefaultT")

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: ...
Expand Down
23 changes: 18 additions & 5 deletions selectolax/parser.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterator, TypeVar, Literal, overload
from typing import Any, Iterator, TypeVar, Literal, overload

DefaultT = TypeVar("DefaultT")

Expand All @@ -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
Expand Down Expand Up @@ -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."""
...
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c7cde2

Please sign in to comment.