Skip to content

test: add type annotations to tests and the demo - #130

Merged
proxi merged 2 commits into
mainfrom
proxi/type-tests
Dec 1, 2026
Merged

proxi merged 2 commits into
mainfrom
proxi/type-tests

Conversation

@proxi

@proxi proxi commented Dec 1, 2026

Copy link
Copy Markdown
Member

Fourth step towards mypy --disallow-untyped-defs, after #127, #128 and #129.

Annotates the last 82 untyped functions: 81 in test/ and one in examples/cohort_demo.py. mypy --disallow-untyped-defs bitmapist test examples scripts now passes (82 → 0). The flag itself is not turned on here; that is the next PR.

Mostly mechanical:

  • 53 argument-less tests and one fixture only gain -> None.
  • Fixtures and helpers in test/conftest.py get real signatures.
  • log_message in the demo gets *args: Any.

Not mechanical, all in test/test_cohort.py. Annotating a test makes mypy check its body, which surfaced these:

  • as_percent=1 → as_percent=True in two places (the parameter is bool).
  • _utc(*args) takes explicit year, month, day, hour, minute. With *args, mypy cannot rule out tzinfo being passed twice.
  • _first_row(**selects) takes explicit select1, select2, select1b, select2b. With **selects, mypy matches the values against every other parameter of get_dates_data too.
  • Two assert isinstance(event, WeekEvents) before reading .year / .week, since _weeks_events_fn returns GenericPeriodEvents | BitOperation.
  • One # type: ignore[override] on the Frozen.now clock stub: it returns a plain datetime where datetime.now() promises Self.

Also test_period_start_end spells out the five event classes as a union, because from_date is not declared on GenericPeriodEvents.

mypy passes in the project env and with the pre-commit hook's setup, with and without the flag. I could not run the test suite locally (no Redis backend), so tests are verified by CI only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XGjgWiXoieBLCLiXckPSe9

The last 82 untyped functions: 81 in test/ and one in
examples/cohort_demo.py. 'mypy --disallow-untyped-defs' now passes for
the whole repository.

Most tests only gain '-> None'. Annotating them makes mypy check their
bodies, which needed a few changes in test_cohort.py:

- as_percent=1 becomes as_percent=True.
- _utc() and _first_row() take explicit parameters, not *args/**kwargs.
- Two isinstance(event, WeekEvents) asserts before reading .year/.week.
- A 'type: ignore[override]' on the Frozen.now clock stub, which returns
  a plain datetime where datetime.now() promises Self.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XGjgWiXoieBLCLiXckPSe9
@proxi proxi added the 🚢 Ship PR requires passing checks before merge, but no independent human review. label Dec 1, 2026
@proxi
proxi marked this pull request as ready for review December 1, 2026 13:09

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds type annotations to the last 82 untyped functions (81 in tests, one in the cohort demo) so mypy --disallow-untyped-defs passes over bitmapist test examples scripts, including a few small mypy-driven fixes in test_cohort.py (as_percent=True, explicit-argument helpers, isinstance guards, one targeted type: ignore). No inline issues were flagged; the annotations were verified against the actual source signatures, the helper signature changes preserve call-site behavior, and no security concerns surfaced. One small reuse note: _first_row could use the existing EventNames alias from bitmapist.cohort instead of re-spelling its str | list[str] unions.

I also included a few optional follow-up notes in the details below.

Optional follow-up notes (2)
  • P3 test/test_cohort.py:227: bitmapist.cohort already defines EventNames = str | Iterable[str] | None (line 93) and get_dates_data uses it for these same four parameters. Reusing it here — from bitmapist.cohort import EventNames (the test file already imports private helpers like _chart_data and _compare) and annotating select1: EventNames, select2: EventNames, select1b: EventNames = None, select2b: EventNames = None — avoids a second, narrower definition of the same concept. The inline str | list[str] also rejects tuples/sets that get_dates_data accepts, so a future caller would have to widen the annotation again.
  • P3 test/test_cohort.py:2: tzinfo is only used in an annotation (Frozen.now), and this file has no from __future__ import annotations. Every other annotation-only import in the repo lives in an if TYPE_CHECKING: block (Callable/Iterator in test/conftest.py, TypeAlias/Pipeline in bitmapist/__init__.py), so ruff's flake8-type-checking rules (TCH, enabled in pyproject.toml and run by the pre-commit hook) will ask for this one too. Note that moving it as-is would break frozen_now at runtime, since the annotation is evaluated when Frozen is defined — quote it as well (def now(cls, tz: "tzinfo | None" = None)) or add a targeted # noqa: TC003 if you'd rather keep the import at runtime.

Share Feedback • Review Logs • Trace

get_dates_data() already uses EventNames for the same four parameters.
The helper spelled its own, narrower 'str | list[str]'.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XGjgWiXoieBLCLiXckPSe9
@proxi

proxi commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

This comment was posted by Claude on Janusz's behalf, as part of addressing the doistbot review.

bitmapist.cohort already defines EventNames = str | Iterable[str] | None [...] Reusing it here [...] avoids a second, narrower definition of the same concept.

Changed in 6059ddc: _first_row now imports EventNames from bitmapist.cohort and uses it for all four parameters.

tzinfo is only used in an annotation (Frozen.now), and this file has no from __future__ import annotations. [...] ruff's flake8-type-checking rules [...] will ask for this one too.

Not changed. ruff does not flag it: ruff check --select TC test/test_cohort.py passes, and so does the full ruff check with the repo config. Without from __future__ import annotations the annotation is evaluated at runtime, so ruff treats the import as a runtime one. As the note itself says, moving it under TYPE_CHECKING would break frozen_now, so the plain import is the simplest correct form.

@proxi
proxi merged commit 27fe8f7 into main Dec 1, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚢 Ship PR requires passing checks before merge, but no independent human review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants