Skip to content

feat(python): give System exceptions native Python bases - #5040

Merged
dbrattli merged 2 commits into
mainfrom
feat/python-native-exception-bases
Oct 4, 2026
Merged

dbrattli merged 2 commits into
mainfrom
feat/python-native-exception-bases

Conversation

@dbrattli

@dbrattli dbrattli commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Python except TimeoutError currently misses a generated .NET TimeoutException. The same interoperability gap applies to other .NET exceptions with a natural Python counterpart. This change keeps distinct .NET exception classes while making Python's builtin handlers catch them.

Changes

  • Copy System.fs into the Python runtime and update its project reference.
  • Centralize ExceptionBase and the native bases in exception_bases.py. Keep types.ExceptionBase as a re-export of the same class for existing generated code and Python imports; new compiler output imports the canonical module.
  • Add native bases that retain ExceptionBase ancestry, message formatting, and inner_exception while also inheriting the corresponding Python builtin.
  • Preserve the existing .NET reflection ancestry by hiding the native bridge classes in generated reflection metadata.
  • Add F# regressions for the mappings, specific .NET catches, payloads, native timeout catches, reflection, and identity across both ExceptionBase import paths.
.NET exception Python base
ArgumentException and its subclasses, FormatException ValueError
IndexOutOfRangeException IndexError
InvalidOperationException RuntimeError
DivideByZeroException ZeroDivisionError
OverflowException OverflowError
NotImplementedException NotImplementedError
OutOfMemoryException MemoryError
TimeoutException TimeoutError

Related to the Python exception compatibility discussion in #5035. Existing async entry points keep their current exception contracts.

Validation

  • Full .NET Python suite: 2,399 passed.
  • Full generated Python suite on Python 3.14: 2,493 passed, with two warnings from existing cancellation timer tests.
  • Full generated Pyright reports 34 errors / 45 warnings, with no increase from the existing baseline. The changed hand-written runtime files have no diagnostics.
  • The native mapping and timeout-handler regressions fail against main's runtime and pass with this change. Confirmed the reflection bridge interrupted ancestry before the generator adjustment.
  • All 43 constructor exports and signatures match main.
  • Targeted inheritance/payload probes pass on Python 3.12.13, 3.13.8, and 3.14.0.
  • Compiler/runtime compilation, Fantomas, native helper Ruff lint/format, and diff checks pass.

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 34
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_hash_set.py 18 Excluded
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 2 Excluded

@dbrattli
dbrattli marked this pull request as ready for review October 4, 2026 11:30
@dbrattli
dbrattli merged commit 4ee3158 into main Oct 4, 2026
44 checks passed
@dbrattli
dbrattli deleted the feat/python-native-exception-bases branch October 4, 2026 12:13
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