Visitar URL original
Preserve constructor TypeError in Version.replace by Zhuyuyangyy · Pull Request #501 · python-semver/python-semver · GitHub
Skip to content

Preserve constructor TypeError in Version.replace - #501

Merged
tomschr merged 2 commits into
python-semver:masterfrom
Zhuyuyangyy:fix/replace-preserve-typeerror
Oct 27, 2026
Merged

tomschr merged 2 commits into
python-semver:masterfrom
Zhuyuyangyy:fix/replace-preserve-typeerror

Conversation

@Zhuyuyangyy

Copy link
Copy Markdown
Contributor

Problem

Version.replace() catches every TypeError from the constructor and rewrites it as an unexpected-keyword error, even when all keyword names are valid:

from semver import Version
Version.parse("1.2.3").replace(major=None)
# TypeError: replace() got 0 unexpected keyword argument(s):

The actual problem is the value passed to int(), not an unknown keyword. The same happens for minor and patch, or an object that cannot be converted to an integer. This is a diagnostic fix; the exception type and accepted inputs do not change.

Change

Re-raise the original constructor exception when there are no unknown keys. Keep the existing custom error for genuinely unknown keywords, and keep subclass construction unchanged.

Add six regression cases covering the three numeric components and two invalid value types. Each checks that the constructor's original error is retained; all six fail before the fix.

Validation

Python 3.12.3:

  • python -m pytest -q: 517 passed, 1 skipped, 100% reported statement/branch coverage.
  • git diff --check: passed.
  • Ruff on the two touched files reports the same 60 pre-existing findings on the baseline and patched versions; no unrelated lint cleanup is included.

AI assistance was used to prepare this patch and tests. The failure and test results above were reproduced locally.

@tomschr tomschr 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.

Thank you @Zhuyuyangyy! Much appreciated! ❤️

@tomschr
tomschr merged commit 0bd62d4 into python-semver:master Oct 27, 2026
17 checks passed
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.

2 participants