-
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.
Merge pull request #441 from realpython/python-type-hints-multiple-types
Code files for Type Hints tutorial
- Loading branch information
Showing
9 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
python-type-hints-multiple-types/01_type_hints_for_one_piece_of_data.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def parse_email(email_address: str) -> str | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username | ||
return None | ||
|
||
|
||
# from typing import Union | ||
# def parse_email(email_address: str) -> Union[str, None]: | ||
# if "@" in email_address: | ||
# username, domain = email_address.split("@") | ||
# return username | ||
# return None |
13 changes: 13 additions & 0 deletions
13
python-type-hints-multiple-types/02_type_hints_for_multiple_pieces_of_data.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
# 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("@") | ||
# return username, domain | ||
# return None |
16 changes: 16 additions & 0 deletions
16
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
|
||
def apply_func(func: Callable[..., Any], *args: Any) -> Any: | ||
return func(*args) | ||
|
||
|
||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
apply_func(parse_email, "[email protected]") |
28 changes: 28 additions & 0 deletions
28
python-type-hints-multiple-types/04_type_hints_for_factory_function.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import functools | ||
import time | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
|
||
def timeit(function: Callable[..., Any]) -> Callable[..., Any]: | ||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
start = time.perf_counter() | ||
result = function(*args, **kwargs) | ||
end = time.perf_counter() | ||
print(f"{function.__name__}() finished in {end - start:.10f}s") | ||
return result | ||
|
||
return wrapper | ||
|
||
|
||
@timeit | ||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None | ||
|
||
|
||
username, domain = parse_email("[email protected]") | ||
print(username, domain) |
37 changes: 37 additions & 0 deletions
37
python-type-hints-multiple-types/05_type_hints_for_generator.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from collections.abc import Generator, Iterator | ||
|
||
|
||
def parse_email() -> Generator[tuple[str, str], str, str]: | ||
sent = yield "", "" | ||
while sent != "": | ||
if "@" in sent: | ||
username, domain = sent.split("@") | ||
sent = yield username, domain | ||
else: | ||
sent = yield "invalid email" | ||
return "Done" | ||
|
||
|
||
generator = parse_email() | ||
next(generator) | ||
generator.send("[email protected]") | ||
generator.send("realpython") | ||
try: | ||
generator.send("") | ||
except StopIteration as ex: | ||
print(ex.value) | ||
|
||
|
||
def parse_emails(emails: list[str]) -> Iterator[tuple[str, str]]: | ||
for email in emails: | ||
if "@" in email: | ||
username, domain = email.split("@") | ||
yield username, domain | ||
|
||
|
||
# from collections.abc import Iterable | ||
# def parse_emails(emails: Iterable[str]) -> Iterable[tuple[str, str]]: | ||
# for email in emails: | ||
# if "@" in email: | ||
# username, domain = email.split("@") | ||
# yield username, domain |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import TypeAlias | ||
|
||
|
||
EmailComponents: TypeAlias = tuple[str, str] | None | ||
|
||
|
||
def parse_email(email_address: str) -> EmailComponents: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def parse_email(email_address: str) -> tuple[str, str] | None: | ||
if "@" in email_address: | ||
username, domain = email_address.split("@") | ||
return username, domain | ||
return None |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# How to Use Type Hints for Multiple Return Types in Python | ||
|
||
This folder contains sample code for the [How to Use Type Hints for Multiple Return Types in Python](https://realpython.com/python-type-hints-multiple-types/) tutorial on Real Python. | ||
|
||
## Installation | ||
|
||
Some of the code requires the following third-party library: | ||
|
||
- [`mypy`](https://pypi.org/project/mypy/) | ||
|
||
To install it into a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/), type the following commands: | ||
|
||
```shell | ||
$ python3 -m venv venv/ | ||
$ source venv/bin/activate | ||
(venv) $ python -m pip install -r requirements.txt | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mypy==1.5.1 | ||
mypy-extensions==1.0.0 | ||
typing_extensions==4.8.0 |