Skip to content

feat: add type annotations to the core module - #128

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

proxi merged 2 commits into
mainfrom
proxi/type-core-module

Conversation

@proxi

@proxi proxi commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Second step towards mypy --disallow-untyped-defs, after #127 (System alias).

All 48 untyped functions in bitmapist/__init__.py are now annotated: mypy --disallow-untyped-defs bitmapist/__init__.py passes. Untyped functions left in the repo: 142 → 94 (bitmapist.cohort, tests, one in examples).

What to look at:

  • Mixins declare the system and redis_key attributes they read. Annotations only: no class attributes are created and the MRO is unchanged.
  • HasRedisKey is a new protocol for what bit operations combine, with redis_key as a read-only property. It matches the existing hasattr(..., "redis_key") check in BitOperation, so duck-typed objects still type-check.
  • BitOperation.__init__ needs two casts, because hasattr() does not narrow a union for mypy. It now assigns self.system once after the branch, not in both arms.
  • from_date(), delta(), next(), prev() return Self.
  • not_none() gets two overloads, so not_none(year, now.year) is int and not int | None.

Not done on purpose: from_date is not declared on GenericPeriodEvents. HourEvents.from_date only accepts datetime, the others accept date | datetime, so a shared signature would be wrong for one of them.

No runtime change intended. mypy passes in the project env and with the pre-commit hook's setup. 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

All 48 untyped functions in bitmapist/__init__.py are now annotated, so
'mypy --disallow-untyped-defs bitmapist/__init__.py' passes.

- Mixins declare the 'system' and 'redis_key' attributes they read.
  These are annotations only: no class attributes, no change to the MRO.
- HasRedisKey is a protocol for what bit operations combine, matching
  the hasattr(..., 'redis_key') check in BitOperation.
- from_date(), delta(), next() and prev() return Self.
- not_none() gets overloads, so not_none(year, now.year) is an int.

No runtime change. BitOperation.__init__ assigns self.system once
instead of in both branches, and needs two casts because hasattr() does
not narrow a union for mypy.

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 12:39
@proxi
proxi changed the base branch from proxi/system-type-alias to main December 1, 2026 12:40

@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 all 48 previously untyped functions in bitmapist/__init__.py, including a new HasRedisKey protocol, Self returns on the period/navigational methods, and not_none overloads — an annotation-only change with no runtime behavior change. No inline issues were flagged; the annotations check out as accurate against the existing runtime behavior, and CI remains the verification point for the test suite since it couldn't be run locally without a Redis backend.

I also left one optional follow-up note in the details below.

Optional follow-up note (1)
  • P3 bitmapist/init.py:112: redis_key is declared as a settable protocol member, so mypy only accepts objects whose redis_key is writable. A duck-typed object exposing it as a read-only @property passes the existing hasattr(self, "redis_key") check at runtime but won't satisfy HasRedisKey. Declaring it read-only (@property def redis_key(self) -> str: ...) matches what bit operations actually do with it.

Share Feedback • Review Logs • Trace

Bit operations only read redis_key. Declared as a settable attribute,
the protocol rejected objects that expose it as a read-only property,
although they pass the hasattr() check at runtime.

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.

redis_key is declared as a settable protocol member, so mypy only accepts objects whose redis_key is writable. [...] Declaring it read-only (@property def redis_key(self) -> str: ...) matches what bit operations actually do with it.

Changed in b8c6810: HasRedisKey.redis_key is now a read-only property. Checked with mypy: an object exposing redis_key as a @property is accepted by BitOpAnd(...) and event & obj, the event classes (plain attribute) still satisfy the protocol, and an object without redis_key is still rejected.

@proxi
proxi merged commit 3b14ab3 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