From e6ba9580ac7a106e0c175d045dc77b4d6ec92443 Mon Sep 17 00:00:00 2001 From: Geir Arne Hjelle Date: Thu, 19 Oct 2023 23:52:12 +0200 Subject: [PATCH] Final QA (#451) --- .../01_type_hints_for_one_piece_of_data.py | 3 +++ .../02_type_hints_for_multiple_pieces_of_data.py | 3 +++ .../03_type_hints_for_callback.py | 13 ++++++++----- .../04_type_hints_for_factory_function.py | 13 ++++++++----- .../05_type_hints_for_generator.py | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.py b/python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.py index d34f0dba50..8da0a92531 100644 --- a/python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.py +++ b/python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.py @@ -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("@") diff --git a/python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.py b/python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.py index 37a41c0b91..c0bfc13a70 100644 --- a/python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.py +++ b/python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.py @@ -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("@") diff --git a/python-type-hints-multiple-types/03_type_hints_for_callback.py b/python-type-hints-multiple-types/03_type_hints_for_callback.py index 8a269c1225..d65ee6df33 100644 --- a/python-type-hints-multiple-types/03_type_hints_for_callback.py +++ b/python-type-hints-multiple-types/03_type_hints_for_callback.py @@ -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, "claudia@realpython.com") diff --git a/python-type-hints-multiple-types/04_type_hints_for_factory_function.py b/python-type-hints-multiple-types/04_type_hints_for_factory_function.py index 658e20dfa3..c2b8afc52c 100644 --- a/python-type-hints-multiple-types/04_type_hints_for_factory_function.py +++ b/python-type-hints-multiple-types/04_type_hints_for_factory_function.py @@ -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() @@ -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("claudia@realpython.com") diff --git a/python-type-hints-multiple-types/05_type_hints_for_generator.py b/python-type-hints-multiple-types/05_type_hints_for_generator.py index 7013ac2c44..fab68bd2ef 100644 --- a/python-type-hints-multiple-types/05_type_hints_for_generator.py +++ b/python-type-hints-multiple-types/05_type_hints_for_generator.py @@ -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"