Skip to content

fix(version): raise InvalidVersion for non-str pre letters in from_parts - #1241

Merged
henryiii merged 1 commit into
pypa:mainfrom
henryiii:fix/from-parts-bool-validation
Jul 8, 2026
Merged

fix(version): raise InvalidVersion for non-str pre letters in from_parts#1241
henryiii merged 1 commit into
pypa:mainfrom
henryiii:fix/from-parts-bool-validation

Conversation

@henryiii

@henryiii henryiii commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

I removed the bool rejection; I don't think rejecting bools is Pythonic.

🤖 AI text below 🤖

Part of #1239.

_validate_pre (reachable through Version.from_parts and __replace__) normalized the pre letter with .lower() before any type check, so a non-string first element escaped as AttributeError instead of InvalidVersion:

>>> Version.from_parts(release=(1,), pre=(1, 1))
AttributeError: 'int' object has no attribute 'lower'

The letter is now type-checked before it is normalized, so the call raises InvalidVersion like the sibling validators do for bad types. A regression test is included.

This PR originally also rejected bool where the validators expect an int (e.g. post=True producing 1.postTrue); that part was dropped as it is arguably just a quirk of Python rather than a validation gap.

🤖 Generated with Claude Code

`_validate_pre` normalized the letter with `.lower()` before any type
check, so a non-string first element escaped as `AttributeError` instead
of `InvalidVersion`: `Version.from_parts(release=(1,), pre=(1, 1))`.
The letter is now type-checked before it is normalized, matching how the
sibling validators report bad types.

Assisted-by: ClaudeCode:claude-opus-4-8
Assisted-by: ClaudeCode:claude-fable-5
@henryiii
henryiii force-pushed the fix/from-parts-bool-validation branch from b01587d to dd7789a Compare June 10, 2026 23:23
@henryiii henryiii changed the title fix(version): reject bools and non-str pre letters in from_parts validation Jun 10, 2026
@henryiii
henryiii merged commit 9d4927b into pypa:main Jul 8, 2026
59 checks passed
@henryiii
henryiii deleted the fix/from-parts-bool-validation branch July 8, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants