Skip to content

fix: enforce runtime casting semantics - #311

Merged
dbrattli merged 1 commit into
mainfrom
fix/casting-semantics
Nov 12, 2026
Merged

dbrattli merged 1 commit into
mainfrom
fix/casting-semantics

Conversation

@dbrattli

Copy link
Copy Markdown
Owner

Invalid downcasts previously relied on assertions that disappeared under python -O, and try_downcast(list[int], ["wrong"]) could succeed by discarding generic arguments. Use explicit runtime checks: incompatible upcast and downcast values raise TypeError, while try_downcast returns None for a value mismatch. Unsupported parameterized targets now raise TypeError rather than claiming unchecked element types.

Keep the uncurried upcast(Base, value) API and infer its return type from the target alone. Document that this form checks compatibility at runtime, with a TODO to adopt upcast(Base)(value) for static enforcement in the next major release. Successful casts preserve object identity.

Compatibility changes: failed downcasts raise TypeError instead of AssertionError; invalid upcasts now fail; parameterized targets, including list[Any], are rejected. Add identity, inference, negative typing, and optimized-interpreter regressions, and update the casting documentation.

Verification: .venv/bin/pytest -q and PATH="$PWD/.venv/bin:$PATH" .venv/bin/pre-commit run --all-files.

@dbrattli
dbrattli merged commit 2504851 into main Nov 12, 2026
6 checks passed
@dbrattli
dbrattli deleted the fix/casting-semantics branch November 12, 2026 18:22
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