Repository navigation
feat: add type annotations to the core module - #128
Conversation
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
doistbot
left a comment
There was a problem hiding this comment.
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)
bitmapist/init.py:112:
redis_keyis declared as a settable protocol member, so mypy only accepts objects whoseredis_keyis writable. A duck-typed object exposing it as a read-only@propertypasses the existinghasattr(self, "redis_key")check at runtime but won't satisfyHasRedisKey. Declaring it read-only (@property def redis_key(self) -> str: ...) matches what bit operations actually do with it.
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
|
This comment was posted by Claude on Janusz's behalf, as part of addressing the doistbot review.
Changed in b8c6810: |
Second step towards
mypy --disallow-untyped-defs, after #127 (Systemalias).All 48 untyped functions in
bitmapist/__init__.pyare now annotated:mypy --disallow-untyped-defs bitmapist/__init__.pypasses. Untyped functions left in the repo: 142 → 94 (bitmapist.cohort, tests, one inexamples).What to look at:
systemandredis_keyattributes they read. Annotations only: no class attributes are created and the MRO is unchanged.HasRedisKeyis a new protocol for what bit operations combine, withredis_keyas a read-only property. It matches the existinghasattr(..., "redis_key")check inBitOperation, so duck-typed objects still type-check.BitOperation.__init__needs two casts, becausehasattr()does not narrow a union for mypy. It now assignsself.systemonce after the branch, not in both arms.from_date(),delta(),next(),prev()returnSelf.not_none()gets two overloads, sonot_none(year, now.year)isintand notint | None.Not done on purpose:
from_dateis not declared onGenericPeriodEvents.HourEvents.from_dateonly acceptsdatetime, the others acceptdate | 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