diff --git a/examples/typed_python/example_1.py b/examples/typed_python/example_1.py new file mode 100644 index 0000000..e7f191c --- /dev/null +++ b/examples/typed_python/example_1.py @@ -0,0 +1,3 @@ +def f(x: float) -> float: + y = x**2 + return y diff --git a/examples/typed_python/example_2.py b/examples/typed_python/example_2.py new file mode 100644 index 0000000..3c9b70c --- /dev/null +++ b/examples/typed_python/example_2.py @@ -0,0 +1,12 @@ +from __future__ import annotations + + +def f(x: int) -> list[int]: + return list(range(x)) + + +def g(x: str | int) -> str: + if isinstance(x, str): + return x.lower() + else: + return x diff --git a/examples/typed_python/example_protocol.py b/examples/typed_python/example_protocol.py new file mode 100644 index 0000000..847ddd8 --- /dev/null +++ b/examples/typed_python/example_protocol.py @@ -0,0 +1,17 @@ +import typing +from typing import Protocol # or typing_extensions for < 3.8 + +class Duck(Protocol): + def quack(self) -> str: + ... + +def pester_duck(a_duck: Duck) -> None: + print(a_duck.quack()) + +class MyDuck: + def quack(self) -> str: + return "quack" + +# Check explicitly that MyDuck is implementing the Duck protocol +if typing.TYPE_CHECKING: + _: Duck = typing.cast(MyDuck, None) diff --git a/examples/typed_python/readme.md b/examples/typed_python/readme.md new file mode 100644 index 0000000..1a15b87 --- /dev/null +++ b/examples/typed_python/readme.md @@ -0,0 +1,11 @@ +# Typed python + +Install `mypy`: + +```bash +pipx install mypy +``` + +Run `mypy example_1.py`, etc. + +Some of the files have mistakes in them, can you fix them? diff --git a/slides.md b/slides.md index a04c8ac..e68d884 100644 --- a/slides.md +++ b/slides.md @@ -677,8 +677,6 @@ To run: `pip install pytest` and then `pytest` to discover & run them all. ## First tip: your `project.toml` file -Reminder: https://scikit-hep.org/developer/pytest is a great place to look for tips! - ```toml [tool.pytest.ini_options] minversion = "6.0" # minimal version of pytest @@ -702,6 +700,10 @@ testpaths = ["tests"] # search for tests in "test" directory * You can also add `breakpoint()` in your code to get into a debugger +  + +Reminder: https://scikit-hep.org/developer/pytest is a great place to look for tips! + --- layout: two-cols --- @@ -784,8 +786,6 @@ System IO, GUIs, hardware, slow processes; there are a lot of things that are ha * **đŸ’¡Solution:** Add types and run a type checker. -Typed code looks like this: - ```python def f(x: float) -> float: y = x**2 @@ -795,7 +795,10 @@ def f(x: float) -> float: * Functions always have types in and out * Variable definitions rarely have types -How do we use it? + + + +How do we use it? (requires `pipx install mypy`) ```bash mypy --strict tmp.py @@ -804,6 +807,8 @@ mypy --strict tmp.py Some type checkers: MyPy (Python), Pyright (Microsoft), Pytype (Google), or Pyre (Meta). +đŸ‘‰ Example files available [here](https://github.com/klieret/everything-you-didnt-now-you-needed/tree/main/examples/typed_python). + ---