|
| 1 | +# Pydantic Example (Importing Python Models) |
| 2 | + |
| 3 | +This example demonstrates how to **import and use Pydantic models defined in Python** from F# code using Fable.Python. |
| 4 | + |
| 5 | +> **Note:** This is the opposite approach from the [FastAPI example](../fastapi/), which shows how to **define Pydantic models in F#** that compile to Python. |
| 6 | +
|
| 7 | +## Use Cases |
| 8 | + |
| 9 | +This pattern is useful when you want to: |
| 10 | + |
| 11 | +- Use models generated from OpenAPI specs or other tools |
| 12 | +- Use models maintained by a Python team |
| 13 | +- Integrate with an existing Python codebase |
| 14 | +- Share models between Python and F# code |
| 15 | + |
| 16 | +## Project Structure |
| 17 | + |
| 18 | +```txt |
| 19 | +examples/pydantic/ |
| 20 | +├── App.fs # F# application using the models |
| 21 | +├── Models.fs # F# bindings for Python models |
| 22 | +├── models.py # Handwritten Python Pydantic models |
| 23 | +├── PydanticExample.fsproj |
| 24 | +├── build/ # Generated Python code (git-ignored) |
| 25 | +└── README.md |
| 26 | +``` |
| 27 | + |
| 28 | +## How It Works |
| 29 | + |
| 30 | +### 1. Define Pydantic Models in Python |
| 31 | + |
| 32 | +Create your models in `models.py`: |
| 33 | + |
| 34 | +```python |
| 35 | +from pydantic import BaseModel |
| 36 | + |
| 37 | +class User(BaseModel): |
| 38 | + id: int |
| 39 | + name: str |
| 40 | + email: str | None = None |
| 41 | + age: int | None = None |
| 42 | +``` |
| 43 | + |
| 44 | +### 2. Create F# Bindings |
| 45 | + |
| 46 | +Import the Python models using `[<Import>]`: |
| 47 | + |
| 48 | +```fsharp |
| 49 | +[<Import("User", "models")>] |
| 50 | +type User = |
| 51 | + abstract id: int with get, set |
| 52 | + abstract name: string with get, set |
| 53 | + abstract email: string option with get, set |
| 54 | + abstract age: int option with get, set |
| 55 | +``` |
| 56 | + |
| 57 | +### 3. Create Constructors |
| 58 | + |
| 59 | +Use `[<Emit>]` to call the Python constructor with named arguments: |
| 60 | + |
| 61 | +```fsharp |
| 62 | +[<RequireQualifiedAccess>] |
| 63 | +module User = |
| 64 | + [<Import("User", "models")>] |
| 65 | + [<Emit("$0(id=$1, name=$2, email=$3, age=$4)")>] |
| 66 | + let create (id: int) (name: string) (email: string option) (age: int option) : User = nativeOnly |
| 67 | +``` |
| 68 | + |
| 69 | +### 4. Use in F# Code |
| 70 | + |
| 71 | +```fsharp |
| 72 | +let user = User.create 1 "Alice" (Some "[email protected]") (Some 30) |
| 73 | +printfn "User: %s (id=%d)" user.name user.id |
| 74 | +``` |
| 75 | + |
| 76 | +## Building |
| 77 | + |
| 78 | +From the repository root: |
| 79 | + |
| 80 | +```bash |
| 81 | +# Using justfile |
| 82 | +just example-pydantic |
| 83 | + |
| 84 | +# Or manually |
| 85 | +cd examples/pydantic |
| 86 | +dotnet fable --lang python --outDir build |
| 87 | +cp models.py build/ |
| 88 | +cd build && python app.py |
| 89 | +``` |
| 90 | + |
| 91 | +## Output |
| 92 | + |
| 93 | +```txt |
| 94 | +User 1: Alice (id=1) |
| 95 | + |
| 96 | +User 1 age: 30 |
| 97 | +
|
| 98 | +User 2: Bob (id=2) |
| 99 | +User 2 email: None |
| 100 | +
|
| 101 | +Product: Laptop - $1299.99 |
| 102 | +In stock: true |
| 103 | +Tags: ['electronics', 'computers'] |
| 104 | +
|
| 105 | + |
| 106 | +
|
| 107 | +Create request for: Charlie |
| 108 | +``` |
| 109 | + |
| 110 | +## See Also |
| 111 | + |
| 112 | +- [FastAPI example](../fastapi/) - Shows the opposite pattern: defining Pydantic models in F# that compile to Python |
0 commit comments