-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
23 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 8 additions & 5 deletions
13
python-type-hints-multiple-types/03_type_hints_for_callback.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
@@ -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]") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters