Skip to content

Commit

Permalink
Final QA (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
gahjelle authored Oct 19, 2023
1 parent b8e4ae8 commit e6ba958
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def parse_email(email_address: str) -> str | None:
return None


# %% Python 3.9 and earlier

# from typing import Union
#
# def parse_email(email_address: str) -> Union[str, None]:
# if "@" in email_address:
# username, domain = email_address.split("@")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def parse_email(email_address: str) -> tuple[str, str] | None:
return None


# %% Python 3.9 and earlier

# from typing import Tuple, Union
#
# def parse_email(email_address: str) -> Union[Tuple[str, str], None]:
# if "@" in email_address:
# username, domain = email_address.split("@")
Expand Down
13 changes: 8 additions & 5 deletions python-type-hints-multiple-types/03_type_hints_for_callback.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from collections.abc import Callable
from typing import Any
from typing import ParamSpec, TypeVar

P = ParamSpec("P")
T = TypeVar("T")

def apply_func(func: Callable[..., Any], *args: Any) -> Any:
return func(*args)

def apply_func(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
return func(*args, **kwargs)

def parse_email(email_address: str) -> tuple[str, str] | None:

def parse_email(email_address: str) -> tuple[str, str]:
if "@" in email_address:
username, domain = email_address.split("@")
return username, domain
return None
return "", ""


apply_func(parse_email, "[email protected]")
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import functools
import time
from collections.abc import Callable
from typing import Any
from typing import ParamSpec, TypeVar

P = ParamSpec("P")
T = TypeVar("T")

def timeit(function: Callable[..., Any]) -> Callable[..., Any]:

def timeit(function: Callable[P, T]) -> Callable[P, T]:
@functools.wraps(function)
def wrapper(*args, **kwargs):
def wrapper(*args: P.args, **kwargs: P.kwargs):
start = time.perf_counter()
result = function(*args, **kwargs)
end = time.perf_counter()
Expand All @@ -17,11 +20,11 @@ def wrapper(*args, **kwargs):


@timeit
def parse_email(email_address: str) -> tuple[str, str] | None:
def parse_email(email_address: str) -> tuple[str, str]:
if "@" in email_address:
username, domain = email_address.split("@")
return username, domain
return None
return "", ""


username, domain = parse_email("[email protected]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def parse_email() -> Generator[tuple[str, str], str, str]:
username, domain = sent.split("@")
sent = yield username, domain
else:
sent = yield "invalid email"
sent = yield "ERROR", "invalid email"
return "Done"


Expand Down

0 comments on commit e6ba958

Please sign in to comment.