Skip to content

Commit

Permalink
Add mypy examples
Browse files Browse the repository at this point in the history
  • Loading branch information
klieret committed Nov 2, 2022
1 parent 8cb50f4 commit 9b559ea
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/typed_python/example_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def f(x: float) -> float:
y = x**2
return y
12 changes: 12 additions & 0 deletions examples/typed_python/example_2.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions examples/typed_python/example_protocol.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions examples/typed_python/readme.md
Original file line number Diff line number Diff line change
@@ -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?
15 changes: 10 additions & 5 deletions slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


&nbsp;

Reminder: https://scikit-hep.org/developer/pytest is a great place to look for tips!

---
layout: two-cols
---
Expand Down Expand Up @@ -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
Expand All @@ -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?
</v-click>
<v-click>

How do we use it? (requires `pipx install mypy`)

```bash
mypy --strict tmp.py
Expand All @@ -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).

</v-click>

---
Expand Down

0 comments on commit 9b559ea

Please sign in to comment.