Skip to content

Correct clamp type annotations for callable formatters and None - #458

Open
lohitredddy wants to merge 1 commit into
python-humanize:mainfrom
lohitredddy:fix-clamp-type-annotations
Open

lohitredddy wants to merge 1 commit into
python-humanize:mainfrom
lohitredddy:fix-clamp-type-annotations

Conversation

@lohitredddy

Copy link
Copy Markdown

clamp() documents callable formatters and a None input, but its annotations reject both uses. For example, mypy rejects clamp(1, format=intword, floor=1e6) and clamp(None) even though both already work at runtime.

Changes proposed in this pull request:

  • Accept string and callable formatters in the type annotations.
  • Add overloads so numeric calls retain a precise str return type and None calls return None; clarify the existing behavior in the docstring.
  • Add typing.assert_type regression coverage alongside runtime assertions for those calls.

The function body is unchanged. The new assertions produce three mypy errors against the original annotations and pass with this change.

Validation on Python 3.12:

  • Number tests: 242 passed.
  • Full test suite: 747 passed, 110 skipped (compiled locale catalogs are unavailable in this checkout).
  • mypy: no issues in 14 source files, run with --num-workers 0.
  • Ruff and Black checks on both changed files, plus git diff --check: passed.

Other Python versions and the full pre-commit hook collection were not run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant