Skip to content

Commit 240731f

Browse files
committed
Fix some small type issues.
Use @overload to have a better return type to parse. Use Iterable for QueryParameters to support generators and views. Fixes #156
1 parent b8c9152 commit 240731f

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/hyperlink/_url.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
TypeVar,
4444
Union,
4545
cast,
46+
TYPE_CHECKING,
4647
)
4748
from unicodedata import normalize
4849
from ._socket import inet_pton
@@ -65,7 +66,7 @@
6566
QueryParameters = Union[
6667
Mapping[Text, Optional[Text]],
6768
QueryPairs,
68-
Sequence[Tuple[Text, Optional[Text]]],
69+
Iterable[Tuple[Text, Optional[Text]]],
6970
]
7071
T = TypeVar("T")
7172

@@ -2415,6 +2416,35 @@ def __dir__(self):
24152416
# # End Twisted Compat Code
24162417

24172418

2419+
if TYPE_CHECKING:
2420+
# Add some overloads so that parse gives a better return value.
2421+
# Literal is not available in all pythons so we only bring it in for mypy.
2422+
# Also to remain compatible with 2.7 we use pass instead of ...
2423+
from typing import Literal, overload
2424+
2425+
@overload
2426+
def parse(url):
2427+
# type: (Text) -> DecodedURL
2428+
pass
2429+
2430+
2431+
@overload
2432+
def parse(url, decoded, lazy=False):
2433+
# type: (Text, Literal[True], bool) -> DecodedURL
2434+
pass
2435+
2436+
2437+
@overload
2438+
def parse(url, decoded, lazy=False):
2439+
# type: (Text, Literal[False], bool) -> URL
2440+
pass
2441+
2442+
@overload
2443+
def parse(url, decoded=True, lazy=False):
2444+
# type: (Text, bool, bool) -> Union[URL, DecodedURL]
2445+
pass
2446+
2447+
24182448
def parse(url, decoded=True, lazy=False):
24192449
# type: (Text, bool, bool) -> Union[URL, DecodedURL]
24202450
"""

0 commit comments

Comments
 (0)