Skip to content

Commit

Permalink
Fix some small type issues.
Browse files Browse the repository at this point in the history
Use @overload to have a better return type to parse.
Use Iterable for QueryParameters to support generators and views.

Fixes #156
  • Loading branch information
euresti committed Mar 16, 2021
1 parent b8c9152 commit 240731f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/hyperlink/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
TypeVar,
Union,
cast,
TYPE_CHECKING,
)
from unicodedata import normalize
from ._socket import inet_pton
Expand All @@ -65,7 +66,7 @@
QueryParameters = Union[
Mapping[Text, Optional[Text]],
QueryPairs,
Sequence[Tuple[Text, Optional[Text]]],
Iterable[Tuple[Text, Optional[Text]]],
]
T = TypeVar("T")

Expand Down Expand Up @@ -2415,6 +2416,35 @@ def __dir__(self):
# # End Twisted Compat Code


if TYPE_CHECKING:
# Add some overloads so that parse gives a better return value.
# Literal is not available in all pythons so we only bring it in for mypy.
# Also to remain compatible with 2.7 we use pass instead of ...
from typing import Literal, overload

@overload
def parse(url):
# type: (Text) -> DecodedURL
pass


@overload
def parse(url, decoded, lazy=False):
# type: (Text, Literal[True], bool) -> DecodedURL
pass


@overload
def parse(url, decoded, lazy=False):
# type: (Text, Literal[False], bool) -> URL
pass

@overload
def parse(url, decoded=True, lazy=False):
# type: (Text, bool, bool) -> Union[URL, DecodedURL]
pass


def parse(url, decoded=True, lazy=False):
# type: (Text, bool, bool) -> Union[URL, DecodedURL]
"""
Expand Down

0 comments on commit 240731f

Please sign in to comment.