Skip to content

Commit fe51f1c

Browse files
committed
[Python] Fix generic type parameter handling for class methods and ResizeArray Seq compatibility
1 parent 0134cbc commit fe51f1c

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16-
* [Python] Fix `ResizeArray` compatibility with `Seq`/`Array` (by @dbrattli)
16+
* [Python] Fix `ResizeArray` compatibility with `Seq`/`Array` functions (by @dbrattli)
17+
* [Python] Fix `FSharpList` generic type parameter handling for `IEnumerable_1` compatibility (by @dbrattli)
1718

1819
## 5.0.0-alpha.20 - 2025-12-08
1920

src/Fable.Compiler/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16-
* [Python] Fix `ResizeArray` compatibility with `Seq`/`Array` (by @dbrattli)
16+
* [Python] Fix `ResizeArray` compatibility with `Seq`/`Array` functions (by @dbrattli)
17+
* [Python] Fix `FSharpList` generic type parameter handling for `IEnumerable_1` compatibility (by @dbrattli)
1718

1819
## 5.0.0-alpha.19 - 2025-12-08
1920

src/Fable.Transforms/Python/Fable2Python.Transforms.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,9 @@ let calculateTypeParams (com: IPythonCompiler) ctx (info: Fable.MemberFunctionOr
27752775
|> Set.union explicitGenerics
27762776
|> Set.union signatureGenerics
27772777
|> Set.union bodyGenerics
2778+
// Filter out generics that are already defined at class level
2779+
|> Set.difference
2780+
<| ctx.ScopedTypeParams
27782781

27792782
makeFunctionTypeParams com ctx repeatedGenerics
27802783

@@ -3696,6 +3699,15 @@ let rec transformDeclaration (com: IPythonCompiler) ctx (decl: Fable.Declaration
36963699
if classAttributes.Style = ClassStyle.Properties && not classAttributes.Init then
36973700
failwithf "ClassAttributes.Init must be true when ClassAttributes.Style is ClassStyle.Properties"
36983701

3702+
// Add class generic parameters to scoped type params so methods don't redeclare them
3703+
let classGenericParams =
3704+
ent.GenericParameters
3705+
|> List.map (fun p -> p.Name |> Helpers.clean)
3706+
|> Set.ofList
3707+
3708+
let ctx =
3709+
{ ctx with ScopedTypeParams = Set.union ctx.ScopedTypeParams classGenericParams }
3710+
36993711
let classMembers =
37003712
decl.AttachedMembers
37013713
|> List.collect (fun memb ->

src/Fable.Transforms/Python/Replacements.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ let (|TypedArrayCompatible|_|) (com: Compiler) (arrayKind: ArrayKind) t =
4747
let error com msg =
4848
Helper.ConstructorCall(makeIdentExpr "Exception", Any, [ msg ])
4949

50+
/// Wraps ResizeArray (Python list) arguments with to_enumerable for Seq module compatibility.
51+
let wrapResizeArrayToEnumerable com (arg: Expr) =
52+
let rec getInnerType expr =
53+
match expr with
54+
| TypeCast(inner, _) -> getInnerType inner
55+
| _ -> expr.Type
56+
57+
match getInnerType arg with
58+
| Array(_, ResizeArray) -> Helper.LibCall(com, "util", "to_enumerable", arg.Type, [ arg ])
59+
| _ -> arg
60+
5061
let coreModFor =
5162
function
5263
| BclGuid -> "guid"
@@ -1548,6 +1559,9 @@ let formattableString
15481559
let seqModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
15491560
// printfn "seqModule: %A" i.CompiledName
15501561

1562+
// Wrap ResizeArray arguments with to_enumerable for type compatibility
1563+
let args = args |> List.map (wrapResizeArrayToEnumerable com)
1564+
15511565
match i.CompiledName, args with
15521566
| "Cast", [ arg ] -> Some arg // Erase
15531567
| "CreateEvent", [ addHandler; removeHandler; createHandler ] ->

0 commit comments

Comments
 (0)