[Python/Beam] Align random* error messages with .NET and fix pyright errors#27
Merged
ncave merged 1 commit intoncave:randomfrom Apr 5, 2026
Merged
Conversation
…errors Review follow-ups for #4488: Python: - Delete orphaned fable-library-py/fable_library/Array.fs (not in fsproj, declared module ArrayModule, never compiled). - Replace bare PyException("argname") raises in Rust extension with PyValueError carrying descriptive messages aligned with dotnet/fsharp FSharp.Core resource strings. Randomizer validation now matches .NET's dynamic format exactly, including the actual returned value. - Fix core/array.pyi: change Callable[[], float] -> Callable[[], SupportsFloat] for all 15 random *_by signatures. Fable transpiles F# (fun () -> 0.0) as returning Float64, which caused 13 pyright errors in test_array.py. Beam: - Align randomizer error message with .NET's dynamic format via io_lib:format. - Use andalso in random_sample_by guard (idiomatic Erlang). - Refactor random_choices_by: drop lists:map + lists:seq + lists:nth O(n) per-pick and use a tail-recursive choices_loop/5 helper with list_to_tuple for O(1) indexed access, matching the pattern used by shuffle_loop and sample_loop in the same file. Verification: - cargo check fable-library-core: clean - ./build.sh test python: 2233 passed, 0 failed - pyright on temp/tests/Python/: 0 errors (was 13) - ./build.sh test beam: 2390 passed, 0 failed Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Owner
|
Thank you @dbrattli ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Review follow-ups for fable-compiler#4488, scoped to Python and Beam.
JS/TypeScript is intentionally out of scope — see "Follow-ups for fable-library-ts" below.
Python
fable-library-py/fable_library/Array.fs. This file is not inFable.Library.Python.fsprojand declaresmodule ArrayModule, so the newrandomShuffle/randomChoice/randomSamplemembers added to it in [All] Add missing Array, List, and Seq random choice/shuffle/sample members and tests fable-compiler/Fable#4488 were never compiled. Removing the file prevents future confusion.src/array.rs). All new random operations were raising barePyExceptionwith argument-name-only messages ("source","count","randomizer"). Replaced with descriptivePyValueErrormessages aligned with dotnet/fsharp FSharp.Core resource strings. Randomizer validation now matches.NET's dynamic format exactly:"The index is outside the legal range.\nrandomizer returned <value>, should be in range [0.0, 1.0)."core/array.pyi). The newCallable[[], float]signatures caused 13 type errors intest_array.pybecause Fable transpiles F#(fun () -> 0.0)as returningFloat64, notfloat. Changed toCallable[[], SupportsFloat]. PyO3'sextract::<f64>still acceptsFloat64via__float__.Beam
.NET. Same dynamic format as the Python fix, viaio_lib:format.andalsoin guard forrandom_sample_by(idiomatic Erlang).random_choices_by. Replacedlists:map(fun(_) -> ... end, lists:seq(1, Count))(which allocated an intermediate driver list and used O(n)lists:nthper pick) with achoices_loop/5helper that converts the input to a tuple once for O(1) indexed access — matching the pattern already used byshuffle_loopandsample_loopin the same file.Verification
cargo checkon fable-library-core: clean./build.sh test python: 2233 passed, 0 faileduv run pyright --project pyrightconfig.ci.json temp/tests/Python/: 0 errors (was 13)./build.sh test beam: 2390 passed, 0 failedFollow-ups for fable-library-ts (not in this PR)
Verifying against dotnet/fsharp
src/FSharp.Core/local.fsandFSCore.resxsurfaced two places wherefable-library-ts/Array.fsdiverges from actual .NET and should be revisited in a separate PR:1. Randomizer out-of-range validation
.NETusesexecuteRandomizerinMicrosoft.FSharp.Primitives.Basics.Random:invalidArgOutOfRangeFmt argName "{0}\n{1} returned {2}, should be in range [0.0, 1.0)." [|SR.GetString SR.outOfRange; argName; value|]ArgumentOutOfRangeException(notArgumentException).outOfRange="The index is outside the legal range."(notArg_ArgumentOutOfRangeException="Specified argument was out of the range of valid values.").fable-library-ts currently uses the static string
SR.Arg_ArgumentOutOfRangeExceptionwith no value interpolation, and raisesArgumentExceptionrather thanArgumentOutOfRangeException. The Python and Beam implementations in this PR match the.NETformat with dynamic value; fable-library-ts should follow suit.2.
Array.randomChoiceByempty-source check.NETusesLanguagePrimitives.ErrorStrings.InputArrayEmptyString="The input array was empty."(resource keyarrayWasEmpty) — specifically forArray.randomChoiceByonly.List.*andSeq.*still useinputSequenceEmpty. fable-library-ts currently usesinputSequenceEmptyfor all three.Python cannot fix this inside the Rust extension alone because a single
random_choice_byfunction services all three collection paths there (List.randomChoiceBydelegates toArray.randomChoiceByvia F#). Fixing this properly requires either per-collection entry points in the Rust extension or a parameterized message argument. The same design decision applies to Beam. Recommend solving in fable-library-ts first, then propagating.3. Double-to-string formatting
.NET's default double-to-string produces"1"for1.0, while Rust's{}gives"1"and Erlang's~wgives"1.0". The dynamic randomizer message is therefore close but not byte-identical to.NETacross all three targets. Likely not worth fixing unless users are parsing messages.🤖 Generated with Claude Code