Welcome to pytest-pyright
This project was created for internal use within another project of mine, support will be minimal.
Why Should You Use it?
pytest-pyright was created to ensure that complex types are correctly constrained, i.e will raise an error if used incorrectly.
if your project makes use of any complex types you should find some value out pytest-pyright.
How Does it Work?
pytest-pyright collects all python files in a
typesafety directory relative to wherever you run pytest from, the location of this directory can be changed with the
--pyright-dir pytest option.
The collected files are all type checked individually using pyright, if no errors ocurr then the all tests will pass!
However this is not very useful, keep reading to find out how to assert that an error is raised and check variable types.
Checking for Errors
You can check that certain lines in a file raise an error by adding a comment, e.g.
from typing import Optional def main(string: Optional[str]) -> None: print(string.split('.')) # E: "split" is not a known member of "None"
You can check that the type of a certain variable is what you expect it to be, e.g.
from typing import Union def main(string_or_int: Union[str, int]) -> None: if isinstance(string_or_int, str): reveal_type(string_or_int) # T: str else: reveal_type(string_or_int) # T: int
If any unexpected errors are raised by pyright, any calls to
reveal_type are missing a type comment or any other error, the test will fail and something like the following will be displayed.
_____________________ pyright: unexpected_error.py _____________________ 1 | from typing import TypedDict 2 | 3 | 4 | class Data(TypedDict, total=False): 5 | points: int 6 | 7 | 8 | def data_handler(data: Data) -> None: 9 | print(data['points']) E | Unexpected error: Could not access item in TypedDict