@@ -71,6 +71,27 @@ let getGenericArgs (typ: Fable.Type) : Fable.Type list =
7171let containsGenericParams ( t : Fable.Type ) =
7272 FSharp2Fable.Util.getGenParamNames [ t ] |> List.isEmpty |> not
7373
74+ /// Check if a type contains Option nested inside a container (Array, List, Tuple).
75+ /// When Options are inside invariant containers, we must use Option[ T] form consistently
76+ /// to match function signatures that use generic type parameters.
77+ let rec hasOptionInContainer ( t : Fable.Type ) : bool =
78+ match t with
79+ | Fable.Array( elementType, _) -> containsOptionType elementType
80+ | Fable.List elementType -> containsOptionType elementType
81+ | Fable.Tuple( genArgs, _) -> genArgs |> List.exists containsOptionType
82+ | Fable.DeclaredType(_, genArgs) -> genArgs |> List.exists containsOptionType
83+ | _ -> false
84+
85+ /// Check if a type is or contains an Option type
86+ and containsOptionType ( t : Fable.Type ) : bool =
87+ match t with
88+ | Fable.Option _ -> true
89+ | Fable.Array( elementType, _) -> containsOptionType elementType
90+ | Fable.List elementType -> containsOptionType elementType
91+ | Fable.Tuple( genArgs, _) -> genArgs |> List.exists containsOptionType
92+ | Fable.DeclaredType(_, genArgs) -> genArgs |> List.exists containsOptionType
93+ | _ -> false
94+
7495/// Check if a type is a callable type (Lambda or Delegate)
7596let isCallableType ( t : Fable.Type ) =
7697 match t with
@@ -472,6 +493,8 @@ let makeImportTypeAnnotation com ctx genArgs moduleName typeName =
472493let makeEntityTypeAnnotation com ctx ( entRef : Fable.EntityRef ) genArgs repeatedGenerics =
473494 // printfn "DeclaredType: %A" entRef.FullName
474495 match entRef.FullName, genArgs with
496+ // Python's BaseException - used for catch-all exception handlers
497+ | " BaseException" , _ -> Expression.name " BaseException" , []
475498 | Types.result, _ ->
476499 let resolved , stmts = resolveGenerics com ctx genArgs repeatedGenerics
477500 fableModuleAnnotation com ctx " result" " FSharpResult_2" resolved, stmts
@@ -630,8 +653,13 @@ let makeBuiltinTypeAnnotation com ctx typ repeatedGenerics kind =
630653 match kind with
631654 | Replacements.Util.BclGuid -> stdlibModuleTypeHint com ctx " uuid" " UUID" [] repeatedGenerics
632655 | Replacements.Util.FSharpReference genArg ->
633- let resolved , stmts = resolveGenerics com ctx [ genArg ] repeatedGenerics
634- fableModuleAnnotation com ctx " core" " FSharpRef" resolved, stmts
656+ // In F#, struct instance method's `this` parameter is represented as inref<StructType>,
657+ // but in Python the struct is passed directly, not wrapped in FSharpRef.
658+ if isInRefOrAnyType com typ then
659+ typeAnnotation com ctx repeatedGenerics genArg
660+ else
661+ let resolved , stmts = resolveGenerics com ctx [ genArg ] repeatedGenerics
662+ fableModuleAnnotation com ctx " core" " FSharpRef" resolved, stmts
635663 (*
636664 | Replacements.Util.BclTimeSpan -> NumberTypeAnnotation
637665 | Replacements.Util.BclDateTime -> makeSimpleTypeAnnotation com ctx "Date"
0 commit comments