Add pyright as a second type checker #14

Merged
alexander merged 5 commits from add-pyright into develop 2026-12-01 15:18:18 +01:00
7 changed files with 91 additions and 46 deletions

View file

@ -3,7 +3,7 @@
By default, `uv run nox` runs the sessions that assure code quality:
- "format": Format source files with `autoflake`, `black`, `flynt`, `isort`, and `ruff`
- "lint": Lint source files with `flake8`, `isort`, `mypy`, and `ruff`
- "lint": Lint source files with `flake8`, `isort`, `mypy`, `pyright`, and `ruff`
- "test": Run the test suite with `pytest`
- "doctest": Check if code snippets in the docstrings run
@ -93,14 +93,19 @@ SCRIPTS_LOCATION = "scripts/"
TESTS_LOCATION = "tests/"
# Locations that `mypy` and `pyright` always check as a whole
TYPECHECK_LOCATIONS = (
NOXFILE_LOCATION,
SCRIPTS_LOCATION,
PACKAGE_LOCATION,
)
# Locations of all *.py files
SRC_LOCATIONS = (
f"{DOCS_LOCATION}conf.py",
"migrations/env.py",
"migrations/versions/",
NOXFILE_LOCATION,
SCRIPTS_LOCATION,
PACKAGE_LOCATION,
*TYPECHECK_LOCATIONS,
TESTS_LOCATION,
)
@ -183,13 +188,13 @@ def format_code(session: nox.Session) -> None:
@nox.session(name="lint", python=MAIN_PYTHON)
def lint_code(session: nox.Session) -> None:
"""Lint source files with `flake8`, `isort`, `mypy`, and `ruff`.
"""Lint source files with `flake8`, `isort`, `mypy`, `pyright`, and `ruff`.
If no extra arguments are provided, all source files are linted.
Otherwise, they are interpreted as paths the linters work on recursively.
`mypy` always checks the noxfile, the scripts, and the package regardless
because it needs the whole program to follow types across modules.
`mypy` and `pyright` always check the noxfile, the scripts, and the package
regardless because they need the whole program to follow types across modules.
"""
start(session)
@ -233,6 +238,7 @@ def lint_code(session: nox.Session) -> None:
"pandas-vet",
"pep8-naming",
"pydoclint[flake8]",
"pyright[nodejs]",
"ruff",
"tryceratops",
)
@ -252,7 +258,10 @@ def lint_code(session: nox.Session) -> None:
session.run("ruff", "check", *locations)
session.run("mypy", "--version")
session.run("mypy", NOXFILE_LOCATION, SCRIPTS_LOCATION, PACKAGE_LOCATION)
session.run("mypy", *TYPECHECK_LOCATIONS)
session.run("pyright", "--version")
session.run("pyright", *TYPECHECK_LOCATIONS)
@nox.session(name="test", python=MAIN_PYTHON)

View file

@ -123,6 +123,7 @@ dev = [
"pandas-vet (>=2023.8.2)",
"pep8-naming (>=0.15.1,<0.16.0)",
"pydoclint[flake8] (>=0.12.0,<0.13.0)",
"pyright[nodejs] (>=1.1.414,<2.0.0)",
"ruff (>=0.17.6,<0.18.0)",
"tryceratops (>=2.4.1,<3.0.0)",
]
@ -525,6 +526,14 @@ ignore_missing_imports = true
[tool.pyright]
# Source: https://microsoft.github.io/pyright/#/configuration
# Treat libraries without type annotations as `Any`, like `mypy` does
useLibraryCodeForTypes = false
[tool.pytest.ini_options]
# Source: https://docs.pytest.org/en/stable/reference/reference.html#ini-options-ref

View file

@ -237,7 +237,7 @@ class Path(meta.Base):
# get from A to B. As a step's "start_location" may equal the previous step's
# "end_location", we use an `OrderedSet` to find the unique latitude-longitude
# pairs that make up the path from `.first_address` to `.second_address`.
steps: ordered_set.OrderedSet[tuple[float, float]] = ordered_set.OrderedSet()
steps: ordered_set.OrderedSet[tuple[float, float]] = ordered_set.OrderedSet([])
for step in route["steps"]:
steps.add(
(step["start_location"]["lat"], step["start_location"]["lng"]),

View file

@ -222,40 +222,32 @@ class Forecast(meta.Base):
"""
forecasts = []
for timestamp_idx in data.index:
start_at = timestamp_idx.to_pydatetime()
actual = int(data.loc[timestamp_idx, "actual"])
prediction = float(round(data.loc[timestamp_idx, "prediction"], 5))
# Explicit type casting. SQLAlchemy does not convert
# `float('NaN')`s into plain `None`s.
low80 = data.loc[timestamp_idx, "low80"]
high80 = data.loc[timestamp_idx, "high80"]
low95 = data.loc[timestamp_idx, "low95"]
high95 = data.loc[timestamp_idx, "high95"]
low80 = None if math.isnan(low80) else float(round(low80, 5))
high80 = None if math.isnan(high80) else float(round(high80, 5))
low95 = None if math.isnan(low95) else float(round(low95, 5))
high95 = None if math.isnan(high95) else float(round(high95, 5))
rows = data[["actual", "prediction", "low80", "high80", "low95", "high95"]]
for (
timestamp_idx,
actual,
prediction,
low80,
high80,
low95,
high95,
) in rows.itertuples(name=None):
forecasts.append(
cls(
pixel=pixel,
start_at=start_at,
start_at=timestamp_idx.to_pydatetime(),
time_step=time_step,
train_horizon=train_horizon,
model=model,
actual=actual,
prediction=prediction,
low80=low80,
high80=high80,
low95=low95,
high95=high95,
actual=int(actual),
prediction=float(round(prediction, 5)),
# Explicit type casting. SQLAlchemy does not convert
# `float('NaN')`s into plain `None`s.
low80=None if math.isnan(low80) else float(round(low80, 5)),
high80=None if math.isnan(high80) else float(round(high80, 5)),
low95=None if math.isnan(low95) else float(round(low95, 5)),
high95=None if math.isnan(high95) else float(round(high95, 5)),
),
)

