Skip to content

Commit

Permalink
Merge pull request #441 from realpython/python-type-hints-multiple-types
Browse files Browse the repository at this point in the history
Code files for Type Hints tutorial
  • Loading branch information
KateFinegan authored Oct 18, 2023
2 parents 5b06cd6 + 9d97eb8 commit b8e4ae8
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 0 deletions.
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
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 python-type-hints-multiple-types/03_type_hints_for_callback.py
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]")
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 python-type-hints-multiple-types/05_type_hints_for_generator.py
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
11 changes: 11 additions & 0 deletions python-type-hints-multiple-types/06_type_aliases.py
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
5 changes: 5 additions & 0 deletions python-type-hints-multiple-types/07_static_type_checking.py
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
17 changes: 17 additions & 0 deletions python-type-hints-multiple-types/README.md
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
```
3 changes: 3 additions & 0 deletions python-type-hints-multiple-types/requirements.txt
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

0 comments on commit b8e4ae8

Please sign in to comment.