Repository navigation
test: add type annotations to tests and the demo - #130
Conversation
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
doistbot
left a comment
There was a problem hiding this comment.
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)
test/test_cohort.py:227:
bitmapist.cohortalready definesEventNames = str | Iterable[str] | None(line 93) andget_dates_datauses it for these same four parameters. Reusing it here —from bitmapist.cohort import EventNames(the test file already imports private helpers like_chart_dataand_compare) and annotatingselect1: EventNames, select2: EventNames, select1b: EventNames = None, select2b: EventNames = None— avoids a second, narrower definition of the same concept. The inlinestr | list[str]also rejects tuples/sets thatget_dates_dataaccepts, so a future caller would have to widen the annotation again.test/test_cohort.py:2:
tzinfois only used in an annotation (Frozen.now), and this file has nofrom __future__ import annotations. Every other annotation-only import in the repo lives in anif TYPE_CHECKING:block (Callable/Iteratorintest/conftest.py,TypeAlias/Pipelineinbitmapist/__init__.py), so ruff's flake8-type-checking rules (TCH, enabled inpyproject.tomland run by the pre-commit hook) will ask for this one too. Note that moving it as-is would breakfrozen_nowat runtime, since the annotation is evaluated whenFrozenis defined — quote it as well (def now(cls, tz: "tzinfo | None" = None)) or add a targeted# noqa: TC003if you'd rather keep the import at runtime.
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
|
This comment was posted by Claude on Janusz's behalf, as part of addressing the doistbot review.
Changed in 6059ddc:
Not changed. ruff does not flag it: |
Fourth step towards
mypy --disallow-untyped-defs, after #127, #128 and #129.Annotates the last 82 untyped functions: 81 in
test/and one inexamples/cohort_demo.py.mypy --disallow-untyped-defs bitmapist test examples scriptsnow passes (82 → 0). The flag itself is not turned on here; that is the next PR.Mostly mechanical:
-> None.test/conftest.pyget real signatures.log_messagein 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=Truein two places (the parameter isbool)._utc(*args)takes explicityear, month, day, hour, minute. With*args, mypy cannot rule outtzinfobeing passed twice._first_row(**selects)takes explicitselect1,select2,select1b,select2b. With**selects, mypy matches the values against every other parameter ofget_dates_datatoo.assert isinstance(event, WeekEvents)before reading.year/.week, since_weeks_events_fnreturnsGenericPeriodEvents | BitOperation.# type: ignore[override]on theFrozen.nowclock stub: it returns a plaindatetimewheredatetime.now()promisesSelf.Also
test_period_start_endspells out the five event classes as a union, becausefrom_dateis not declared onGenericPeriodEvents.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