Skip to content

Quickstart

Note

This tutorial does not actually do anything meaningful and is just meant to show how to get pytest-pyright up and running.

In a new directory, create and activate a virtualenv

python3 -m venv .venv
source .venv/bin/activate

Install dependencies

pip install -U pytest pytest-pyright

Create a typesafety directory

mkdir typesafety

Create a new file in your editor at typesafety/quickstart.py and copy paste the content below:

from typing import TypedDict


class Data(TypedDict, total=False):
    points: int


def data_handler(data: Data) -> None:
    points = data.get('points')
    reveal_type(points)  # T: int | None

    points = data.get('points', 0)
    reveal_type(points)  # T: int

    # as we specified total=False in the Data type, the points key might be missing
    print(data['points'])  # E: Could not access item in TypedDict

Run pytest

pytest
============================test session starts ============================
platform darwin -- Python 3.10.0b4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/robert/tmp/pytest-pyright-quickstart
plugins: pyright-0.0.1
collected 1 item

typesafety/quickstart.py .                                            [100%]

============================ 1 passed in 2.43s =============================