View file

@ -2,6 +2,7 @@
import abc
import datetime as dt
import typing as t
import pandas as pd
@ -20,16 +21,14 @@ class ForecastingModelABC(abc.ABC):
"""
self._order_history = order_history
@property
@abc.abstractmethod
def name(self) -> str:
"""The name of the model.
name: t.ClassVar[str]
"""The name of the model.
Used to identify `Forecast`s of the same `*Model` in the database.
So, these must be chosen carefully and must not be changed later on!
Used to identify `Forecast`s of the same `*Model` in the database.
So, these must be chosen carefully and must not be changed later on!
Example: "hets" or "varima" for tactical demand forecasting
"""
Example: "hets" or "varima" for tactical demand forecasting
"""
@abc.abstractmethod
def predict(

View file

@ -40,7 +40,7 @@ class TestGenericForecastingModelProperties:
assert model is not None
def test_model_has_a_name(self, model_cls, order_history):
"""Access the `*Model.name` property."""
"""Access the `*Model.name` class variable."""
model = model_cls(order_history=order_history)
result = model.name

36
uv.lock generated
View file

@ -2337,6 +2337,22 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/2d/6a/8a9ac7d5e2f7cf53c459bfef90ebc17a2f8cd54b665af96799d4c534840e/nodeenv-1.12.0-py3-none-any.whl", hash = "sha256:80644d65c10cc2be12e3e4b54b3367ff288c7ad24ae75c2dcc65454fc7449d4d", size = 39442, upload-time = "2026-10-31T19:03:17.427Z" },
]
[[package]]
name = "nodejs-wheel-binaries"
version = "24.19.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/c0/76/7e97195e14346598565a0de4ca8bdbd5e634b3fb5b1ba590b7b1b89f8a63/nodejs_wheel_binaries-24.19.0.tar.gz", hash = "sha256:db217eef8cab8551667863379b08db4d9067403f6cbbe87481eb40edceb8aa9b", size = 8058, upload-time = "2026-08-19T21:47:19.671Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/8c/52/0774b52c7be8151ad9d5aff44edc100c3f13d6d8eb3765f63ffa40e69fe8/nodejs_wheel_binaries-24.19.0-py2.py3-none-macosx_13_0_arm64.whl", hash = "sha256:e12cbfd69089504e42fb14194ce734a9dcf3eb38c820ca63dd511d36fb964e9c", size = 56047203, upload-time = "2026-08-19T21:46:43.448Z" },
{ url = "https://files.pythonhosted.org/packages/67/3a/4fdbbfecf2c23d52c0e3f68de7f7c1b3c97a26d328c69c5f6c49c48e340e/nodejs_wheel_binaries-24.19.0-py2.py3-none-macosx_13_0_x86_64.whl", hash = "sha256:1c890adf4b7e6556ccc1ca66c866bb81884b6c9a581dee4e530dc7f78fb9d514", size = 56219459, upload-time = "2026-08-19T21:46:48.45Z" },
{ url = "https://files.pythonhosted.org/packages/5f/a8/0147149415195c59b8a72a594916bfb80d6be4d586f9fbfda313889e0efc/nodejs_wheel_binaries-24.19.0-py2.py3-none-manylinux_2_28_aarch64.whl", hash = "sha256:4e029dadfae1295876063c96b236f673487e8c27379fe146c1e2250283520227", size = 60588256, upload-time = "2026-08-19T21:46:53.298Z" },
{ url = "https://files.pythonhosted.org/packages/f4/89/6631d0982353da1bb7bc00bb1988f702822c62b42634f57999ee53b5c337/nodejs_wheel_binaries-24.19.0-py2.py3-none-manylinux_2_28_x86_64.whl", hash = "sha256:4196a947bcc883f2003ab101762d729f3e99b5e86b75bd09151563403e2eceb8", size = 61123607, upload-time = "2026-08-19T21:46:58.117Z" },
{ url = "https://files.pythonhosted.org/packages/32/a2/fa30f0841e4602995782e124359f9b910c7b481d98decf61ef0b2fc3ebfb/nodejs_wheel_binaries-24.19.0-py2.py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:352e048ab4dd35e7de5f338d1cc4fcbf77a0e93da30bf7336a8217ee246b31d7", size = 62632842, upload-time = "2026-08-19T21:47:03.42Z" },
{ url = "https://files.pythonhosted.org/packages/18/01/22d97ca72213f66cc386ee638db30c2e62757fdf761c6029074bced83d1c/nodejs_wheel_binaries-24.19.0-py2.py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:28d078b2ced9e2069516e652dc4b1380e7a1a7f2d3934eccd1611586d283ba4c", size = 63250653, upload-time = "2026-08-19T21:47:07.938Z" },
{ url = "https://files.pythonhosted.org/packages/88/d1/e3be8fa327a795bcaf7a19cd84299e338a7bce32ff0665fdce9cfa22573c/nodejs_wheel_binaries-24.19.0-py2.py3-none-win_amd64.whl", hash = "sha256:67e3abeb9c3830cae8c8487ae8a2af7cc27dfa75af06145cee5ca7d1857c81bd", size = 42448503, upload-time = "2026-08-19T21:47:12.093Z" },
{ url = "https://files.pythonhosted.org/packages/1d/37/34cf28ba1691a060174948a9927fe61091982d6048b2e403071a9acce443/nodejs_wheel_binaries-24.19.0-py2.py3-none-win_arm64.whl", hash = "sha256:d9074c665ea68b04e183d82482c86dc907d3a9bd15eb6cf85542cb785266bb36", size = 40090155, upload-time = "2026-08-19T21:47:16.032Z" },
]
[[package]]
name = "notebook-shim"
version = "0.2.4"
@ -2886,6 +2902,24 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/38/bb/d215ee7c73b61497b28a5503f9f53523f294fcc936762b7caf90e0c1c2b5/pyparsing-3.3.3-py3-none-any.whl", hash = "sha256:ece8c00a69cf01b45d0b1dedabb469c90d8caf996d4fda40f147627a122849a4", size = 126420, upload-time = "2026-09-20T20:59:04.025Z" },
]
[[package]]
name = "pyright"
version = "1.1.414"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "nodeenv" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/e1/1b/244c7b710031ada80f27e579ec20d28a2285dfc318fed0339866b1047f12/pyright-1.1.414.tar.gz", hash = "sha256:523c0a97c60da6333234955c277730c9cf4f5bd6d5399e7b7d2b0fc5d3599524", size = 4154638, upload-time = "2026-09-10T12:26:53.181Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/d7/ba/18b6e682ead424ad24bcc134339ae5d1b931cd9ae260540592a058a91279/pyright-1.1.414-py3-none-any.whl", hash = "sha256:2a6b4b3298c9eec174c5ed83bd338de6eee82df2992f3e1930e6199d381be36f", size = 6225049, upload-time = "2026-09-10T12:26:51.427Z" },
]
[package.optional-dependencies]
nodejs = [
{ name = "nodejs-wheel-binaries" },
]
[[package]]
name = "pytest"
version = "9.2.0"
@ -3990,6 +4024,7 @@ dev = [
{ name = "pep8-naming" },
{ name = "pre-commit" },
{ name = "pydoclint", extra = ["flake8"] },
{ name = "pyright", extra = ["nodejs"] },
{ name = "ruff" },
{ name = "tryceratops" },
]
@ -4071,6 +4106,7 @@ dev = [
{ name = "pep8-naming", specifier = ">=0.15.1,<0.16.0" },
{ name = "pre-commit", specifier = ">=4.7.0,<5.0.0" },
{ name = "pydoclint", extras = ["flake8"], specifier = ">=0.12.0,<0.13.0" },
{ name = "pyright", extras = ["nodejs"], specifier = ">=1.1.414,<2.0.0" },
{ name = "ruff", specifier = ">=0.17.6,<0.18.0" },
{ name = "tryceratops", specifier = ">=2.4.1,<3.0.0" },
]