<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Cardamom Code]]></title><description><![CDATA[Python type annotation tutorial]]></description><link>https://cardamomcode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 18:12:43 GMT</lastBuildDate><atom:link href="https://cardamomcode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Fable Python]]></title><description><![CDATA[Introduction to Fable.Python
Generated on 2026-03-08 17:54 UTC using Fable v5.0.0-rc.2

This post is part of the F# Advent Calendar 2025. Thank you, Sergey Tihon, for organizing this wonderful traditi]]></description><link>https://cardamomcode.dev/fable-python</link><guid isPermaLink="true">https://cardamomcode.dev/fable-python</guid><category><![CDATA[F#]]></category><category><![CDATA[Python]]></category><category><![CDATA[fable]]></category><category><![CDATA[#fsharp]]></category><category><![CDATA[fablecompiler]]></category><dc:creator><![CDATA[Dag Brattli]]></dc:creator><pubDate>Sun, 21 Dec 2025 14:39:30 GMT</pubDate><content:encoded><![CDATA[<h1>Introduction to Fable.Python</h1>
<p><em>Generated on 2026-03-08 17:54 UTC using Fable v5.0.0-rc.2</em></p>
<blockquote>
<p>This post is part of the <a href="https://sergeytihon.com/2025/11/03/f-advent-calendar-in-english-2025/">F# Advent Calendar 2025</a>. Thank you, Sergey Tihon, for organizing this wonderful tradition that brings the F# community together every year!</p>
</blockquote>
<p>This guide covers <a href="https://fable.io/">Fable</a> and <a href="https://github.com/fable-compiler/Fable.Python/">Fable.Python</a> - a compiler that transforms F# code into Python.</p>
<h2>Table of Contents</h2>
<ol>
<li><p><a href="#heading-are-you-a-python-developer">F# for Python Developers</a> - Core concepts if you're coming from Python</p>
</li>
<li><p><a href="#heading-getting-started-with-fablepython">Getting Started</a> - Installation and your first project</p>
</li>
<li><p><a href="#heading-python-interop">Python Interop</a> - Calling Python libraries from F#</p>
</li>
<li><p><a href="#heading-creating-python-bindings">Creating Bindings</a> - Type-safe wrappers for Python packages</p>
</li>
<li><p><a href="#heading-f-compatibility-in-fablepython">F# Compatibility</a> - What works, what doesn't</p>
</li>
<li><p><a href="#heading-async-programming">Async Programming</a> - F# async and Python asyncio</p>
</li>
<li><p><a href="#heading-testing-fablepython-projects">Testing</a> - Using pytest with F# code</p>
</li>
<li><p><a href="#heading-fable-v5-whats-new">Fable v5</a> - New features and the Rust core</p>
</li>
<li><p><a href="#heading-pydantic-interop">Pydantic Integration</a> - Type-safe data validation</p>
</li>
<li><p><a href="#heading-fastapi">FastAPI</a> - Building type-safe web APIs in the Python ecosystem</p>
</li>
<li><p><a href="#heading-units-of-measure">Units of Measure</a> - Compile-time dimensional analysis</p>
</li>
<li><p><a href="#heading-fableliterate-the-strange-loop">Fable.Literate</a> - The tool that wrote this post</p>
</li>
</ol>
<p><strong>A teaser:</strong> the final chapter reveals how this entire blog post was generated. The converter that transforms F# literate files into Markdown is itself written in F#, compiled to Python with Fable, and documented using its own output format. It's turtles all the way down.</p>
<h2>What is Fable?</h2>
<p><a href="https://fable.io/">Fable</a> is a compiler that brings F# to different platforms and ecosystems. While Fable is best known for compiling F# to TypeScript and JavaScript, it also supports other targets including Python, Rust, and Dart.</p>
<h2>Why Fable.Python?</h2>
<p>F# is a functional-first language with powerful features like:</p>
<ul>
<li><p><strong>Type inference</strong> - Write less, express more</p>
</li>
<li><p><strong>Pattern matching</strong> - Elegant handling of complex data</p>
</li>
<li><p><strong>Immutability by default</strong> - Safer, more predictable code</p>
</li>
<li><p><strong>Algebraic data types</strong> - Model your domain precisely with discriminated unions and records</p>
</li>
</ul>
<p>These features make F# excellent for <a href="https://www.pragprog.com/titles/swdddf/domain-modeling-made-functional/">Domain Modeling</a> - expressing business rules as types that the compiler enforces.</p>
<p>Python is currently <a href="https://www.tiobe.com/tiobe-index/">the most popular programming language in the world</a>. And no matter what you think of Python, it will always be the second best language for everything. That ubiquity is exactly why Fable.Python exists.</p>
<h2>When to Use Fable.Python</h2>
<p>Fable.Python is a great choice when:</p>
<ul>
<li><p><strong>Python ecosystem access</strong> - You need AI/ML libraries (PyTorch, TensorFlow, LangChain), data science tools (Pandas, NumPy), or frameworks like Pydantic and FastAPI</p>
</li>
<li><p><strong>F# type safety</strong> - You want pattern matching and exhaustive checking while using Python libraries</p>
</li>
<li><p><strong>Shared domain logic</strong> - Write once in F#, run on .NET, JavaScript, Rust, and Python</p>
</li>
<li><p><strong>Publish to PyPI</strong> - Your F# library can be available to the entire Python ecosystem</p>
</li>
<li><p><strong>Units of measure</strong> - F#'s compile-time dimensional analysis prevents unit errors that Python can't catch</p>
</li>
</ul>
<h2>When Not to Use Fable.Python</h2>
<ul>
<li><p>When your F# code depends on .NET libraries without Fable support</p>
</li>
<li><p>Performance-critical code (Python has runtime overhead)</p>
</li>
<li><p>Team won't learn F#</p>
</li>
</ul>
<p><strong>Best fit:</strong> You love F#, but need Python's ecosystem.</p>
<h2>A First Example</h2>
<p>Let's begin with a simple F# example:</p>
<pre><code class="language-fsharp">let greet (name: string) = $"Hello, {name}!"

let message = greet "Fable.Python"
</code></pre>
<p>When compiled with Fable, this generates the following Python:</p>
<pre><code class="language-python">def greet(name: str) -&gt; str:
    return concat("Hello, ", name, "!")

message: str = greet("Fable.Python")
</code></pre>
<p>Notice how the explicit type annotation <code>(name: string)</code> generates clean Python with <code>name: str</code>. Without it, F# infers from usage and Fable generates <code>name: Any | None = None</code> to handle cases where the function might be called with no argument. Type annotations give you cleaner output.</p>
<h2>The Power of Types</h2>
<p>F# shines when modeling domain concepts. Consider this example:</p>
<pre><code class="language-fsharp">type Shape =
    | Circle of radius: float
    | Rectangle of width: float * height: float

let area shape =
    match shape with
    | Circle radius -&gt; System.Math.PI * radius * radius
    | Rectangle(width, height) -&gt; width * height

let shapes = [ Circle 5.0; Rectangle(3.0, 4.0) ]

let totalArea = shapes |&gt; List.sumBy area
</code></pre>
<p>This compiles to Python while preserving the semantic meaning. The <code>Shape</code> type becomes a tagged class structure, and the <code>match</code> expression becomes clean conditional logic. The compiler ensures you handle all cases, i.e if you add a new shape variant, the compiler will warn you about unhandled cases in the <code>area</code> function.</p>
<h2>What's Next?</h2>
<p>In the following chapters, we will get started by setting up your environment, working with Python libraries, and understanding F# compatibility with Fable. Let's begin.</p>
<h2>Are You a Python Developer?</h2>
<p>If you're coming from Python, this chapter covers the F# code you'll see throughout this guide. F# is more approachable than it might appear, and many concepts are familiar.</p>
<h3>What is F#?</h3>
<p>F# is a functional-first language that runs on .NET. But here's the key insight for you: <strong>with Fable.Python, .NET is just a build tool</strong>. You write F#, it compiles to Python, and you run Python. Your deployment is pure Python.</p>
<p>Think of it like TypeScript for JavaScript - you get better tooling and type safety during development, but the output is the language you know.</p>
<h3>Key Concepts You'll See</h3>
<p>Here's how F# concepts map to Python equivalents you already know.</p>
<h4>Type Inference</h4>
<p>F# has type inference like Python's type hints, but enforced at compile time:</p>
<pre><code class="language-python"># Python with type hints (optional, not enforced)
def greet(name: str) -&gt; str:
    return f"Hello, {name}"
</code></pre>
<pre><code class="language-fsharp">// F# - types are inferred automatically
let greet name = $"Hello, {name}"

// Or explicitly annotated (rarely needed)
let greetExplicit (name: string) : string = $"Hello, {name}"
</code></pre>
<p>The compiler figures out that <code>name</code> is a string and <code>greet</code> returns a string. No need to write it unless you want to.</p>
<h4>Pattern Matching</h4>
<p>Python 3.10+ has <code>match</code>/<code>case</code>:</p>
<pre><code class="language-python"># Python match/case
match command:
    case "quit":
        return exit()
    case "help":
        return show_help()
    case _:
        return unknown_command()
</code></pre>
<p>F# pattern matching is similar but more powerful:</p>
<pre><code class="language-fsharp">let handleCommand command =
    match command with
    | "quit" -&gt; "Exiting..."
    | "help" -&gt; "Showing help..."
    | _ -&gt; "Unknown command"
</code></pre>
<p>F# pattern matching also destructures data, which we'll see with discriminated unions.</p>
<h4>Discriminated Unions (Sum Types)</h4>
<p>This is F#'s superpower. Think of it as a type-safe enum that can hold data:</p>
<pre><code class="language-python"># Python - often done with classes or dataclasses
class Shape:
    pass

class Circle(Shape):
    def __init__(self, radius: float):
        self.radius = radius

class Rectangle(Shape):
    def __init__(self, width: float, height: float):
        self.width = width
        self.height = height
</code></pre>
<pre><code class="language-fsharp">// F# discriminated union - much more concise
type Shape =
    | Circle of radius: float
    | Rectangle of width: float * height: float

// Pattern matching ensures you handle all cases
let area shape =
    match shape with
    | Circle radius -&gt; Math.PI * radius * radius
    | Rectangle(width, height) -&gt; width * height
</code></pre>
<p>The compiler warns you if you forget to handle a case. No more runtime <code>AttributeError</code> because you forgot a shape type.</p>
<h4>Records</h4>
<p>Records are like Python's <code>@dataclass</code> but immutable by default:</p>
<pre><code class="language-python"># Python dataclass
@dataclass
class Person:
    name: str
    age: int
    email: str | None = None
</code></pre>
<pre><code class="language-fsharp">// F# record
type Person = {
    Name: string
    Age: int
    Email: string option
}

// Creating a record
let alice = {
    Name = "Alice"
    Age = 30
    Email = Some "alice@example.com"
}
</code></pre>
<p>Records are immutable - to "change" one, you create a copy with updated fields:</p>
<pre><code class="language-fsharp">let olderAlice = { alice with Age = 31 }
</code></pre>
<h4>The Pipeline Operator</h4>
<p>The <code>|&gt;</code> operator is like method chaining, but for any function:</p>
<pre><code class="language-python"># Python - nested calls or intermediate variables
result = sum(map(lambda x: x * 2, filter(lambda x: x &gt; 0, numbers)))

# Or with intermediate variables
positives = filter(lambda x: x &gt; 0, numbers)
doubled = map(lambda x: x * 2, positives)
result = sum(doubled)
</code></pre>
<pre><code class="language-fsharp">let numbers = [ -1; 2; -3; 4; 5 ]

// F# pipeline - reads left to right, top to bottom
let result =
    numbers
    |&gt; List.filter (fun x -&gt; x &gt; 0)
    |&gt; List.map (fun x -&gt; x * 2)
    |&gt; List.sum
</code></pre>
<p>The <code>|&gt;</code> operator takes the value on the left and passes it as the last argument to the function on the right. It makes data transformations very readable.</p>
<h4>Option Types</h4>
<p>F# uses <code>Option</code> instead of <code>None</code>/null. This forces you to handle missing values:</p>
<pre><code class="language-python"># Python - None can sneak in anywhere
def find_user(id: int) -&gt; User | None:
    ...

user = find_user(123)
print(user.name)  # Runtime error if user is None!
</code></pre>
<pre><code class="language-fsharp">// F# Option - compiler ensures you handle None
let findUser id : Person option = if id = 1 then Some alice else None

let displayName userId =
    match findUser userId with
    | Some person -&gt; person.Name
    | None -&gt; "Unknown user"
</code></pre>
<p>You cannot accidentally use a <code>None</code> value - the compiler requires you to unwrap the option first.</p>
<h3>F# vs Python: Quick Reference</h3>
<table>
<thead>
<tr>
<th>Concept</th>
<th>Python</th>
<th>F#</th>
</tr>
</thead>
<tbody><tr>
<td>Function def</td>
<td><code>def foo(x):</code></td>
<td><code>let foo x =</code></td>
</tr>
<tr>
<td>Lambda</td>
<td><code>lambda x: x + 1</code></td>
<td><code>fun x -&gt; x + 1</code></td>
</tr>
<tr>
<td>List</td>
<td><code>[1, 2, 3]</code></td>
<td><code>[1; 2; 3]</code></td>
</tr>
<tr>
<td>Tuple</td>
<td><code>(1, "a")</code></td>
<td><code>(1, "a")</code></td>
</tr>
<tr>
<td>Dictionary</td>
<td><code>{"a": 1}</code></td>
<td><code>Map.ofList [("a", 1)]</code></td>
</tr>
<tr>
<td>None check</td>
<td><code>if x is None:</code></td>
<td><code>match x with None -&gt;</code></td>
</tr>
<tr>
<td>String format</td>
<td><code>f"Hello {name}"</code></td>
<td><code>$"Hello {name}"</code></td>
</tr>
<tr>
<td>Type annotation</td>
<td><code>x: int</code></td>
<td><code>x: int32</code></td>
</tr>
<tr>
<td>Comments</td>
<td><code># comment</code></td>
<td><code>// comment</code></td>
</tr>
<tr>
<td>Multiline string</td>
<td><code>"""text"""</code></td>
<td><code>"""text"""</code> (same!)</td>
</tr>
</tbody></table>
<h3>Why Learn F#?</h3>
<p>As a Python developer, F# gives you:</p>
<ol>
<li><p><strong>Catch bugs at compile time</strong> - No more <code>TypeError</code> or <code>AttributeError</code> at runtime</p>
</li>
<li><p><strong>Exhaustive pattern matching</strong> - Compiler ensures you handle all cases</p>
</li>
<li><p><strong>Immutability by default</strong> - Fewer bugs from unexpected state changes</p>
</li>
<li><p><strong>Excellent refactoring</strong> - Change a type, compiler shows every place to update</p>
</li>
<li><p><strong>Self-documenting code</strong> - Types serve as documentation that can't go stale</p>
</li>
</ol>
<h3>Don't Worry About .NET</h3>
<p>You might think "but I don't know .NET!" - and that's fine. For Fable.Python:</p>
<ul>
<li><p>You don't deploy to .NET</p>
</li>
<li><p>You don't need to learn C# or ASP.NET</p>
</li>
<li><p>You don't need Windows or Visual Studio</p>
</li>
</ul>
<p>.NET is just the build toolchain. You:</p>
<ol>
<li><p>Write F# code</p>
</li>
<li><p>Run <code>dotnet fable --lang python</code></p>
</li>
<li><p>Get Python files</p>
</li>
<li><p>Run with <code>python</code></p>
</li>
</ol>
<p>Your deployment, your dependencies, your runtime - all Python.</p>
<h3>Ready to Start?</h3>
<p>Now that you understand the basics, let's set up your first Fable.Python project in the next chapter!</p>
<h2>Getting Started with Fable.Python</h2>
<p>In this section we will set up a Fable.Python project from scratch and get our first F# code running as Python.</p>
<h3>Prerequisites</h3>
<p>You'll need:</p>
<ul>
<li><p><a href="https://dotnet.microsoft.com/download">.NET SDK</a> (6.0 or later. We recommend installing the latest LTS version, currently .NET 10)</p>
</li>
<li><p><a href="https://www.python.org/downloads/">Python 3.12+</a> (Fable targets Python 3.12 or higher)</p>
</li>
<li><p><a href="https://docs.astral.sh/uv/">uv</a> (recommended) - A fast Python package manager written in Rust that simplifies dependency management, virtual environments, and the installation of Python itself.</p>
</li>
</ul>
<p>If you don't have <code>uv</code> installed:</p>
<pre><code class="language-bash"># macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
</code></pre>
<p>You can also use <code>pip</code> if you prefer, but <code>uv</code> is significantly faster and handles virtual environments automatically.</p>
<h3>Project Setup</h3>
<p>Create a new directory and initialize an F# project:</p>
<pre><code class="language-bash">mkdir my-fable-python
cd my-fable-python

# Create F# console app
dotnet new console -lang F#

# Set up local tools and install Fable 5
dotnet new tool-manifest
dotnet tool install fable --version 5.0.0-rc.2

# Add Fable.Core package
dotnet add package Fable.Core --version 5.0.0-rc.1
</code></pre>
<h3>Install Python Dependencies</h3>
<p>Fable-generated Python code requires the <code>fable-library</code> runtime:</p>
<pre><code class="language-bash"># Using uv (recommended)
uv add "fable-library==5.0.0rc2"

# Or with pip
pip install "fable-library==5.0.0rc2"
</code></pre>
<hr />
<p><strong>Note:</strong> Version pinning matters. The fable-library version must match your Fable compiler version. Note that PyPI uses <code>5.0.0rc2</code> format instead of <code>5.0.0-rc.2</code> for prerelease release candidate versions.</p>
<hr />
<h3>Your First Program</h3>
<p>Replace the contents of <code>Program.fs</code> with:</p>
<pre><code class="language-fsharp">printfn "Hello from Fable.Python!"

let square x = x * x
let numbers = [1; 2; 3; 4; 5]
let squares = numbers |&gt; List.map square

printfn "Squares: %A" squares
</code></pre>
<h3>Compile and Run</h3>
<p>Transpile to Python:</p>
<pre><code class="language-bash">dotnet fable --lang python
</code></pre>
<p>This creates <code>program.py</code> in your project directory. Run it:</p>
<pre><code class="language-bash"># Using uv
uv run python program.py

# Or directly with python
python3 program.py
</code></pre>
<p>You should see:</p>
<pre><code class="language-text">Hello from Fable.Python!
Squares: [1; 4; 9; 16; 25]
</code></pre>
<h3>Watch Mode</h3>
<p>For development, use watch mode to automatically recompile on changes:</p>
<pre><code class="language-bash">dotnet fable watch --lang python
</code></pre>
<p>Now any changes to your F# files will instantly produce updated Python output.</p>
<h3>Project Structure</h3>
<p>After setup, your project looks like this:</p>
<pre><code class="language-text">my-fable-python/
├── Program.fs          # Your F# source code
├── program.py          # Generated Python (don't edit!)
├── my-fable-python.fsproj
├── fable_modules/      # Fable runtime modules
└── .config/
    └── dotnet-tools.json
</code></pre>
<h3>Next Steps</h3>
<p>Now that you have a working setup, let's see how we can interact with Python libraries by using <strong>Bindings</strong>.</p>
<h2>Python Interop</h2>
<p>With a Fable.Python project set up, we can start to work with Python libraries and the existing bindings in the Fable.Python ecosystem.</p>
<h3>The Fable.Python Library</h3>
<p>The <a href="https://github.com/fable-compiler/Fable.Python">Fable.Python</a> NuGet package provides ready-to-use bindings for Python's standard library. Add it to your project:</p>
<pre><code class="language-bash">dotnet add package Fable.Python
</code></pre>
<p>This gives you typed access to modules like <code>os</code>, <code>sys</code>, <code>json</code>, <code>asyncio</code>, and more.</p>
<h3>Using Standard Library Modules</h3>
<p>Here's how to use Python's <code>os</code> module:</p>
<pre><code class="language-fsharp">open Fable.Python.Os

let currentDir = os.getcwd ()
let files = os.listdir "."
</code></pre>
<p>The bindings follow F# naming conventions, but Fable automatically converts to Python's snake_case when generating code.</p>
<h3>Working with Python's json Module</h3>
<p>For basic JSON operations, use Python's built-in <code>json</code> module:</p>
<pre><code class="language-fsharp">open Fable.Python.Json

// Serialize F# data to JSON string
let data = {|
    name = "Alice"
    age = 30
|}
</code></pre>
<p>Anonymous records (<code>{| ... |}</code>) are perfect for JSON - they compile to Python dictionaries. See the Compatibility chapter for details on how F# types map to Python types.</p>
<h3>Calling Python Functions</h3>
<h4>Basic Function Calls</h4>
<p>Most Python functions can be called naturally through bindings:</p>
<pre><code class="language-fsharp">open Fable.Python.Builtins

let length = builtins.len [ 1; 2; 3 ]
let absValue = builtins.abs (-42)
</code></pre>
<p>The <code>builtins</code> module provides typed access to Python's built-in functions. These calls compile directly to <code>len([1, 2, 3])</code> and <code>abs(-42)</code> in Python.</p>
<h4>Working with sys Module</h4>
<pre><code class="language-fsharp">open Fable.Python.Sys

let pythonVersion = sys.version
let args = sys.argv
</code></pre>
<h4>Path Operations with os.path</h4>
<pre><code class="language-fsharp">let fullPath = os.path.join [| "/home"; "user"; "file.txt" |]
let fileName = os.path.basename "/path/to/file.txt"
let dirName = os.path.dirname "/path/to/file.txt"
</code></pre>
<p>The <code>os.path</code> functions work with arrays of path segments. These compile to Python's <code>os.path.join</code>, <code>os.path.basename</code>, and <code>os.path.dirname</code> calls.</p>
<h3>Environment Variables</h3>
<p>Use <code>os.getenv</code> to safely retrieve environment variables:</p>
<pre><code class="language-fsharp">let home = os.getenv ("HOME", "")
let user = os.getenv "USER" // Returns string option
</code></pre>
<h3>File Operations</h3>
<p>Reading and writing files uses Python's built-in functions:</p>
<pre><code class="language-fsharp">open Fable.Core

[&lt;Emit("open($0, 'r').read()")&gt;]
let readFile (path: string) : string = nativeOnly

[&lt;Emit("open(\(0, 'w').write(\)1)")&gt;]
let writeFile (path: string) (content: string) : unit = nativeOnly
</code></pre>
<p>For more complex file handling, you might want to use Python's context managers through custom bindings (covered in the Bindings chapter).</p>
<h3>Type Conversions</h3>
<h4>Explicit Conversions</h4>
<p>Sometimes you need to convert between F# and Python types explicitly:</p>
<pre><code class="language-fsharp">// F# list to Python list (usually automatic)
let fsharpList = [ 1; 2; 3 ]

// When you need a ResizeArray specifically
let asResizeArray = ResizeArray(fsharpList)
</code></pre>
<h4>Working with obj</h4>
<p>When dealing with dynamic Python APIs, you may encounter <code>obj</code>:</p>
<pre><code class="language-fsharp">let handleDynamic (value: obj) =
    // Pattern match on the actual type
    match value with
    | :? string as s -&gt; $"Got string: {s}"
    | :? int as n -&gt; $"Got int: {n}"
    | _ -&gt; "Got something else"
</code></pre>
<h3>Importing Python Modules</h3>
<p>Fable provides several ways to import Python modules and functions.</p>
<h4>Using import Functions</h4>
<p>The <code>import</code> function lets you import a specific member from a module:</p>
<pre><code class="language-fsharp">open Fable.Core.PyInterop

// Import a specific function from a module
let add5: int -&gt; int = import "add5" "my_module"

// Import all exports as an interface
type IMathModule =
    abstract add: int -&gt; int -&gt; int
    abstract multiply: int -&gt; int -&gt; int

let mathModule: IMathModule = importAll "math_utils"
</code></pre>
<h4>Using Import Attributes</h4>
<p>For module-level imports, use attributes:</p>
<pre><code class="language-fsharp">[&lt;ImportAll("my_native_module")&gt;]
let nativeModule: IMathModule = nativeOnly
</code></pre>
<p>The <code>nativeOnly</code> value is a placeholder - Fable replaces it with the actual import.</p>
<h3>Emit: Inline Python Code</h3>
<p>When you need to write raw Python code, use <code>Emit</code>:</p>
<h4>The Emit Attribute</h4>
<pre><code class="language-fsharp">[&lt;Emit("len($0)")&gt;]
let pyLen (x: 'a) : int = nativeOnly

[&lt;Emit("\(0 + \)1")&gt;]
let pyAdd (x: int) (y: int) : int = nativeOnly

[&lt;Emit("isinstance(\(0, \)1)")&gt;]
let pyIsInstance (obj: obj) (typ: obj) : bool = nativeOnly
</code></pre>
<p>The <code>\(0</code>, <code>\)1</code>, etc. are placeholders for the function arguments.</p>
<h4>emitPyExpr for Inline Expressions</h4>
<p>For one-off expressions without defining a function:</p>
<pre><code class="language-fsharp">let two: int = emitPyExpr (1, 1) "\(0 + \)1"
let hello: string = emitPyExpr () "\"Hello\""
</code></pre>
<h4>emitPyStatement for Multi-line Code</h4>
<p>For more complex Python code with statements:</p>
<pre><code class="language-fsharp">let factorial (count: int) : int =
    emitPyStatement
        count
        """if $0 &lt; 2:
        return 1
    else:
        return \(0 * factorial(\)0 - 1)
"""
</code></pre>
<h4>Py.python for Literal Python Code</h4>
<p><code>Py.python</code> provides a cleaner way to embed literal Python code without parameter placeholders. The code is printed as statements, so use Python's <code>return</code> keyword if you need to return a value:</p>
<pre><code class="language-fsharp">open Fable.Python

let greet (name: string) : string =
    Py.python
        $"""
    greeting = f"Hello, {{name}}!"
    return greeting
"""
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">def greet(name: str) -&gt; str:
    greeting = f"Hello, {name}!"
    return greeting
</code></pre>
<p>This is useful when you want to write a block of Python code directly, especially when it doesn't need parameter substitution (you can use F# string interpolation instead).</p>
<h3>StringEnum: Type-Safe String Constants</h3>
<p><code>StringEnum</code> creates discriminated unions that compile to Python strings:</p>
<pre><code class="language-fsharp">[&lt;StringEnum&gt;]
type Direction =
    | North
    | South
    | [&lt;CompiledName("E")&gt;] East // Custom string value
    | West

// North compiles to "north", East compiles to "E"
</code></pre>
<h4>StringEnum with Case Rules</h4>
<p>Control the string format with <code>CaseRules</code>:</p>
<pre><code class="language-fsharp">[&lt;StringEnum(CaseRules.SnakeCase)&gt;]
type UserStatus =
    | ActiveUser // -&gt; "active_user"
    | InactiveUser // -&gt; "inactive_user"

[&lt;StringEnum(CaseRules.KebabCase)&gt;]
type CssBoxSizing =
    | ContentBox // -&gt; "content-box"
    | BorderBox // -&gt; "border-box"
</code></pre>
<p>Available case rules: <code>None</code>, <code>LowerFirst</code>, <code>SnakeCase</code>, <code>SnakeCaseAllCaps</code>, <code>KebabCase</code>, <code>LowerAll</code>.</p>
<h3>Erased Unions</h3>
<p>Erased unions let you create type-safe wrappers that disappear at runtime:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
type StringOrInt =
    | AsString of string
    | AsInt of int

    member this.Describe() =
        match this with
        | AsString s -&gt; $"String: {s}"
        | AsInt n -&gt; $"Int: {n}"

// AsString "hello" compiles to just "hello" in Python
// AsInt 42 compiles to just 42
</code></pre>
<p>This is useful for APIs that accept multiple types (like Python's duck typing).</p>
<h3>Python Decorators</h3>
<p>Fable.Python supports Python decorators through several mechanisms.</p>
<h4>Creating F#-Side Decorators</h4>
<p>You can create custom decorators that wrap functions at compile time:</p>
<pre><code class="language-fsharp">type LogAttribute(msg: string) =
    inherit Py.DecoratorAttribute()

    override _.Decorate(fn) =
        Py.argsFunc (fun args -&gt;
            printfn $"LOG: {msg}"
            fn.Invoke(args))

[&lt;Log("calling myFunction")&gt;]
let myFunction x = x + 1
</code></pre>
<h4>Using Py.Decorate for Python Decorators</h4>
<p>Apply Python decorators to classes using <code>Py.Decorate</code>. The attribute takes the decorator name, the module to import from, and optional parameters:</p>
<pre><code class="language-fsharp">[&lt;Py.Decorate("dataclass", "dataclasses")&gt;]
[&lt;Py.ClassAttributes(Py.ClassAttributeStyle.Attributes, false)&gt;]
type DecoratedUser() =
    member val Name: string = "" with get, set
    member val Age: int = 0 with get, set
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">@dataclass
class DecoratedUser:
    Age: int32 = int32.ZERO
    Name: str = ""
</code></pre>
<h4>DecorateTemplate for Reusable Decorators</h4>
<p>When building a library, you can create custom decorator attributes that users can apply without knowing the underlying Python syntax. Use <code>Py.DecorateTemplate</code>:</p>
<pre><code class="language-fsharp">/// Custom route decorator for a web framework
[&lt;Erase&gt;]
[&lt;Py.DecorateTemplate("app.get('{0}')", "fastapi")&gt;]
type GetRouteAttribute(path: string) =
    inherit System.Attribute()
</code></pre>
<p>Now users of your library can simply write:</p>
<pre><code class="language-fsharp">[&lt;GetRoute("/users")&gt;]
static member get_users() = ...
// Generates: @app.get('/users')
</code></pre>
<p>The template string uses <code>{0}</code>, <code>{1}</code>, etc. as placeholders for the attribute's constructor arguments. The <code>[&lt;Erase&gt;]</code> attribute prevents the attribute type from being emitted to Python.</p>
<p>This is how the FastAPI bindings define <code>[&lt;Get&gt;]</code>, <code>[&lt;Post&gt;]</code>, and other route decorators - making them easy for users to apply without understanding the Python decorator syntax.</p>
<h3>Class Attributes and DataClasses</h3>
<h4>Py.DataClass</h4>
<p>Use <code>Py.DataClass</code> to generate Python classes with class-level type annotations as defined by <a href="https://peps.python.org/pep-0526/#class-and-instance-variable-annotations">PEP 526</a>. This is what frameworks like Pydantic, dataclasses, and attrs expect:</p>
<pre><code class="language-fsharp">// BaseModel from Pydantic (import this from Fable.Python.Pydantic in
// real code to get proper bindings)
[&lt;Import("BaseModel", "pydantic")&gt;]
type BaseModel() = class end

[&lt;Py.DataClass&gt;]
type PydanticModel() =
    inherit BaseModel()
    member val Name: string = "" with get, set
    member val Age: int = 0 with get, set
</code></pre>
<p>This generates class-level type annotations suitable for Pydantic. See the Pydantic chapter for more on working with Pydantic models:</p>
<pre><code class="language-python">class PydanticModel(BaseModel):
    Age: int32 = int32.ZERO
    Name: str = ""
</code></pre>
<h4>Py.ClassAttributes</h4>
<p><code>Py.DataClass</code> is shorthand for <code>Py.ClassAttributes(style = Attributes, init = false)</code>. Use <code>Py.ClassAttributes</code> directly when you need different options:</p>
<pre><code class="language-fsharp">[&lt;Py.ClassAttributes(style = Py.ClassAttributeStyle.Attributes, init = true)&gt;]
type UserWithInit() =
    member val Name: string = "" with get, set
    member val Age: int = 0 with get, set
</code></pre>
<p>The parameters control how the class is generated:</p>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Effect</th>
</tr>
</thead>
<tbody><tr>
<td><code>style = Attributes</code></td>
<td>Generate class-level type annotations</td>
</tr>
<tr>
<td><code>style = Properties</code></td>
<td>Generate properties with instance attribute backing</td>
</tr>
<tr>
<td><code>init = false</code></td>
<td>Don't generate <code>__init__</code> (Pydantic/dataclass provides it)</td>
</tr>
<tr>
<td><code>init = true</code></td>
<td>Generate <code>__init__</code> with attribute assignments</td>
</tr>
</tbody></table>
<h4>ClassAttributesTemplate for Library Authors</h4>
<p>Library authors can create shorthand attributes using <code>ClassAttributesTemplate</code>. This is how <code>Py.DataClass</code> is defined:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
[&lt;ClassAttributesTemplate(ClassAttributeStyle.Attributes, false)&gt;]
type DataClassAttribute() =
    inherit Attribute()
</code></pre>
<p>You can create your own shortcuts for common patterns:</p>
<pre><code class="language-fsharp">/// Shorthand for mutable classes with generated __init__
[&lt;Erase&gt;]
[&lt;Py.ClassAttributesTemplate(Py.ClassAttributeStyle.Attributes, true)&gt;]
type MutableClassAttribute() =
    inherit System.Attribute()
</code></pre>
<p>Now users can simply write <code>[&lt;MutableClass&gt;]</code> instead of the verbose <code>[&lt;Py.ClassAttributes(style = Attributes, init = true)&gt;]</code>.</p>
<h4>AttachMembers</h4>
<p>Use <code>AttachMembers</code> to generate Python-style classes with methods directly attached:</p>
<pre><code class="language-fsharp">[&lt;AttachMembers&gt;]
type Counter(initial: int) =
    let mutable count = initial

    member _.Count = count
    member _.Increment() = count &lt;- count + 1
    member _.Decrement() = count &lt;- count - 1
</code></pre>
<h3>Global Bindings</h3>
<p>Bind to Python global objects with the <code>Global</code> attribute:</p>
<pre><code class="language-fsharp">[&lt;Global("list")&gt;]
type PyList =
    [&lt;Emit("\(0.append(\)1)")&gt;]
    abstract append: item: obj -&gt; unit

    [&lt;Emit("len($0)")&gt;]
    abstract length: int
</code></pre>
<h3>Keyword Arguments with ParamObject</h3>
<p>Use <code>ParamObject</code> to generate Python keyword arguments:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
type IHttpClient =
    [&lt;ParamObject(1)&gt;]
    abstract fetch: url: string * ?timeout: int * ?headers: obj -&gt; obj
</code></pre>
<p>When called as <code>client.fetch("http://...", timeout=30)</code>, this generates Python code with keyword arguments: <code>client.fetch("http://...", timeout=30)</code>.</p>
<h3>createEmpty for Dynamic Objects</h3>
<p>Create empty objects that can have properties set dynamically:</p>
<pre><code class="language-fsharp">type IConfig =
    abstract host: string with get, set
    abstract port: int with get, set

let config = createEmpty&lt;IConfig&gt;
// config.host &lt;- "localhost"
// config.port &lt;- 8080
</code></pre>
<h3>Practical Example: Reading JSON Config</h3>
<p>Here's a complete example combining several concepts:</p>
<pre><code class="language-fsharp">let loadConfig (path: string) =
    let content = readFile path
    // Parse JSON and work with it
    json.loads content
</code></pre>
<h3>What's Next?</h3>
<p>Now you know how to use existing Python bindings and core interop features. In the next chapter we will see how you can create your own bindings for Python libraries that don't have F# bindings yet.</p>
<h2>Creating Python Bindings</h2>
<p>When a Python library doesn't have F# bindings, you can create your own. This chapter covers the patterns and best practices for writing type-safe bindings that feel natural in F#.</p>
<blockquote>
<p>Writing bindings have long been a major pain point, spending countless hours wrestling with interop details. With AI-assisted coding tools, generating initial binding code for your favorite Python libraries has become much easier.</p>
</blockquote>
<h3>Core Principles</h3>
<p>When writing bindings, follow these principles:</p>
<ol>
<li><p><strong>Near-native F# experience</strong> - Make the API feel like idiomatic F#</p>
</li>
<li><p><strong>Prefer overloads over union types</strong> - Use multiple function overloads, not <code>U2&lt;A,B&gt;</code></p>
</li>
<li><p><strong>Stay close to Python docs</strong> - Users should be able to reference Python documentation</p>
</li>
<li><p><strong>Type safety first</strong> - Leverage F#'s type system to catch errors at compile time</p>
</li>
</ol>
<h3>The IExports Pattern</h3>
<p>The recommended pattern for binding a Python module uses an erased interface:</p>
<pre><code class="language-fsharp">open Fable.Core

[&lt;Erase&gt;]
type IExports =
    abstract dumps: obj: obj -&gt; string
    abstract loads: s: string -&gt; obj

[&lt;ImportAll("json")&gt;]
let json: IExports = nativeOnly
</code></pre>
<p>This generates: <code>import json</code></p>
<p>The <code>[&lt;Erase&gt;]</code> attribute means the interface only exists at compile time (erased = no code generated for it). The <code>nativeOnly</code> placeholder tells Fable the value will be resolved at runtime.</p>
<h3>Import Attributes</h3>
<h4>ImportAll - Import Entire Module</h4>
<p><code>[&lt;ImportAll("module")&gt;]</code> imports the module and binds it to a value:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
type IOsExports =
    abstract getcwd: unit -&gt; string
    abstract listdir: path: string -&gt; string array

[&lt;ImportAll("os")&gt;]
let os: IOsExports = nativeOnly

// Usage: os.getcwd()
</code></pre>
<h4>Import - Import Specific Member</h4>
<p><code>[&lt;Import("member", "module")&gt;]</code> imports a specific class or function:</p>
<pre><code class="language-fsharp">[&lt;Import("Path", "pathlib")&gt;]
type Path =
    abstract exists: unit -&gt; bool
    abstract is_file: unit -&gt; bool
    abstract read_text: unit -&gt; string
</code></pre>
<p>This generates: <code>from pathlib import Path</code></p>
<h4>ImportMember - Import by Name</h4>
<p><code>[&lt;ImportMember("module")&gt;]</code> imports a member matching the F# value name:</p>
<pre><code class="language-fsharp">[&lt;ImportMember("datetime")&gt;]
let datetime: obj = nativeOnly

// Generates: from datetime import datetime
</code></pre>
<h3>The Emit Attribute</h3>
<p>For Python syntax that can't be expressed with imports, use <code>[&lt;Emit&gt;]</code>:</p>
<pre><code class="language-fsharp">[&lt;Emit("len($0)")&gt;]
let len (x: 'a) : int = nativeOnly

[&lt;Emit("isinstance(\(0, \)1)")&gt;]
let isinstance (obj: obj) (typ: obj) : bool = nativeOnly

[&lt;Emit("\(0[\)1]")&gt;]
let getItem (obj: 'a) (key: 'b) : 'c = nativeOnly
</code></pre>
<p>The <code>\(0</code>, <code>\)1</code>, <code>$2</code> placeholders represent arguments in order.</p>
<p>For methods on objects, use <code>$0</code> for <code>self</code>:</p>
<pre><code class="language-fsharp">[&lt;Emit("$0.upper()")&gt;]
let upper (s: string) : string = nativeOnly
</code></pre>
<h3>Function Overloads</h3>
<p><strong>Why prefer overloads over erased unions?</strong> Erased unions like <code>U2&lt;string, bytes&gt;</code> require callers to wrap values explicitly, creating friction. Instead of:</p>
<pre><code class="language-fsharp">// ❌ Avoid this - creates friction for callers
abstract parse: source: U2&lt;string, bytes&gt; -&gt; AST
</code></pre>
<p>Use multiple overloads:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
type IAstExports =
    // ✅ Multiple overloads - easy to call
    abstract parse: source: string -&gt; obj
    abstract parse: source: string * filename: string -&gt; obj
    abstract parse: source: string * filename: string * mode: string -&gt; obj
</code></pre>
<p>This matches how Python's <code>ast.parse()</code> works - optional parameters become additional overloads.</p>
<h3>String Enums</h3>
<p>For Python APIs that use string constants, use <code>[&lt;StringEnum&gt;]</code>:</p>
<pre><code class="language-fsharp">[&lt;StringEnum&gt;]
[&lt;RequireQualifiedAccess&gt;]
type HttpMethod =
    | [&lt;CompiledName("GET")&gt;] Get
    | [&lt;CompiledName("POST")&gt;] Post
    | [&lt;CompiledName("PUT")&gt;] Put
    | [&lt;CompiledName("DELETE")&gt;] Delete

let method = HttpMethod.Get // Compiles to: "GET"
</code></pre>
<p>The <code>[&lt;CompiledName&gt;]</code> attribute controls the exact string value. Use <code>[&lt;RequireQualifiedAccess&gt;]</code> to avoid polluting the namespace.</p>
<h4>Case Rules</h4>
<p>Without <code>[&lt;CompiledName&gt;]</code>, you can use case rules:</p>
<pre><code class="language-fsharp">[&lt;StringEnum(CaseRules.SnakeCase)&gt;]
type FileMode =
    | ReadOnly // Compiles to: "read_only"
    | WriteOnly // Compiles to: "write_only"
    | ReadWrite // Compiles to: "read_write"
</code></pre>
<p>Available case rules: <code>None</code>, <code>LowerFirst</code>, <code>SnakeCase</code>, <code>KebabCase</code>.</p>
<h3>Named Parameters</h3>
<p>For Python functions with keyword arguments, use <code>[&lt;NamedParams&gt;]</code>:</p>
<pre><code class="language-fsharp">[&lt;Erase&gt;]
type IBuiltins =
    [&lt;NamedParams(fromIndex = 1)&gt;]
    abstract ``open``: file: string * ?mode: string * ?encoding: string -&gt; obj
</code></pre>
<p>This generates: <code>open(file, mode=..., encoding=...)</code></p>
<p>Parameters after <code>fromIndex</code> become keyword arguments.</p>
<h3>Binding Classes</h3>
<p>For Python classes you want to inherit from or instantiate:</p>
<pre><code class="language-fsharp">[&lt;Import("BaseModel", "pydantic")&gt;]
type BaseModel() = class end
</code></pre>
<p>For classes with methods:</p>
<pre><code class="language-fsharp">[&lt;Import("Counter", "collections")&gt;]
type Counter&lt;'T&gt; =
    abstract most_common: ?n: int -&gt; ('T * int) array
    abstract update: iterable: 'T seq -&gt; unit
</code></pre>
<h3>Complete Example: Binding requests</h3>
<p>Here's how you might bind Python's <code>requests</code> library:</p>
<pre><code class="language-fsharp">[&lt;StringEnum&gt;]
[&lt;RequireQualifiedAccess&gt;]
type RequestMethod =
    | [&lt;CompiledName("GET")&gt;] Get
    | [&lt;CompiledName("POST")&gt;] Post

type Response =
    abstract status_code: int
    abstract text: string
    abstract json: unit -&gt; obj

[&lt;Erase&gt;]
type IRequestsExports =
    abstract get: url: string -&gt; Response
    abstract get: url: string * headers: obj -&gt; Response

    abstract post: url: string -&gt; Response
    abstract post: url: string * data: string -&gt; Response
    abstract post: url: string * data: string * headers: obj -&gt; Response

[&lt;ImportAll("requests")&gt;]
let requests: IRequestsExports = nativeOnly
</code></pre>
<p>Usage would be:</p>
<pre><code class="language-fsharp">let response = requests.get "https://api.example.com/data"
printfn "Status: %d" response.status_code
</code></pre>
<h3>Best Practices</h3>
<ol>
<li><p><strong>Document your bindings</strong> - Add XML doc comments from Python docs</p>
</li>
<li><p><strong>Use F# naming conventions</strong> - Fable converts camelCase to snake_case</p>
</li>
<li><p><strong>Test in Python</strong> - Always verify the generated code works</p>
</li>
<li><p><strong>Keep bindings focused</strong> - One module per Python package</p>
</li>
<li><p><strong>Handle None carefully</strong> - Use <code>option</code> types for nullable returns</p>
</li>
</ol>
<h3>File Organization</h3>
<p>A typical binding module structure:</p>
<pre><code class="language-fsharp">module Fable.Python.MyLibrary

open Fable.Core

// 1. Type aliases for complex types
type Callback = string -&gt; unit

// 2. Supporting types (enums, records)
[&lt;StringEnum&gt;]
type Mode = | Fast | Slow

// 3. Class imports
[&lt;Import("Client", "mylibrary")&gt;]
type Client = ...

// 4. Module exports interface
[&lt;Erase&gt;]
type IExports = ...

// 5. Module import
[&lt;ImportAll("mylibrary")&gt;]
let myLibrary: IExports = nativeOnly

// 6. Convenience wrappers (optional)
let createClient url = myLibrary.createClient url
</code></pre>
<h3>What's Next?</h3>
<p>With bindings covered, the <strong>Compatibility</strong> chapter shows which F# features work with Fable.Python and any limitations to be aware of.</p>
<h2>F# Compatibility in Fable.Python</h2>
<p>This chapter covers supported features, limitations, and important differences from .NET when targeting Python with Fable.</p>
<h3>Common Types and Objects</h3>
<p>Some F#/.NET types have counterparts in Python. Fable takes advantage of this to compile to native types that are more performant and reduce code size. Native types also simplify interop with Python code and libraries. The most important common types are:</p>
<table>
<thead>
<tr>
<th>F#/.NET Type</th>
<th>Python Type</th>
<th>Notes</th>
</tr>
</thead>
<tbody><tr>
<td><code>string</code></td>
<td><code>str</code></td>
<td>Behaves the same</td>
</tr>
<tr>
<td><code>bool</code></td>
<td><code>bool</code></td>
<td>Behaves the same</td>
</tr>
<tr>
<td><code>char</code></td>
<td><code>str</code></td>
<td>Compiled as string of length 1</td>
</tr>
<tr>
<td><code>Tuple</code></td>
<td><code>tuple</code></td>
<td>Native Python tuple</td>
</tr>
<tr>
<td><code>ResizeArray&lt;T&gt;</code></td>
<td><code>list</code></td>
<td>Native Python list</td>
</tr>
<tr>
<td><code>Dictionary&lt;K,V&gt;</code></td>
<td><code>dict</code></td>
<td>Native Python dict</td>
</tr>
<tr>
<td><code>seq&lt;T&gt;</code> / <code>IEnumerable</code></td>
<td><code>Iterable</code></td>
<td>Uses <code>__iter__</code> protocol</td>
</tr>
<tr>
<td><code>Array</code></td>
<td><code>FSharpArray</code></td>
<td>Custom wrapper for F# semantics</td>
</tr>
</tbody></table>
<h3>.NET Base Class Library</h3>
<p>Fable provides support for some .NET BCL classes. The following are translated to Python with most methods available:</p>
<table>
<thead>
<tr>
<th>.NET Type</th>
<th>Python Type</th>
</tr>
</thead>
<tbody><tr>
<td><code>System.String</code></td>
<td><code>str</code></td>
</tr>
<tr>
<td><code>System.Boolean</code></td>
<td><code>bool</code></td>
</tr>
<tr>
<td><code>System.Char</code></td>
<td><code>str</code></td>
</tr>
<tr>
<td><code>System.DateTime</code></td>
<td><code>datetime</code></td>
</tr>
<tr>
<td><code>System.Decimal</code></td>
<td><code>decimal</code></td>
</tr>
<tr>
<td><code>System.Collections.Generic.List&lt;T&gt;</code></td>
<td><code>list</code></td>
</tr>
<tr>
<td><code>System.Collections.Generic.Dictionary&lt;K,V&gt;</code></td>
<td><code>dict</code></td>
</tr>
</tbody></table>
<h3>FSharp.Core</h3>
<p>Most FSharp.Core operators are supported, including formatting with <code>sprintf</code>, <code>printfn</code>, and <code>failwithf</code>. The following types from FSharp.Core translate to Python:</p>
<table>
<thead>
<tr>
<th>F# Type</th>
<th>Python</th>
</tr>
</thead>
<tbody><tr>
<td><code>Tuple</code></td>
<td><code>tuple</code></td>
</tr>
<tr>
<td><code>Option&lt;T&gt;</code></td>
<td><code>Optional[T]</code> (*)</td>
</tr>
<tr>
<td><code>string</code></td>
<td><code>str</code></td>
</tr>
<tr>
<td><code>List&lt;T&gt;</code></td>
<td><code>List.fs</code> (immutable list)</td>
</tr>
<tr>
<td><code>Map&lt;K,V&gt;</code></td>
<td><code>Map.fs</code> (immutable map)</td>
</tr>
<tr>
<td><code>Set&lt;T&gt;</code></td>
<td><code>Set.fs</code> (immutable set)</td>
</tr>
<tr>
<td><code>ResizeArray&lt;T&gt;</code></td>
<td><code>list</code></td>
</tr>
<tr>
<td>Record types</td>
<td><code>@dataclass</code></td>
</tr>
<tr>
<td>Anonymous Records</td>
<td><code>dict</code></td>
</tr>
</tbody></table>
<p>(*) Generated as <code>T | None</code> in Python 3.12+</p>
<h3>Interfaces and Protocols</h3>
<p>.NET interfaces map to Python protocols and special methods:</p>
<table>
<thead>
<tr>
<th>.NET Interface</th>
<th>Python</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td><code>IEquatable</code></td>
<td><code>__eq__</code></td>
<td>Equality comparison</td>
</tr>
<tr>
<td><code>IEnumerator</code></td>
<td><code>__next__</code></td>
<td>Iterator protocol</td>
</tr>
<tr>
<td><code>IEnumerable</code></td>
<td><code>__iter__</code></td>
<td>For-loop iteration</td>
</tr>
<tr>
<td><code>IComparable</code></td>
<td><code>__lt__</code> + <code>__eq__</code></td>
<td>Ordering and sorting</td>
</tr>
<tr>
<td><code>IDisposable</code></td>
<td><code>__enter__</code> + <code>__exit__</code></td>
<td>Context managers (<code>with</code> statement)</td>
</tr>
<tr>
<td><code>ToString()</code></td>
<td><code>__str__</code></td>
<td>String representation</td>
</tr>
</tbody></table>
<h3>Fully Supported Features</h3>
<h4>Core Types</h4>
<p>These F# types map directly to Python equivalents:</p>
<pre><code class="language-fsharp">// Strings -&gt; Python str
let greeting = "Hello, Python!"

// Booleans -&gt; Python bool
let isEnabled = true

// Tuples -&gt; Python tuple
let coordinates = (10.5, 20.3)

// F# List -&gt; Python list (via fable-library)
let numbers = [ 1; 2; 3; 4; 5 ]

// ResizeArray -&gt; Python list (native)
let mutableList = ResizeArray&lt;int&gt;()
</code></pre>
<pre><code class="language-python">greeting: str = "Hello, Python!"

is_enabled: bool = True

coordinates: tuple[float64, float64] = (float64(10.5), float64(20.3))

numbers: FSharpList[int32] = of_array(
    Array[int32]([int32.ONE, int32.TWO, int32.THREE, int32.FOUR, int32.FIVE])
)

mutable_list: list[int32] = []
</code></pre>
<p>Each of these F# values compiles to its Python equivalent. Strings become <code>str</code>, booleans become <code>bool</code>, and tuples become Python tuples. The F# <code>list</code> uses the fable-library implementation for immutable semantics, while <code>ResizeArray</code> compiles directly to Python's mutable <code>list</code>.</p>
<h4>Functions and Lambdas</h4>
<p>First-class functions work as expected:</p>
<pre><code class="language-fsharp">let add x y = x + y
let multiply = fun x y -&gt; x * y

let applyTwice f x = f (f x)
let result = applyTwice (add 1) 5 // 7
</code></pre>
<p>Functions are first-class values in F#. The <code>applyTwice</code> function takes another function <code>f</code> as a parameter and applies it twice. Partial application works naturally - <code>(add 1)</code> creates a new function that adds 1 to its argument.</p>
<h4>Pattern Matching</h4>
<p>Full pattern matching support:</p>
<pre><code class="language-fsharp">type Result&lt;'T, 'E&gt; =
    | Ok of 'T
    | Error of 'E

let handleResult result =
    match result with
    | Ok value -&gt; $"Success: {value}"
    | Error err -&gt; $"Failed: {err}"

let activePatternExample input =
    match input with
    | x when x &gt; 0 -&gt; "positive"
    | x when x &lt; 0 -&gt; "negative"
    | _ -&gt; "zero"
</code></pre>
<h4>Records</h4>
<p>Records compile to Python dataclasses:</p>
<pre><code class="language-fsharp">type Person = {
    Name: string
    Age: int
    Email: string option
}

let person = {
    Name = "Alice"
    Age = 30
    Email = Some "alice@example.com"
}
</code></pre>
<pre><code class="language-python">@dataclass(eq=False, repr=False, slots=True)
class Person(Record):
    name: str
    age: int32
    email: str | None
</code></pre>
<h4>Discriminated Unions</h4>
<p>DUs are fully supported with pattern matching:</p>
<pre><code class="language-fsharp">type Shape =
    | Circle of radius: float
    | Rectangle of width: float * height: float
    | Triangle of a: float * b: float * c: float

let describe shape =
    match shape with
    | Circle r -&gt; $"Circle with radius {r}"
    | Rectangle(w, h) -&gt; $"Rectangle {w}x{h}"
    | Triangle(a, b, c) -&gt; $"Triangle with sides {a}, {b}, {c}"
</code></pre>
<h4>Object-Oriented Features</h4>
<p>Classes, interfaces, inheritance, and overloading work:</p>
<pre><code class="language-fsharp">type IShape =
    abstract member Area: float

type Circle2(radius: float) =
    member _.Radius = radius

    interface IShape with
        member _.Area = System.Math.PI * radius * radius
</code></pre>
<h4>Collections</h4>
<p>Core collection operations are supported:</p>
<pre><code class="language-fsharp">let listOps =
    [ 1..10 ]
    |&gt; List.filter (fun x -&gt; x % 2 = 0)
    |&gt; List.map (fun x -&gt; x * x)
    |&gt; List.sum

let arrayOps = [| 1; 2; 3 |] |&gt; Array.map (fun x -&gt; x + 1)

let setOps = Set.ofList [ 1; 2; 2; 3; 3; 3 ] // {1, 2, 3}

let mapOps = Map.ofList [ ("a", 1); ("b", 2) ]
</code></pre>
<h3>Limitations and Differences</h3>
<h4>Options Are Erased</h4>
<p>Options are erased at runtime, which is actually a feature rather than a limitation. This makes interop with Python libraries seamless - you can pass F# option values directly to Python functions expecting <code>T | None</code>:</p>
<pre><code class="language-fsharp">let someValue = Some 42 // Compiles to just: 42
let noneValue = None // Compiles to: None
</code></pre>
<p>This erasure means Python code receives native values without any wrapper overhead. When calling a Python library that returns <code>Optional[T]</code>, you get values that work directly with F# pattern matching.</p>
<p>For the rare edge case of nested options (<code>Option&lt;Option&lt;T&gt;&gt;</code>), Fable.Python uses a <code>SomeWrapper</code> to distinguish <code>Some None</code> from <code>None</code>. However, nested options are uncommon in practice - the F# compiler warns about them in type annotations, and well-designed library bindings avoid exposing them at API boundaries.</p>
<h4>Multi-line Lambdas</h4>
<p>Python doesn't support multi-line lambdas. Fable lifts them to separate functions:</p>
<pre><code class="language-fsharp">// This F#:
let processed =
    [ 1; 2; 3 ]
    |&gt; List.map (fun x -&gt;
        let doubled = x * 2
        let squared = doubled * doubled
        squared)

// Becomes a separate function in Python
</code></pre>
<p>We can see that the mapping becomes a separate function in the generated Python code.</p>
<pre><code class="language-python">def mapping(x_1: int32) -&gt; int32:
    return x_1 * x_1

processed: FSharpList[int32] = map(
    mapping, of_array(Array[int32]([int32.ONE, int32.TWO, int32.THREE]))
)
</code></pre>
<h4>Numeric Types</h4>
<p>Numeric types in Fable.Python are implemented using custom PyO3 wrapper types written in Rust. These wrappers maintain F#-style semantics (like proper overflow behavior) while integrating seamlessly with Python.</p>
<table>
<thead>
<tr>
<th>F# Type</th>
<th>.NET Type</th>
<th>Python Type</th>
<th>Notes</th>
</tr>
</thead>
<tbody><tr>
<td><code>int</code></td>
<td>Int32</td>
<td>Int32</td>
<td>Custom wrapper with overflow semantics</td>
</tr>
<tr>
<td><code>int64</code></td>
<td>Int64</td>
<td>Int64</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>int16</code></td>
<td>Int16</td>
<td>Int16</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>byte</code></td>
<td>Byte</td>
<td>UInt8</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>sbyte</code></td>
<td>SByte</td>
<td>Int8</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>uint16</code></td>
<td>UInt16</td>
<td>UInt16</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>uint32</code></td>
<td>UInt32</td>
<td>UInt32</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>uint64</code></td>
<td>UInt64</td>
<td>UInt64</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>float</code> / <code>double</code></td>
<td>Double</td>
<td>Float64</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>float32</code> / <code>single</code></td>
<td>Single</td>
<td>Float32</td>
<td>Custom wrapper</td>
</tr>
<tr>
<td><code>bigint</code></td>
<td>BigInteger</td>
<td>int</td>
<td>Native Python type</td>
</tr>
<tr>
<td><code>nativeint</code></td>
<td>IntPtr</td>
<td>int</td>
<td>Native Python type</td>
</tr>
</tbody></table>
<p>The wrapper types ensure type safety and correct arithmetic behavior:</p>
<pre><code class="language-fsharp">let small: int = 42
let big: bigint = 12345678901234567890I

// Wrapper types maintain proper overflow semantics
let maxInt: int = System.Int32.MaxValue
let wrapped: int = maxInt + 1 // Wraps around like .NET

// bigint uses Python's native arbitrary-precision int
let huge: bigint = 999999999999999999999999999999I
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">small: int32 = int32(42)

big: int = 12345678901234567890

wrapped: int32 = max_int + int32.ONE

huge: int = 999999999999999999999999999999
</code></pre>
<h4>Computation Expressions</h4>
<p>Async and task computation expressions have some differences from .NET. Use <code>Async.StartAsTask</code> for Python compatibility.</p>
<h3>Project Configuration</h3>
<h4>Entry Point Applications</h4>
<p>If your project has <code>[&lt;EntryPoint&gt;]</code>, you need:</p>
<pre><code class="language-xml">&lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
&lt;/PropertyGroup&gt;
</code></pre>
<p>This ensures the use of absolute imports in generated Python. Applications in Python must use absolute imports to run correctly.</p>
<h4>Libraries</h4>
<p>Libraries use relative imports by default, which is correct for packages.</p>
<h3>Best Practices</h3>
<ol>
<li><p><strong>Test in Python</strong> - Always test generated code in Python, not just in F#</p>
</li>
<li><p><strong>Avoid reflection</strong> - Reflection has limited support</p>
</li>
<li><p><strong>Use type annotations</strong> - Helps with debugging generated code</p>
</li>
<li><p><strong>Check fable-library</strong> - Some .NET APIs may not be implemented yet</p>
</li>
</ol>
<h3>Summary</h3>
<p>Fable.Python provides excellent F# support. The main things to watch for are:</p>
<ul>
<li><p>Option erasure in edge cases</p>
</li>
<li><p>Multi-line lambda lifting, will not be anonymous</p>
</li>
<li><p>Some .NET APIs may be missing</p>
</li>
</ul>
<p>For most F# code, you can write idiomatic functional code and it will compile to clean, working Python.</p>
<h2>Async Programming</h2>
<p>Asynchronous programming is essential for modern applications - from web APIs to data processing pipelines. F# offers two models for async code: <code>async</code> workflows and <code>task</code> expressions. Understanding when to use each is key to effective Fable.Python development.</p>
<h3>Comparing Python and F# Async Models</h3>
<p>Python's async model is built on <code>asyncio</code>. Python coroutines are <strong>cold</strong> - calling an <code>async def</code> function returns a coroutine object that doesn't execute until awaited:</p>
<pre><code class="language-python">import asyncio

async def fetch_data():
    print("Starting")  # Not printed when function is called!
    await asyncio.sleep(1)
    return "data"

coro = fetch_data()    # Returns coroutine, nothing executes yet
result = await coro    # NOW "Starting" prints and code runs

# Or more commonly:
asyncio.run(fetch_data())
</code></pre>
<p>F# provides two computation expressions that compile to Python's async model:</p>
<ul>
<li><p><code>async { }</code> - F#'s original async workflows (cold, composable, multi-target)</p>
</li>
<li><p><code>task { }</code> - .NET-style tasks (hot in .NET, compiles to native <code>async def</code> in Python)</p>
</li>
</ul>
<h3>F# Async Workflows</h3>
<p>The <code>async</code> computation expression has been part of F# since the beginning. It creates <em>cold</em> async operations - they don't start until explicitly run.</p>
<pre><code class="language-fsharp">open System

let fetchDataAsync () =
    async {
        do! Async.Sleep 1000
        return "data from async"
    }
</code></pre>
<p>Key characteristics of <code>async</code>:</p>
<ul>
<li><p><strong>Cold execution</strong> - Nothing happens until you start it</p>
</li>
<li><p><strong>Composable</strong> - Combine with <code>Async.Parallel</code>, <code>Async.Sequential</code>, etc.</p>
</li>
<li><p><strong>Multi-target</strong> - The same code works on .NET, JavaScript, AND Python</p>
</li>
<li><p><strong>Cancellation</strong> - Built-in support via <code>CancellationToken</code></p>
</li>
</ul>
<h4>Running Async Workflows</h4>
<p>There are several ways to execute an async workflow:</p>
<pre><code class="language-fsharp">let runAsyncExample () =
    // Run synchronously (blocking) - simplest approach
    let result = fetchDataAsync () |&gt; Async.RunSynchronously

    // Start immediately (non-blocking) - Ignore discards the result
    fetchDataAsync () |&gt; Async.Ignore |&gt; Async.StartImmediate

    result
</code></pre>
<h4>Combining Async Operations</h4>
<p>F# async shines when composing multiple operations:</p>
<pre><code class="language-fsharp">let fetchMultipleAsync () =
    async {
        let! results =
            [ fetchDataAsync (); fetchDataAsync (); fetchDataAsync () ]
            |&gt; Async.Parallel

        return results |&gt; Array.toList
    }
</code></pre>
<p>The <code>Async.Parallel</code> function runs all operations concurrently and waits for all to complete. This is much cleaner than manually managing multiple coroutines in Python.</p>
<h4>Error Handling in Async</h4>
<p>Use <code>try...with</code> inside async blocks or <code>Async.Catch</code> for explicit error handling:</p>
<pre><code class="language-fsharp">let safeAsync () =
    async {
        try
            do! Async.Sleep 100
            failwith "Something went wrong"
            return "success"
        with ex -&gt;
            return $"Error: {ex.Message}"
    }

let catchExample () =
    async {
        let! result = safeAsync () |&gt; Async.Catch

        match result with
        | Choice1Of2 value -&gt; printfn $"Got: {value}"
        | Choice2Of2 ex -&gt; printfn $"Failed: {ex.Message}"
    }
</code></pre>
<h3>F# Tasks</h3>
<p>The <code>task</code> computation expression in .NET creates <em>hot</em> tasks that start immediately. However, when compiled to Python via Fable, tasks become Python coroutines - which are <em>cold</em> just like Python's native <code>async def</code> functions.</p>
<p>A key improvement in Fable v5 is that <code>task { }</code> now compiles to Python's native <code>async def</code> syntax. Previously, Fable generated regular functions returning <code>Awaitable[T]</code>, which frameworks like FastAPI couldn't recognize as async endpoints.</p>
<pre><code class="language-fsharp">open System.Threading.Tasks

let processItemTask (item: string) =
    task {
        do! Task.Delay 100
        return item.ToUpper()
    }
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">async def process_item_task(item: str) -&gt; str:
    builder_0040: Any = task()

    def _arrow43(item: Any = item) -&gt; Callable[[FSharpRef[Any]], bool]:
        def _arrow42(__unit: Unit = UNIT) -&gt; Callable[[FSharpRef[Any]], bool]:
            return builder_0040.Return(item.upper())

        return builder_0040.Bind(delay(int32(100)), _arrow42)

    return await builder_0040.Run(builder_0040.Delay(_arrow43))
</code></pre>
<p>Now frameworks like FastAPI can detect and handle these as proper async endpoints.</p>
<h4>Task vs Async: Key Differences</h4>
<table>
<thead>
<tr>
<th>Aspect</th>
<th><code>async { }</code></th>
<th><code>task { }</code></th>
</tr>
</thead>
<tbody><tr>
<td>.NET execution</td>
<td>Cold (lazy)</td>
<td>Hot (immediate)</td>
</tr>
<tr>
<td>Python execution</td>
<td>Cold</td>
<td>Cold (coroutines are cold)</td>
</tr>
<tr>
<td>Python output</td>
<td>Wrapped awaitable</td>
<td>Native <code>async def</code></td>
</tr>
<tr>
<td>Framework compat</td>
<td>Manual bridging</td>
<td>Direct (FastAPI, etc.)</td>
</tr>
<tr>
<td>Multi-target</td>
<td>.NET, JS, Python</td>
<td>.NET, Python</td>
</tr>
<tr>
<td>Composition</td>
<td>Rich (<code>Async.Parallel</code>)</td>
<td>Basic</td>
</tr>
</tbody></table>
<blockquote>
<p><strong>Why the difference?</strong> In .NET, an <code>async</code> method is still a regular method - when you call it, the method body starts executing immediately until it hits an <code>await</code>. The returned <code>Task</code> represents work already in progress.</p>
<p>In Python, <code>async def</code> creates a <em>coroutine function</em>. Calling it doesn't run the body - it returns a coroutine object (a generator-like structure). This coroutine is just a "recipe" that must be driven by an event loop via <code>await</code> or <code>asyncio.run()</code>.</p>
<p>When Fable compiles F# <code>task</code> to Python <code>async def</code>, the cold Python semantics apply. The advantage of <code>task</code> for Python is the native <code>async def</code> signature that frameworks recognize.</p>
</blockquote>
<h4>Working with Tasks</h4>
<pre><code class="language-fsharp">let fetchDataTask () =
    task {
        do! Task.Delay 100 // Do some async work
        return "data from task"
    }

let taskExample () =
    task {
        let! result = fetchDataTask ()
        return $"Processed: {result}"
    }

let taskWithLoop () =
    task {
        let mutable sum = 0

        for i in 1..10 do
            sum &lt;- sum + i

        return sum
    }
</code></pre>
<h3>Mapping to Python</h3>
<p>Understanding how F# async constructs map to Python helps when debugging or integrating with Python code.</p>
<h4>Async Workflows → Python</h4>
<p>F# <code>async</code> workflows compile to a wrapped async structure:</p>
<pre><code class="language-fsharp">let simpleAsync () =
    async {
        do! Async.Sleep 500
        return 42
    }
</code></pre>
<p>In Python, this generates:</p>
<pre><code class="language-python">def simple_async(__unit: Unit = UNIT) -&gt; Async[int32]:
    def _arrow52(__unit: Unit = UNIT) -&gt; Async[int32]:
        def _arrow51(__unit: Unit = UNIT) -&gt; Async[int32]:
            return singleton.Return(int32(42))

        return singleton.Bind(sleep(int32(500)), _arrow51)

    return singleton.Delay(_arrow52)
</code></pre>
<h4>Tasks → Native async def</h4>
<p>F# <code>task</code> expressions compile directly to Python's <code>async def</code>:</p>
<pre><code class="language-fsharp">let simpleTask () =
    task {
        do! Task.Delay 500
        return 42
    }
</code></pre>
<p>In Python, this generates:</p>
<pre><code class="language-python">async def simple_task(__unit: Unit = UNIT) -&gt; int32:
    builder_0040: Any = task()

    def _arrow54(__unit: Unit = UNIT) -&gt; Callable[[FSharpRef[Any]], bool]:
        def _arrow53(__unit: Unit = UNIT) -&gt; Callable[[FSharpRef[Any]], bool]:
            return builder_0040.Return(int32(42))

        return builder_0040.Bind(delay(int32(500)), _arrow53)

    return await builder_0040.Run(builder_0040.Delay(_arrow54))
</code></pre>
<h4>Running Tasks from F<code>#</code></h4>
<p>To run a task and get its result in F#:</p>
<pre><code class="language-fsharp">let runTaskExample () =
    let tsk = simpleTask ()

    // Block and wait for result
    let result = tsk.GetAwaiter().GetResult()
    printfn $"Got: {result}"
</code></pre>
<p>You can also await tasks inside other tasks:</p>
<pre><code class="language-fsharp">let chainedTasks () =
    task {
        let! first = simpleTask ()
        let! second = simpleTask ()
        return first + second
    }
</code></pre>
<h4>Running in Python's Event Loop</h4>
<p>When your compiled Python code runs, you'll need an event loop. For scripts:</p>
<pre><code class="language-python">import asyncio

async def main():
    result = await simple_task()
    print(result)

asyncio.run(main())
</code></pre>
<p>For frameworks like FastAPI, the event loop is managed for you.</p>
<h3>Practical Patterns</h3>
<h4>Async HTTP Requests</h4>
<p>Here's a pattern for async HTTP operations (assuming you have bindings for <code>aiohttp</code>):</p>
<pre><code class="language-fsharp">// Simulated async HTTP - in real code you'd use aiohttp bindings
let fetchUrlAsync (url: string) =
    async {
        do! Async.Sleep 100 // Simulates network delay
        return $"Response from {url}"
    }

let fetchMultipleUrls (urls: string list) =
    async {
        let! responses = urls |&gt; List.map fetchUrlAsync |&gt; Async.Parallel

        return responses |&gt; Array.toList
    }
</code></pre>
<h4>Sequential vs Parallel</h4>
<p>Choose based on whether operations are independent:</p>
<pre><code class="language-fsharp">let sequentialProcessing items =
    async {
        let results = ResizeArray()

        for item in items do
            let! result = fetchUrlAsync item
            results.Add(result)

        return results |&gt; Seq.toList
    }

let parallelProcessing items =
    async {
        let! results = items |&gt; List.map fetchUrlAsync |&gt; Async.Parallel
        return results |&gt; Array.toList
    }
</code></pre>
<h4>Cancellation</h4>
<p>F# async supports cancellation via <code>CancellationToken</code>:</p>
<pre><code class="language-fsharp">open System.Threading

let cancellableWork (token: CancellationToken) =
    async {
        for i in 1..100 do
            token.ThrowIfCancellationRequested()
            do! Async.Sleep 50
            printfn $"Step {i}"

        return "Completed"
    }

let runWithTimeout () =
    async {
        use cts = new CancellationTokenSource(2000) // 2 second timeout

        try
            let! result = cancellableWork cts.Token
            return Some result
        with :? OperationCanceledException -&gt;
            return None
    }
</code></pre>
<h4>Advanced: StartWithContinuations</h4>
<p>For fine-grained control over success, error, and cancellation outcomes, use <code>Async.StartWithContinuations</code>. This is useful when you need different handling paths for each case:</p>
<pre><code class="language-fsharp">let runWithContinuations () =
    Async.StartWithContinuations(
        fetchDataAsync (),
        (fun result -&gt; printfn $"Success: {result}"),
        (fun ex -&gt; printfn $"Error: {ex.Message}"),
        (fun _cancelled -&gt; printfn "Cancelled")
    )
</code></pre>
<h3>When to Use What</h3>
<h4>Use <code>task { }</code> for Python Interop</h4>
<p>When working with Python frameworks that expect native async functions:</p>
<pre><code class="language-fsharp">// FastAPI endpoint (see FastAPI chapter)
let getItemTask (itemId: int) =
    task {
        do! Task.Delay 10

        return {|
            id = itemId
            name = "Widget"
        |}
    }
</code></pre>
<h4>Use <code>async { }</code> for Multi-Target Code</h4>
<p>When you want the same async code to work on Python, .NET, AND JavaScript:</p>
<pre><code class="language-fsharp">// This code compiles to all Fable targets
let sharedBusinessLogic (input: string) =
    async {
        do! Async.Sleep 100
        let processed = input.ToUpper()
        return processed
    }
</code></pre>
<h4>Use <code>async { }</code> for Composition</h4>
<p>When you need rich composition primitives:</p>
<pre><code class="language-fsharp">let complexWorkflow () =
    async {
        // Run three operations in parallel
        let! results =
            [ fetchDataAsync (); fetchDataAsync (); fetchDataAsync () ]
            |&gt; Async.Parallel

        // Then do something sequential
        do! Async.Sleep 100

        return results |&gt; Array.toList
    }
</code></pre>
<h3>Summary</h3>
<table>
<thead>
<tr>
<th>Scenario</th>
<th>Recommendation</th>
</tr>
</thead>
<tbody><tr>
<td>FastAPI endpoints</td>
<td><code>task { }</code></td>
</tr>
<tr>
<td>aiohttp/asyncio libs</td>
<td><code>task { }</code></td>
</tr>
<tr>
<td>Multi-target library</td>
<td><code>async { }</code></td>
</tr>
<tr>
<td>Complex composition</td>
<td><code>async { }</code></td>
</tr>
<tr>
<td>Cancellation-heavy</td>
<td><code>async { }</code></td>
</tr>
<tr>
<td>Simple one-off async</td>
<td>Either works</td>
</tr>
</tbody></table>
<p>The key insight: <code>task</code> <strong>for Python-native</strong> <code>async def</code> <strong>integration (FastAPI, etc.),</strong> <code>async</code> <strong>for Fable portability and rich composition</strong>. Both are cold in Python.</p>
<p>In the next chapter, we'll look at Fable v5 features that make Python development even smoother.</p>
<h2>Testing Fable.Python Projects</h2>
<p>F# is a strongly typed language - if it compiles, it often just works. But "compiles" doesn't mean "correct". We still need testing to verify our assumptions and ensure code does what we expect. This is especially true for:</p>
<ul>
<li><p><strong>Parsers and transformers</strong>: Like Fable.Literate itself, where logic correctness matters more than type safety</p>
</li>
<li><p><strong>External dependencies</strong>: Side effects from file I/O, network calls, or Python libraries can't be checked at compile time</p>
</li>
<li><p><strong>Cross-platform transpilation</strong>: When targeting Python (or JavaScript), we need confidence that generated code behaves identically to .NET</p>
</li>
</ul>
<p>Fable itself demonstrates this commitment to correctness: the compiler has over 2000 unit tests for Python transpilation and more than 2600 tests for JavaScript. Without this extensive test suite, maintaining Fable would be impossible - the maintainers would be constantly battling regressions for every change or fix.</p>
<p>With Fable.Python, you can write tests in F# that run on both .NET and Python, catching platform-specific issues before they reach production.</p>
<p>This chapter covers two testing approaches:</p>
<ul>
<li><p><strong>XUnit-style</strong>: Familiar to many developers, uses pytest on Python</p>
</li>
<li><p><strong>Expecto-style</strong>: Functional approach using Fable.Pyxpecto</p>
</li>
</ul>
<h3>XUnit-Style Testing with Fable.Python.Testing</h3>
<p>The <code>Fable.Python.Testing</code> module provides a simple, cross-platform testing API that works with pytest on Python. Just open the module and start writing tests:</p>
<pre><code class="language-fsharp">open Fable.Python.Testing

[&lt;Fact&gt;]
let ``test addition works`` () =
    let result = 2 + 2
    result |&gt; equal 4

[&lt;Fact&gt;]
let ``test list operations work`` () =
    let numbers = [1; 2; 3]
    numbers |&gt; List.sum |&gt; equal 6
    numbers |&gt; List.length |&gt; equal 3

[&lt;Fact&gt;]
let ``test string concatenation works`` () =
    let greeting = "Hello" + " " + "World"
    greeting |&gt; equal "Hello World"
</code></pre>
<h4>Available Assertions</h4>
<p>The module provides these assertion functions:</p>
<table>
<thead>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td><code>equal expected actual</code></td>
<td>Assert equality (F# style: expected first)</td>
</tr>
<tr>
<td><code>notEqual expected actual</code></td>
<td>Assert inequality</td>
</tr>
<tr>
<td><code>throwsError msg f</code></td>
<td>Assert function throws with exact message</td>
</tr>
<tr>
<td><code>throwsErrorContaining sub f</code></td>
<td>Assert error contains substring</td>
</tr>
<tr>
<td><code>throwsAnyError f</code></td>
<td>Assert function throws any error</td>
</tr>
<tr>
<td><code>doesntThrow f</code></td>
<td>Assert function completes without error</td>
</tr>
</tbody></table>
<h4>Testing Exceptions</h4>
<p>The exception helpers make it easy to test error cases:</p>
<pre><code class="language-fsharp">[&lt;Fact&gt;]
let ``test throws on invalid input`` () =
    throwsAnyError (fun () -&gt;
        failwith "something went wrong"
    )

[&lt;Fact&gt;]
let ``test error message contains text`` () =
    throwsErrorContaining "invalid" (fun () -&gt;
        failwith "The input was invalid"
    )
</code></pre>
<h4>Running with Pytest</h4>
<p>Fable transpiles <code>[&lt;Fact&gt;]</code> functions to Python functions prefixed with <code>test_</code>, which pytest discovers automatically:</p>
<pre><code class="language-bash"># Transpile tests to Python
dotnet fable test/ --lang python --outDir build/tests

# Run with pytest
pytest build/tests
</code></pre>
<p>Pytest output looks familiar:</p>
<pre><code class="language-text">========================= test session starts =========================
collected 3 items

test_my_module.py::test_addition_works PASSED                    [ 33%]
test_my_module.py::test_list_operations_work PASSED              [ 66%]
test_my_module.py::test_string_concatenation_works PASSED        [100%]

========================== 3 passed in 0.02s ==========================
</code></pre>
<h3>Expecto-Style Testing with Pyxpecto</h3>
<p><a href="https://github.com/haf/expecto">Expecto</a> is a functional testing library for F#. <a href="https://www.nuget.org/packages/Fable.Pyxpecto">Fable.Pyxpecto</a> brings the same API to Fable, supporting JavaScript, Python, and .NET.</p>
<h4>Why Expecto-Style?</h4>
<ul>
<li><p><strong>Composable</strong>: Tests are values you can combine and transform</p>
</li>
<li><p><strong>No magic</strong>: No reflection, no attributes - just functions</p>
</li>
<li><p><strong>Familiar F# idioms</strong>: Uses lists and pipelines</p>
</li>
</ul>
<h4>Setting Up Pyxpecto</h4>
<p>Add the package to your test project:</p>
<pre><code class="language-bash">dotnet add package Fable.Pyxpecto --version 2.0.0
</code></pre>
<p>Use conditional compilation to support both platforms:</p>
<pre><code class="language-fsharp">#if FABLE_COMPILER
open Fable.Pyxpecto
#else
open Expecto
#endif
</code></pre>
<h4>Writing Expecto-Style Tests</h4>
<p>Tests are built using <code>testCase</code> and <code>testList</code>:</p>
<pre><code class="language-fsharp">let mathTests =
    testList "Math" [
        testCase "addition works" &lt;| fun _ -&gt;
            let result = 2 + 2
            Expect.equal result 4 "2 + 2 should equal 4"

        testCase "multiplication works" &lt;| fun _ -&gt;
            let result = 3 * 7
            Expect.equal result 21 "3 * 7 should equal 21"
    ]

let stringTests =
    testList "String" [
        testCase "concatenation works" &lt;| fun _ -&gt;
            let result = "Hello" + " " + "World"
            Expect.equal result "Hello World" "strings should concatenate"

        testCase "length is correct" &lt;| fun _ -&gt;
            Expect.equal ("test".Length) 4 "length should be 4"
    ]
</code></pre>
<h4>Composing Test Suites</h4>
<p>Tests are just values, so you can compose them naturally:</p>
<pre><code class="language-fsharp">let allTests =
    testList "All" [
        mathTests
        stringTests
    ]
</code></pre>
<h4>Running Pyxpecto Tests</h4>
<p>Create an entry point that runs differently on each platform:</p>
<pre><code class="language-fsharp">[&lt;EntryPoint&gt;]
let main args =
#if FABLE_COMPILER
    Pyxpecto.runTests [||] allTests
#else
    runTestsWithCLIArgs [] args allTests
#endif
</code></pre>
<p>Run on .NET:</p>
<pre><code class="language-bash">dotnet run --project MyTests.fsproj
</code></pre>
<p>Run on Python:</p>
<pre><code class="language-bash">dotnet fable MyTests/ --lang python --outDir build/tests
python build/tests/program.py
</code></pre>
<h3>Dual-Target Test Projects</h3>
<p>For maximum confidence, run your tests on both platforms. Here's a complete project setup:</p>
<h4>Project File (.fsproj)</h4>
<pre><code class="language-xml">&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
  &lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;TargetFramework&gt;net8.0&lt;/TargetFramework&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include="Expecto" Version="10.2.1" /&gt;
    &lt;PackageReference Include="Fable.Pyxpecto" Version="2.0.0" /&gt;
    &lt;PackageReference Include="Fable.Core" Version="5.0.0-rc.1" /&gt;
    &lt;PackageReference Include="Fable.Python" Version="5.0.0-rc.2" /&gt;
  &lt;/ItemGroup&gt;

  &lt;ItemGroup&gt;
    &lt;Compile Include="Tests.fs" /&gt;
    &lt;Compile Include="Program.fs" /&gt;
  &lt;/ItemGroup&gt;
&lt;/Project&gt;
</code></pre>
<h4>Justfile Commands</h4>
<p>We recommend <a href="https://github.com/casey/just">just</a> as a command runner - it's like <code>make</code> but simpler and cross-platform. Here's how to set up test commands:</p>
<pre><code class="language-just"># Run tests (.NET)
test:
    dotnet run --project Tests/Tests.fsproj

# Build tests to Python
build-tests:
    dotnet fable Tests/ --lang python --outDir output/tests

# Run tests (Python)
test-python: build-tests
    uv run python output/tests/program.py

# Run all tests (both platforms)
test-all: test test-python
</code></pre>
<h3>Testing Async Code</h3>
<p>Both approaches support testing async code. With Pyxpecto:</p>
<pre><code class="language-fsharp">testCase "async operations work" &lt;| fun _ -&gt;
    let computation = task {
        let! a = asyncio.sleep(0.01, 10)
        let! b = asyncio.sleep(0.01, 20)
        return a + b
    }

    let result = asyncio.run computation
    Expect.equal result 30 "async sum should work"
</code></pre>
<h3>Best Practices</h3>
<ol>
<li><p><strong>Test on both platforms</strong>: Subtle differences between .NET and Python can cause bugs. Dual-target testing catches these early.</p>
</li>
<li><p><strong>Use descriptive test names</strong>: F# allows backtick identifiers, so use them for readable names like <code>test addition works</code>.</p>
</li>
<li><p><strong>Keep tests focused</strong>: Each test should verify one behavior.</p>
</li>
<li><p><strong>Prefer Expect assertions</strong>: They provide better error messages than raw assertions.</p>
</li>
<li><p><strong>Organize with testList</strong>: Group related tests for better output.</p>
</li>
</ol>
<h3>Summary</h3>
<table>
<thead>
<tr>
<th>Approach</th>
<th>Best For</th>
<th>Runner</th>
</tr>
</thead>
<tbody><tr>
<td>Fable.Python.Testing</td>
<td>Simple tests, pytest integration</td>
<td>pytest</td>
</tr>
<tr>
<td>Expecto/Pyxpecto</td>
<td>Functional composition, better messages</td>
<td>Pyxpecto</td>
</tr>
</tbody></table>
<p>Both approaches work well with Fable.Python. For most projects, <code>Fable.Python.Testing</code> provides the simplest path - just open the module and start writing <code>[&lt;Fact&gt;]</code> tests that pytest discovers automatically.</p>
<h2>Fable v5: What's New</h2>
<p>Fable v5 brings significant improvements to the Python target, with a focus on correctness, modern Python support, and better interoperability.</p>
<h3>.NET 10 and F# 9.0 Support</h3>
<p>Fable v5 uses native MSBuild for parsing projects instead of Buildalyzer. This avoids creating fake .csproj files which could confuse IDEs.</p>
<p>Key improvements include:</p>
<ul>
<li><p><strong>Nullable Reference Types</strong> - F# 9's compile-time null-safety</p>
</li>
<li><p><strong>Many BCL additions</strong> - Expanded .NET Base Class Library support</p>
</li>
<li><p><strong>63 bug fixes</strong> - Improved stability across all targets</p>
</li>
<li><p><strong>300+ new tests</strong> - Ensuring reliability</p>
</li>
</ul>
<h3>Python Target Highlights</h3>
<p>The Python target has received special attention in v5:</p>
<ul>
<li><p><strong>Python 3.12-3.14 support</strong> (3.10/3.11 are deprecated)</p>
</li>
<li><p><strong>fable-library via PyPI</strong> - No more bundled runtime files</p>
</li>
<li><p><strong>Modern type parameter syntax</strong> - Better type hinting in generated code</p>
</li>
<li><p><code>Py.Decorate</code> <strong>attribute</strong> - Add Python decorators from F#</p>
</li>
<li><p><code>Py.ClassAttributes</code> <strong>attribute</strong> - Fine-grained class generation control</p>
</li>
<li><p><strong>Improved Pydantic interop</strong> - First-class support for data validation</p>
</li>
</ul>
<h3>Rust Core with PyO3</h3>
<p>One of the biggest changes is that the core of fable-library is now written in <strong>Rust</strong> using PyO3. The motivation is <strong>correctness</strong>, not performance:</p>
<h4>Why Rust?</h4>
<ul>
<li><p><strong>Correct .NET semantics</strong> - Sized/signed integers (int8, int16, int32, int64, uint8, etc.)</p>
</li>
<li><p><strong>Proper overflow behavior</strong> - Matches .NET exactly</p>
</li>
<li><p><strong>Fixed-size arrays</strong> - No more Python list quirks for byte streams</p>
</li>
<li><p><strong>Reliable numerics</strong> - Fable 4's pure Python numerics were a constant source of bugs</p>
</li>
</ul>
<p>While Rust is fast, don't expect dramatic speedups for typical F# code. Many F# functions are higher-order and callback to Python - <code>List.map</code>, <code>List.filter</code>, <code>Seq.fold</code>, etc. all invoke your Python lambdas. The Rust core handles the data structures correctly; your code still runs at Python speed.</p>
<h3>fable-library via PyPI</h3>
<p>Before Fable v5, the runtime was bundled in the NuGet package and copied to your output directory. Now it's a simple pip/uv dependency:</p>
<pre><code class="language-bash"># Install with pip
pip install fable-library

# Or with uv (recommended)
uv add fable-library
</code></pre>
<p>For projects, pin your dependencies in <code>pyproject.toml</code>. For stable releases use a minimum version constraint:</p>
<pre><code class="language-toml">dependencies = ["fable-library&gt;=5.0.0"]
</code></pre>
<p>For pre-release versions, pin the exact version to avoid surprises:</p>
<pre><code class="language-toml">dependencies = ["fable-library==5.0.0rc2"]
</code></pre>
<p>This makes dependency management much simpler and follows Python conventions.</p>
<h3>Test Coverage</h3>
<p>Fable v5 significantly increased test coverage across all targets:</p>
<table>
<thead>
<tr>
<th>Target</th>
<th>Fable 4.9</th>
<th>Fable 5</th>
<th>Increase</th>
</tr>
</thead>
<tbody><tr>
<td><strong>JavaScript</strong></td>
<td>2,589</td>
<td>2,748</td>
<td>+159 (+6%)</td>
</tr>
<tr>
<td><strong>Python</strong></td>
<td>1,880</td>
<td>1,974</td>
<td>+94 (+5%)</td>
</tr>
<tr>
<td><strong>Rust</strong></td>
<td>2,118</td>
<td>2,184</td>
<td>+66 (+3%)</td>
</tr>
</tbody></table>
<p>That's <strong>319 new tests</strong> ensuring reliability across the board.</p>
<h3>Getting Started with Fable v5</h3>
<p>To use Fable v5, install the CLI:</p>
<pre><code class="language-bash"># Install Fable 5 CLI
dotnet tool install fable --version 5.0.0-rc.2

# Add Fable.Core to your project
dotnet add package Fable.Core --version 5.0.0-rc.1

# Install the Python runtime
uv add fable-library==5.0.0rc2
</code></pre>
<p>Then compile your F# to Python:</p>
<pre><code class="language-bash">dotnet fable YourProject.fsproj --lang python -o output/
</code></pre>
<p>The generated Python code will be modern, type-hinted, and ready to run.</p>
<h2>Pydantic Interop</h2>
<h3>What is Pydantic?</h3>
<p><a href="https://docs.pydantic.dev/">Pydantic</a> is Python's most popular data validation library. It's the de facto standard for modern Python APIs - FastAPI, LangChain, and countless other frameworks rely on it.</p>
<p>Pydantic gives you:</p>
<ul>
<li><p><strong>Runtime type validation</strong> - Catch bad data before it causes problems</p>
</li>
<li><p><strong>Automatic serialization</strong> - JSON/dict conversion built-in</p>
</li>
<li><p><strong>Schema generation</strong> - OpenAPI/JSON Schema for free</p>
</li>
<li><p><strong>IDE support</strong> - Full autocomplete from type hints</p>
</li>
</ul>
<p>Fable v5 introduces attributes that make F# and Pydantic work together seamlessly.</p>
<h3>Creating Models in F<code>#</code></h3>
<h4>Using ClassAttributes</h4>
<p>The <code>Py.ClassAttributes</code> attribute controls how class members are generated, which is essential for Pydantic compatibility:</p>
<pre><code class="language-fsharp">[&lt;Py.DataClass&gt;]
type User() =
    inherit BaseModel()
    member val Name: string = "" with get, set
    member val Age: int = 0 with get, set
    member val Email: string option = None with get, set
</code></pre>
<p>This generates clean Pydantic code:</p>
<pre><code class="language-python">from pydantic import BaseModel

class User(BaseModel):
    Name: str = ""
    Age: int = 0
    Email: str | None = None
</code></pre>
<p>The <code>Py.DataClass</code> attribute is shorthand for <code>Py.ClassAttributes(style = Attributes, init = false)</code>. It tells Fable to generate class-level type annotations (what Pydantic expects) rather than instance attributes set in <code>__init__</code>.</p>
<h4>The Decorator Attribute</h4>
<p>For simpler cases like dataclasses, use <code>Py.Decorate</code>:</p>
<pre><code class="language-fsharp">[&lt;Py.Decorate("dataclasses.dataclass")&gt;]
type Person = {
    Name: string
    Age: int
}
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">@dataclass(eq=False, repr=False, slots=True)
class Person(Record):
    name: str
    age: int32
</code></pre>
<p>You can pass parameters to decorators:</p>
<pre><code class="language-fsharp">[&lt;Py.Decorate("dataclasses.dataclass", "frozen=True, slots=True")&gt;]
type Point = {
    X: float
    Y: float
}
</code></pre>
<p>The <code>frozen=True</code> makes instances immutable (matching F# record semantics).</p>
<h3>Fields and Validation</h3>
<p>Pydantic's <code>Field()</code> function lets you add constraints and metadata to fields. The <code>Fable.Python.Pydantic</code> module provides typed helpers:</p>
<pre><code class="language-fsharp">[&lt;Py.DataClass&gt;]
type Product() =
    inherit BaseModel()

    member val Name: string = "" with get, set

    // Field with description
    member val Description: Field&lt;string&gt; = Field.Description "Product description" with get, set

    // Field with numeric constraints
    member val Price: Field&lt;float&gt; = Field.Ge 0.0 with get, set // price &gt;= 0

    // Field with string constraints
    member val Sku: Field&lt;string&gt; = Field.Pattern "^[A-Z]{2}-[0-9]{4}$" with get, set // e.g., "AB-1234"
</code></pre>
<p>Available field constraints:</p>
<table>
<thead>
<tr>
<th>Function</th>
<th>Constraint</th>
</tr>
</thead>
<tbody><tr>
<td><code>Field.Gt</code></td>
<td>Greater than</td>
</tr>
<tr>
<td><code>Field.Ge</code></td>
<td>Greater than or equal</td>
</tr>
<tr>
<td><code>Field.Lt</code></td>
<td>Less than</td>
</tr>
<tr>
<td><code>Field.Le</code></td>
<td>Less than or equal</td>
</tr>
<tr>
<td><code>Field.MinLength</code></td>
<td>Minimum string length</td>
</tr>
<tr>
<td><code>Field.MaxLength</code></td>
<td>Maximum string length</td>
</tr>
<tr>
<td><code>Field.Pattern</code></td>
<td>Regex pattern</td>
</tr>
<tr>
<td><code>Field.Default</code></td>
<td>Default value</td>
</tr>
<tr>
<td><code>Field.Description</code></td>
<td>Field description</td>
</tr>
</tbody></table>
<h3>Importing Python-Defined Models</h3>
<p>Sometimes you need to use Pydantic models defined in Python - perhaps from an OpenAPI generator, a Python team, or an existing codebase. Here's the pattern:</p>
<p>Given a Python model in <code>models.py</code>:</p>
<pre><code class="language-python">from pydantic import BaseModel

class Customer(BaseModel):
    id: int
    name: str
    email: str | None = None
</code></pre>
<p>Create F# bindings:</p>
<pre><code class="language-fsharp">/// Customer model imported from models.py
[&lt;Import("Customer", "models")&gt;]
type Customer =
    abstract id: int with get, set
    abstract name: string with get, set
    abstract email: string option with get, set

/// Helper module for creating instances
[&lt;RequireQualifiedAccess&gt;]
module Customer =
    [&lt;Import("Customer", "models")&gt;]
    [&lt;Emit("\(0(id=\)1, name=\(2, email=\)3)")&gt;]
    let create (id: int) (name: string) (email: string option) : Customer = nativeOnly
</code></pre>
<p>Now you can use the Python model from F# with full type safety:</p>
<pre><code class="language-fsharp">let customer = Customer.create 1 "Alice" (Some "alice@example.com")

let showCustomer (c: Customer) =
    printfn "Customer %d: %s" c.id c.name

    match c.email with
    | Some email -&gt; printfn "  Email: %s" email
    | None -&gt; printfn "  No email on file"
</code></pre>
<p>This pattern is useful when you want to:</p>
<ul>
<li><p>Use models generated from OpenAPI specs</p>
</li>
<li><p>Integrate with an existing Python codebase</p>
</li>
<li><p>Share models between Python and F# code</p>
</li>
</ul>
<h3>Type Mappings</h3>
<p>F# types map naturally to Python/Pydantic types:</p>
<table>
<thead>
<tr>
<th>F# Type</th>
<th>Python Type</th>
<th>Notes</th>
</tr>
</thead>
<tbody><tr>
<td><code>string</code></td>
<td><code>str</code></td>
<td></td>
</tr>
<tr>
<td><code>int</code></td>
<td><code>int</code></td>
<td></td>
</tr>
<tr>
<td><code>float</code></td>
<td><code>float</code></td>
<td></td>
</tr>
<tr>
<td><code>bool</code></td>
<td><code>bool</code></td>
<td></td>
</tr>
<tr>
<td><code>'T option</code></td>
<td><code>Optional[T]</code> (*)</td>
<td>Modern union syntax</td>
</tr>
<tr>
<td><code>'T list</code></td>
<td><code>list[T]</code></td>
<td></td>
</tr>
<tr>
<td><code>'T array</code></td>
<td><code>list[T]</code></td>
<td></td>
</tr>
<tr>
<td>Record</td>
<td><code>class</code></td>
<td>With <code>@dataclass</code> or BaseModel</td>
</tr>
<tr>
<td>DU</td>
<td>Tagged class</td>
<td>See below</td>
</tr>
</tbody></table>
<p>(*) Generated as <code>T | None</code> in Python 3.12+</p>
<h4>F# Option to Python Union</h4>
<p>Notice how <code>string option</code> becomes <code>str | None</code> in Python. Fable v5 uses modern Python union syntax for optional types, making the generated code feel native to Python developers.</p>
<h3>Serialization</h3>
<p>Pydantic models have built-in serialization methods:</p>
<pre><code class="language-fsharp">let serializationExample () =
    let user = User()
    user.Name &lt;- "Alice"
    user.Age &lt;- 30
    user.Email &lt;- Some "alice@example.com"

    // Convert to dictionary
    let dict = user.model_dump ()

    // Convert to JSON string
    let json = user.model_dump_json ()

    // Pretty-printed JSON
    let prettyJson = user.model_dump_json_indented 2

    printfn "JSON: %s" json
</code></pre>
<p>The <code>model_dump()</code> and <code>model_dump_json()</code> methods are available on any class that inherits from <code>BaseModel</code>.</p>
<h3>The DTO Boundary Pattern</h3>
<p>A Pydantic model is not your domain - it's a <strong>Data Transfer Object (DTO)</strong>. This distinction is important for well-architected applications:</p>
<pre><code class="language-text">┌─────────────────┐         ┌─────────────────┐         ┌─────────────────┐
│   F# Domain     │   →→→   │   Pydantic DTO  │   →→→   │   JSON / API    │
│                 │   map   │                 │   dump  │                 │
│  UserId (Guid)  │         │  Id: str        │         │  "id": "a1b2.." │
│  Age: int32     │         │  Age: int       │         │  "age": 42      │
│  Balance: Money │         │  Amount: float  │         │  "amount": 3.14 │
└─────────────────┘         └─────────────────┘         └─────────────────┘
</code></pre>
<h4>Different Concerns, Different Types</h4>
<table>
<thead>
<tr>
<th>Concern</th>
<th>Domain Types</th>
<th>Transfer Types</th>
</tr>
</thead>
<tbody><tr>
<td>Purpose</td>
<td>Model business logic</td>
<td>Cross-boundary communication</td>
</tr>
<tr>
<td>Semantics</td>
<td>Rich (overflow, precision)</td>
<td>Simple (JSON-compatible)</td>
</tr>
<tr>
<td>Validation</td>
<td>Business rules</td>
<td>Schema conformance</td>
</tr>
<tr>
<td>Stability</td>
<td>Can evolve internally</td>
<td>API contract</td>
</tr>
</tbody></table>
<h4>Domain Types vs DTO Types</h4>
<pre><code class="language-fsharp">/// Domain model - uses precise F# types
type UserId = UserId of System.Guid

type Money = {
    Amount: decimal
    Currency: string
}

type DomainUser = {
    Id: UserId
    Name: string
    Age: int32 // Bounded, wrapping arithmetic
    Balance: Money
}

/// DTO - uses Python-native types for serialization
[&lt;Py.DataClass&gt;]
type UserDTO() =
    inherit BaseModel()
    member val Id: string = "" with get, set
    member val Name: string = "" with get, set
    member val Age: int = 0 with get, set
    member val BalanceAmount: float = 0.0 with get, set
    member val BalanceCurrency: string = "" with get, set
</code></pre>
<h4>The Mapping Layer</h4>
<p>Explicit transformation between domain and DTO:</p>
<pre><code class="language-fsharp">module UserMapping =
    let toDTO (user: DomainUser) : UserDTO =
        let dto = UserDTO()

        dto.Id &lt;-
            match user.Id with
            | UserId guid -&gt; string guid

        dto.Name &lt;- user.Name
        dto.Age &lt;- int user.Age
        dto.BalanceAmount &lt;- float user.Balance.Amount
        dto.BalanceCurrency &lt;- user.Balance.Currency
        dto

    let fromDTO (dto: UserDTO) : Result&lt;DomainUser, string&gt; =
        try
            Ok {
                Id = UserId(System.Guid.Parse dto.Id)
                Name = dto.Name
                Age = int32 dto.Age
                Balance = {
                    Amount = decimal dto.BalanceAmount
                    Currency = dto.BalanceCurrency
                }
            }
        with ex -&gt;
            Error ex.Message
</code></pre>
<h4>Why This Pattern?</h4>
<p>The "boilerplate" of separate DTO types is actually valuable:</p>
<ol>
<li><p><strong>Serialization just works</strong> - DTOs use Python-native types</p>
</li>
<li><p><strong>Domain integrity preserved</strong> - Your <code>int32</code> still has proper wrapping behavior</p>
</li>
<li><p><strong>Clear boundaries</strong> - The mapping layer handles validation and transformation</p>
</li>
<li><p><strong>API evolution</strong> - DTOs can change independently of domain types</p>
</li>
</ol>
<p>The visual difference between F# records and Pydantic classes is a <strong>feature</strong> - it's a speed bump that makes you think about the boundary you're crossing.</p>
<h3>Why This Matters</h3>
<p>This interop enables powerful patterns:</p>
<ol>
<li><p><strong>Define models in F#</strong> with full type safety and pattern matching</p>
</li>
<li><p><strong>Generate Python classes</strong> that integrate with the Python ecosystem</p>
</li>
<li><p><strong>Use Pydantic validation</strong> in FastAPI, LangChain, and other frameworks</p>
</li>
<li><p><strong>Publish to PyPI</strong> - Your F# types become Python packages</p>
</li>
</ol>
<p>You get the best of both worlds: F#'s type safety during development, and Python's rich ecosystem at runtime.</p>
<p>In the next chapter, we'll see how to use these Pydantic models with FastAPI to build type-safe web APIs.</p>
<h2>FastAPI</h2>
<p>As an F# developer you are probably familiar with web frameworks like ASP.NET Core, Giraffe, or Oxpecker. But Fable.Python also allows you to build web APIs that run in Python environments, using the popular FastAPI framework.</p>
<h3>What is FastAPI?</h3>
<p><a href="https://fastapi.tiangolo.com/">FastAPI</a> is Python's most popular modern web framework. It's fast, easy to use, and built on top of Pydantic for automatic request validation and OpenAPI documentation.</p>
<p>FastAPI gives you:</p>
<ul>
<li><p><strong>High performance</strong> - One of the fastest Python frameworks available</p>
</li>
<li><p><strong>Automatic validation</strong> - Request/response validation via Pydantic, that you already know from the previous chapter</p>
</li>
<li><p><strong>Type hints</strong> - Leverages Python type hints for validation and better editor support</p>
</li>
<li><p><strong>OpenAPI docs</strong> - Interactive Swagger UI and ReDoc generated automatically</p>
</li>
<li><p><strong>Async support</strong> - Native async/await for high concurrency</p>
</li>
</ul>
<p>Fable.Python includes bindings for FastAPI, allowing you to write type-safe APIs using F# while leveraging Python's mature web ecosystem.</p>
<h3>Setting Up</h3>
<p>Add FastAPI and uvicorn to your Python environment:</p>
<pre><code class="language-bash">uv add fastapi uvicorn
</code></pre>
<p>Then import the FastAPI module in your F# code:</p>
<pre><code class="language-fsharp">open Fable.Python.FastAPI
open Fable.Python.Pydantic
</code></pre>
<h3>Creating the Application</h3>
<p>Create a FastAPI application instance at the module level:</p>
<pre><code class="language-fsharp">let app = FastAPI(title = "My API", version = "1.0.0")
</code></pre>
<p>This generates:</p>
<pre><code class="language-python">app: FastAPI = FastAPI(title="My API", description=None, version="1.0.0")
</code></pre>
<p>The <code>app</code> variable name is important - the route decorators reference it.</p>
<h3>Defining Models</h3>
<p>Request and response models use Pydantic's <code>BaseModel</code> (covered in the previous chapter):</p>
<pre><code class="language-fsharp">[&lt;Py.DataClass&gt;]
type Item(Id: int, Name: string, Price: float, InStock: bool) =
    inherit BaseModel()
    member val Id: int = Id with get, set
    member val Name: string = Name with get, set
    member val Price: float = Price with get, set
    member val InStock: bool = InStock with get, set

[&lt;Py.DataClass&gt;]
type CreateItemRequest(Name: string, Price: float, InStock: bool) =
    inherit BaseModel()
    member val Name: string = Name with get, set
    member val Price: float = Price with get, set
    member val InStock: bool = InStock with get, set
</code></pre>
<h3>Defining Endpoints</h3>
<h4>The APIClass Pattern</h4>
<p>FastAPI endpoints are defined using a class with decorated static methods:</p>
<pre><code class="language-fsharp">let items = ResizeArray&lt;Item&gt;()

[&lt;APIClass&gt;]
type API() =
    /// GET /items - List all items
    [&lt;Get("/items")&gt;]
    static member get_items() : ResizeArray&lt;Item&gt; = items

    /// GET /items/{item_id} - Get item by ID
    [&lt;Get("/items/{item_id}")&gt;]
    static member get_item(item_id: int) : Task&lt;obj&gt; =
        task {
            match items |&gt; Seq.tryFind (fun i -&gt; i.Id = item_id) with
            | Some item -&gt; return item :&gt; obj
            | None -&gt; return {| error = "Item not found" |}
        }

    /// POST /items - Create a new item
    [&lt;Post("/items")&gt;]
    static member create_item(request: CreateItemRequest) : Task&lt;obj&gt; =
        task {
            let newId =
                if items.Count = 0 then
                    1
                else
                    (items |&gt; Seq.map (fun i -&gt; i.Id) |&gt; Seq.max) + 1

            let newItem = Item(newId, request.Name, request.Price, request.InStock)
            items.Add(newItem)

            return {|
                status = "created"
                item = newItem
            |}
        }
</code></pre>
<p>This generates Python with proper FastAPI decorators:</p>
<pre><code class="language-python">class API:
    @app.get("/items")
    @staticmethod
    def get_items(__unit: Unit = UNIT) -&gt; list[Item]:
        return items

    @app.get("/items/{item_id}")
    @staticmethod
    async def get_item(item_id: int32) -&gt; Any:
        builder_0040: Any = task()

        def _arrow97(__unit: Unit = UNIT) -&gt; Callable[[FSharpRef[Any]], bool]:
            def predicate(i: Item) -&gt; bool:
                return i.Id == item_id

            match_value: Item | None = erase(try_find(predicate, to_enumerable(items)))
            if match_value is None:
                return builder_0040.Return({"error": "Item not found"})

            else:
                item: Item = match_value
                return builder_0040.Return(item)

        return await builder_0040.Run(builder_0040.Delay(_arrow97))

    @app.post("/items")
    @staticmethod
    async def create_item(request: CreateItemRequest) -&gt; Any:
        builder_0040: Any = task()

        def _arrow99(__unit: Unit = UNIT) -&gt; Callable[[FSharpRef[Any]], bool]:
            def mapping(i: Item) -&gt; int32:
                return i.Id

            class ObjectExpr98:
                def Compare(self, x: int32, y: int32) -&gt; int32:
                    return compare_primitives(x, y)

            new_item: Item = Item(
                Id=int32.ONE
                if (int32(len(items)) == int32.ZERO)
                else (
                    max(map(mapping, to_enumerable(items)), ObjectExpr98()) + int32.ONE
                ),
                Name=request.Name,
                Price=request.Price,
                InStock=request.InStock,
            )
            (items.append(new_item))
            return builder_0040.Return({"item": new_item, "status": "created"})

        return await builder_0040.Run(builder_0040.Delay(_arrow99))
</code></pre>
<h4>Key Points</h4>
<ul>
<li><p><code>[&lt;APIClass&gt;]</code> marks the class for FastAPI routing. We use a class because Fable can only apply decorator attributes to types and methods, not standalone functions</p>
</li>
<li><p>Route decorators: <code>[&lt;Get&gt;]</code>, <code>[&lt;Post&gt;]</code>, <code>[&lt;Put&gt;]</code>, <code>[&lt;Delete&gt;]</code>, <code>[&lt;Patch&gt;]</code></p>
</li>
<li><p>Path parameters use <code>{param_name}</code> syntax and map to function arguments</p>
</li>
<li><p>Pydantic models in parameters are automatically validated</p>
</li>
<li><p>Return types can be sync or async (<code>Task&lt;'T&gt;</code>)</p>
</li>
</ul>
<h4>Anonymous Records for Quick Responses</h4>
<p>F# anonymous records compile to Python dictionaries, perfect for JSON responses:</p>
<pre><code class="language-fsharp">[&lt;APIClass&gt;]
type HealthAPI() =
    [&lt;Get("/health")&gt;]
    static member health() = {|
        status = "healthy"
        version = "1.0.0"
    |}
</code></pre>
<h3>Async Endpoints</h3>
<p>For I/O-bound operations, use <code>task { }</code> to create async endpoints:</p>
<pre><code class="language-fsharp">[&lt;APIClass&gt;]
type AsyncAPI() =
    [&lt;Get("/slow")&gt;]
    static member slow_operation() =
        task {
            // Simulate async work (e.g., database query)
            do! Task.Delay(100)
            return {| message = "Done!" |}
        }
</code></pre>
<p>The <code>task { }</code> computation expression compiles to Python's <code>async def</code>, integrating naturally with FastAPI's async support.</p>
<h3>Path and Query Parameters</h3>
<h4>Path Parameters</h4>
<p>Path parameters are extracted from the URL:</p>
<pre><code class="language-fsharp">[&lt;APIClass&gt;]
type UsersAPI() =
    [&lt;Get("/users/{user_id}")&gt;]
    static member get_user(user_id: int) = {|
        id = user_id
        name = "User " + string user_id
    |}

    [&lt;Get("/users/{user_id}/posts/{post_id}")&gt;]
    static member get_user_post(user_id: int, post_id: int) = {|
        user_id = user_id
        post_id = post_id
    |}
</code></pre>
<h4>Query Parameters</h4>
<p>Query parameters are function arguments not in the path:</p>
<pre><code class="language-fsharp">[&lt;APIClass&gt;]
type SearchAPI() =
    [&lt;Get("/search")&gt;]
    static member search(q: string, limit: int) = {|
        query = q
        limit = limit
    |}
</code></pre>
<p>A request to <code>/search?q=hello&amp;limit=10</code> maps to <code>search("hello", 10)</code>.</p>
<h3>Request Bodies</h3>
<p>POST/PUT/PATCH endpoints receive request bodies as Pydantic models:</p>
<pre><code class="language-fsharp">[&lt;Py.DataClass&gt;]
type CreateUserRequest(name: string, email: string) =
    inherit BaseModel()
    member val name: string = name with get, set
    member val email: string = email with get, set

[&lt;APIClass&gt;]
type UserCrudAPI() =
    [&lt;Post("/users")&gt;]
    static member create_user(request: CreateUserRequest) =
        // FastAPI automatically validates the request body
        {|
            status = "created"
            name = request.name
            email = request.email
        |}
</code></pre>
<p>FastAPI validates the incoming JSON against the Pydantic model and returns a 422 error if validation fails.</p>
<h3>HTTP Exceptions</h3>
<p>Return proper HTTP errors using <code>HTTPException</code>:</p>
<pre><code class="language-fsharp">[&lt;APIClass&gt;]
type ErrorAPI() =
    [&lt;Get("/protected")&gt;]
    static member protected_route() =
        // Check authentication (simplified example)
        let isAuthenticated = false

        if not isAuthenticated then
            raise (System.Exception("Not authenticated"))

        {| message = "Secret data" |}
</code></pre>
<p>In practice, you would use FastAPI's dependency injection for authentication. The <code>HTTPException</code> type is available for more idiomatic error handling:</p>
<pre><code class="language-fsharp">// For proper HTTP exceptions, use a helper that emits Python's raise
[&lt;Emit("raise HTTPException(status_code=\(0, detail=\)1)")&gt;]
let raiseHttp (code: int) (msg: string) : unit = nativeOnly

// Then in your endpoint:
if not isAuthenticated then
    raiseHttp 401 "Not authenticated"
</code></pre>
<h3>Running the Application</h3>
<p>Compile with Fable and run with uvicorn:</p>
<pre><code class="language-bash"># Compile F# to Python
dotnet fable --lang python --outDir build

# Run the server
cd build
uvicorn app:app --reload
</code></pre>
<p>Visit:</p>
<ul>
<li><p><code>http://localhost:8000</code> - Your API</p>
</li>
<li><p><code>http://localhost:8000/docs</code> - Interactive Swagger UI</p>
</li>
<li><p><code>http://localhost:8000/redoc</code> - ReDoc documentation</p>
</li>
</ul>
<h3>Development Workflow</h3>
<p>For hot-reloading during development, run Fable in watch mode:</p>
<pre><code class="language-bash"># Terminal 1: Watch F# files
dotnet fable --lang python --outDir build --watch

# Terminal 2: Run uvicorn with reload
cd build
uvicorn app:app --reload
</code></pre>
<p>Changes to your F# code automatically recompile and uvicorn picks up the changes.</p>
<h3>Complete Example</h3>
<p>Here's a minimal but complete FastAPI application:</p>
<pre><code class="language-fsharp">module App

open System.Threading.Tasks
open Fable.Core
open Fable.Python.FastAPI
open Fable.Python.Pydantic

// Create the app
let app = FastAPI(title = "Todo API", version = "1.0.0")

// Define the model
[&lt;Py.DataClass&gt;]
type Todo(id: int, title: string, completed: bool) =
    inherit BaseModel()
    member val id: int = id with get, set
    member val title: string = title with get, set
    member val completed: bool = completed with get, set

// In-memory store
let todos = ResizeArray&lt;Todo&gt;()

// Define endpoints
[&lt;APIClass&gt;]
type TodoAPI() =
    [&lt;Get("/")&gt;]
    static member root() =
        {| message = "Welcome to Todo API" |}

    [&lt;Get("/todos")&gt;]
    static member list_todos() = todos

    [&lt;Post("/todos")&gt;]
    static member create_todo(title: string) =
        let todo = Todo(todos.Count + 1, title, false)
        todos.Add(todo)
        todo
</code></pre>
<h3>Why F# + FastAPI?</h3>
<p>This combination gives you:</p>
<ol>
<li><p><strong>Compile-time safety</strong> - F# catches errors before they reach Python</p>
</li>
<li><p><strong>Runtime validation</strong> - Pydantic validates incoming requests</p>
</li>
<li><p><strong>Auto documentation</strong> - OpenAPI specs generated from your types</p>
</li>
<li><p><strong>Familiar ecosystem</strong> - Deploy with standard Python tools</p>
</li>
</ol>
<p>You write type-safe F# code, but deploy and run it like any Python web service.</p>
<h3>Hybrid Architecture: F# Backend with Python Endpoints</h3>
<p>Another compelling use case is when you have an existing web service written in F# (using ASP.NET Core, Giraffe, or Oxpecker) but need access to the Python ecosystem for specific functionality. You can use FastAPI to expose endpoints that leverage Python libraries, while your main service remains in F#.</p>
<p>This hybrid approach works well when you need:</p>
<h4>AI/ML Libraries</h4>
<ul>
<li><p><strong>LangChain</strong> / <strong>LlamaIndex</strong> - LLM orchestration and RAG pipelines</p>
</li>
<li><p><strong>Hugging Face Transformers</strong> - Pre-trained models for NLP, vision, audio</p>
</li>
<li><p><strong>OpenAI SDK</strong> / <strong>Anthropic SDK</strong> - LLM API integration with structured outputs</p>
</li>
<li><p><strong>scikit-learn</strong> - Classical machine learning models</p>
</li>
<li><p><strong>PyTorch</strong> / <strong>TensorFlow</strong> - Deep learning inference</p>
</li>
</ul>
<h4>Data Science &amp; Analytics</h4>
<ul>
<li><p><strong>Pandas</strong> / <strong>Polars</strong> - Data manipulation and analysis</p>
</li>
<li><p><strong>NumPy</strong> - Numerical computing</p>
</li>
<li><p><strong>Matplotlib</strong> / <strong>Plotly</strong> - Chart and visualization generation</p>
</li>
<li><p><strong>Apache Arrow</strong> - Efficient cross-language data interchange</p>
</li>
</ul>
<h4>Document Processing</h4>
<ul>
<li><p><strong>PyMuPDF</strong> / <strong>pdfplumber</strong> - PDF text and table extraction</p>
</li>
<li><p><strong>python-docx</strong> - Word document generation</p>
</li>
<li><p><strong>Pillow</strong> - Image processing and manipulation</p>
</li>
<li><p><strong>OpenCV</strong> - Computer vision operations</p>
</li>
</ul>
<h4>Specialized APIs</h4>
<ul>
<li><p><strong>boto3</strong> - AWS services (S3, Lambda, SQS, etc.)</p>
</li>
<li><p><strong>google-cloud-</strong>* - GCP services (BigQuery, Cloud Storage, Vertex AI)</p>
</li>
</ul>
<h4>Scientific Computing</h4>
<ul>
<li><p><strong>SciPy</strong> - Scientific algorithms and optimization</p>
</li>
<li><p><strong>SymPy</strong> - Symbolic mathematics</p>
</li>
<li><p><strong>NetworkX</strong> - Graph algorithms and analysis</p>
</li>
</ul>
<p>The pattern is straightforward: your F# service handles core domain logic and type-safe business rules, while specific endpoints delegate to a FastAPI service for capabilities where Python dominates. This is especially powerful for AI/ML workloads where the Python ecosystem is unmatched.</p>
<h2>Units of Measure</h2>
<p>One of F#'s most powerful features for scientific and engineering code is <strong>units of measure</strong> - compile-time dimensional analysis that prevents unit-related bugs.</p>
<h3>The Problem</h3>
<p>Unit errors are a classic source of bugs. The famous Mars Climate Orbiter was lost because one team used metric units while another used imperial. Python can't catch these errors:</p>
<pre><code class="language-python"># Python - no protection
distance = 100  # meters? feet? who knows!
time = 9.58     # seconds? minutes?
speed = distance / time  # ???
</code></pre>
<h3>F# Units of Measure</h3>
<p>F# lets you annotate numeric types with units that are checked at compile time:</p>
<pre><code class="language-fsharp">[&lt;Measure&gt;]
type m // meters

[&lt;Measure&gt;]
type s // seconds

[&lt;Measure&gt;]
type kg // kilograms
</code></pre>
<p>Now we can define values with units:</p>
<pre><code class="language-fsharp">let distance = 100.0&lt;m&gt;
let time = 9.58&lt;s&gt;
let speed = distance / time // Automatically inferred as float&lt;m/s&gt;
</code></pre>
<p>The compiler tracks units through all operations. Division of meters by seconds gives meters-per-second. This is all checked at compile time.</p>
<h3>Preventing Errors</h3>
<p>Try to add incompatible units and the compiler stops you:</p>
<pre><code class="language-fsharp">let distance = 100.0&lt;m&gt;
let mass = 50.0&lt;kg&gt;

// This won't compile:
// let nonsense = distance + mass
// Error: The unit of measure 'm' does not match 'kg'
</code></pre>
<h3>Derived Units</h3>
<p>You can define derived units based on existing ones:</p>
<pre><code class="language-fsharp">[&lt;Measure&gt;]
type N = kg * m / s^2 // Newton

[&lt;Measure&gt;]
type J = N * m // Joule

let force = 10.0&lt;N&gt;
let displacement = 5.0&lt;m&gt;
let work = force * displacement // Inferred as float&lt;J&gt;
</code></pre>
<h3>Real-World Example: Physics Simulation</h3>
<p>Here's a practical example computing kinetic energy:</p>
<pre><code class="language-fsharp">let kineticEnergy (mass: float&lt;kg&gt;) (velocity: float&lt;m / s&gt;) : float&lt;J&gt; = 0.5 * mass * velocity * velocity

let carMass = 1500.0&lt;kg&gt;
let carSpeed = 30.0&lt;m / s&gt;
let energy = kineticEnergy carMass carSpeed
</code></pre>
<p>The function signature clearly documents what units are expected and returned. The compiler ensures you can't accidentally pass velocity where mass is expected.</p>
<h3>Unit Conversions</h3>
<p>Define conversion functions with explicit unit transformations:</p>
<pre><code class="language-fsharp">[&lt;Measure&gt;]
type km

[&lt;Measure&gt;]
type h

let metersToKm (d: float&lt;m&gt;) : float&lt;km&gt; = d / 1000.0&lt;m / km&gt;
let secondsToHours (t: float&lt;s&gt;) : float&lt;h&gt; = t / 3600.0&lt;s / h&gt;

let marathonDistance = 42195.0&lt;m&gt;
let marathonKm = metersToKm marathonDistance // 42.195&lt;km&gt;
</code></pre>
<h3>Generated Python</h3>
<p>When compiled to Python, units are erased (they're purely a compile-time feature), but your code is guaranteed to be unit-safe:</p>
<pre><code class="language-python">def kinetic_energy(mass: float, velocity: float) -&gt; float:
    return 0.5 * mass * velocity * velocity

car_mass: float = 1500.0
car_speed: float = 30.0
energy: float = kinetic_energy(car_mass, car_speed)
</code></pre>
<p>The Python code is clean and efficient. All the unit checking happened at compile time in F#, so there's no runtime overhead.</p>
<h3>Why This Matters for Python</h3>
<p>Python is widely used in scientific computing, but lacks compile-time unit checking. With Fable.Python, you can:</p>
<ol>
<li><p><strong>Write unit-safe code</strong> in F# with full dimensional analysis</p>
</li>
<li><p><strong>Catch unit errors at compile time</strong> before they become runtime bugs</p>
</li>
<li><p><strong>Generate clean Python</strong> that integrates with NumPy, SciPy, etc.</p>
</li>
<li><p><strong>Document intent</strong> - function signatures show expected units</p>
</li>
</ol>
<p>This is especially valuable for physics simulations, financial calculations, engineering applications, and any domain where mixing up units could be costly.</p>
<h2>Fable.Literate: The Strange Loop</h2>
<p>You've made it to the end - and here's where things get delightfully meta.</p>
<p><strong>The blog post you're reading was generated by the code in this chapter.</strong></p>
<p>This is Fable.Literate, a literate programming converter inspired by <a href="https://github.com/mwouts/jupytext">jupytext</a> and <a href="https://fsprojects.github.io/FSharp.Formatting/">FSharp.Formatting</a>. It's written in F#, compiled to Python via Fable, and it processes the <code>.fs</code> files that make up this blog - including itself.</p>
<p>The chain goes like this:</p>
<ol>
<li><p>Each chapter is an F# file with embedded Markdown comments</p>
</li>
<li><p>Fable compiles the F# to Python</p>
</li>
<li><p>Fable.Literate (this code, running as Python) extracts the documentation</p>
</li>
<li><p>The output is the Markdown you're reading right now</p>
</li>
</ol>
<p>It's a strange loop - the snake eating its tail. And it proves that Fable.Python isn't just a toy: you're looking at a real project that works.</p>
<h3>How It Works</h3>
<p>The converter follows a compiler-like architecture with three phases (just like Fable itself):</p>
<ol>
<li><p><strong>Parse</strong>: Convert source lines into a Block AST</p>
</li>
<li><p><strong>Transform</strong>: Filter hidden blocks, resolve Python includes</p>
</li>
<li><p><strong>Print</strong>: Render the AST as Markdown</p>
</li>
</ol>
<p>The input syntax:</p>
<ul>
<li><p>Lines inside <code>(** ... *)</code> blocks become Markdown</p>
</li>
<li><p>F# code outside those blocks is wrapped in fenced code blocks</p>
</li>
<li><p><code>(*** hide ***)</code> sections are excluded from output</p>
</li>
<li><p><code>(*** include-python: symbol1, symbol2 ***)</code> extracts generated Python code</p>
</li>
</ul>
<h3>AST Types</h3>
<p>The document is represented as a list of blocks. Each block represents a distinct section of the literate source file:</p>
<pre><code class="language-fsharp">/// A single block in the document AST.
type Block =
    /// Raw markdown content from (** ... *) blocks
    | Markdown of content: string
    /// F# code that should be wrapped in fenced blocks
    | FSharpCode of lines: string list
    /// Hidden content - filtered out by Transform.filterHidden
    | Hidden of lines: string list
    /// Unresolved directive to include Python symbols (from parsing)
    /// Resolved to PythonCode by Transform.resolvePythonIncludes
    | IncludePython of symbols: string list
    /// Resolved Python code (after Transform.resolvePythonIncludes)
    | PythonCode of content: string

/// A parsed document is a list of blocks.
type Document = Block list
</code></pre>
<h3>Utils Module</h3>
<p>Utility functions for naming conversion and line classification:</p>
<pre><code class="language-fsharp">module Utils =
    /// List of contributors to thank (Fable-style).
    let contributors = [| "@dbrattli"; "@alfonsogarciacaro"; "@ncave"; "@MangelMaxime"; "@claude 🤖" |]

    /// Returns a random contributor from the list.
    let randomContributor () : string =
        let rnd = Random()
        contributors.[rnd.Next(contributors.Length)]

    /// Converts camelCase to snake_case for the function part.
    let private toSnakeCase (name: string) : string =
        if name.Length &gt; 0 &amp;&amp; Char.IsLower(name.[0]) then
            System.Text.RegularExpressions.Regex.Replace(
                name,
                "[a-z]?[A-Z]",
                fun m -&gt;
                    if m.Value.Length = 1 then
                        m.Value.ToLowerInvariant()
                    else
                        m.Value.Substring(0, 1)
                        + "_"
                        + m.Value.Substring(1, 1).ToLowerInvariant()
            )
        else
            name

    /// Converts F# symbol reference to Python naming.
    /// - "Module.func" -&gt; "Module_func" (Fable keeps camelCase for module functions)
    /// - "func" -&gt; "func" (with snake_case conversion for top-level)
    let toPythonNaming (name: string) : string =
        match name.Split('.') with
        | [| moduleName; funcName |] -&gt; moduleName + "_" + funcName // Module functions stay camelCase
        | _ -&gt; toSnakeCase name // Top-level functions get snake_case

    /// Parses a comma-separated list of symbols from an include-python directive.
    let parseSymbolList (directive: string) : string list =
        // Extract content between "(*** include-python:" and "***)"
        let start = "(*** include-python:".Length
        let endPos = directive.LastIndexOf("***)")

        if endPos &gt; start then
            directive.Substring(start, endPos - start).Trim()
            |&gt; _.Split(',')
            |&gt; Array.map _.Trim()
            |&gt; Array.filter (fun s -&gt; s.Length &gt; 0)
            |&gt; Array.toList
        else
            []

    /// Active pattern for classifying source lines.
    /// - `HideCmd`: The (*** hide ***) directive
    /// - `IncludePythonCmd symbols`: The (*** include-python: sym1, sym2 ***) directive
    /// - `MarkdownSingle content`: Single-line markdown (** content *)
    /// - `MarkdownOpen content`: Start of markdown block, possibly with content
    /// - `MarkdownClose`: End of markdown block *)
    /// - `Content`: Any other line
    let (|HideCmd|IncludePythonCmd|MarkdownSingle|MarkdownOpen|MarkdownClose|Content|) (line: string) =
        let trimmed = line.Trim()

        match trimmed with
        | "(*** hide ***)" -&gt; HideCmd
        | s when s.StartsWith("(*** include-python:") &amp;&amp; s.EndsWith("***)") -&gt; IncludePythonCmd(parseSymbolList s)
        | s when s.StartsWith("(**") &amp;&amp; s.EndsWith("*)") &amp;&amp; s.Length &gt; 5 -&gt;
            MarkdownSingle(s.Substring(3, s.Length - 5).Trim())
        | s when s.StartsWith("(**") -&gt;
            let content = if s.Length &gt; 3 then s.Substring(3).Trim() else ""
            MarkdownOpen content
        | "*)" -&gt; MarkdownClose
        | _ -&gt; Content

    /// Trim empty lines from front, whitespace from end (preserving indentation).
    let trimCode (code: string) : string =
        code.TrimEnd().Split '\n'
        |&gt; Array.skipWhile String.IsNullOrWhiteSpace
        |&gt; String.concat "\n"

open Utils
</code></pre>
<h3>Parser Module</h3>
<p>The parser converts source lines into a Block AST using a fold:</p>
<pre><code class="language-fsharp">module Parser =
    /// Internal state for block accumulation during parsing.
    type private ParserState =
        | CollectingMarkdown of lines: string list
        | CollectingCode of lines: string list
        | CollectingHidden of lines: string list
        | Ready

    /// Parse context threaded through the fold.
    type private ParseContext = {
        State: ParserState
        Blocks: Block list // Accumulated blocks (in reverse)
    }
</code></pre>
<p>Parse lines into a document AST</p>
<pre><code class="language-fsharp">    /// Parse lines into a document AST.
    let parse (lines: string seq) : Document =
        let initial = {
            State = Ready
            Blocks = []
        }

        lines
        |&gt; Seq.fold parseLine initial
        |&gt; flushState
        |&gt; _.Blocks
        |&gt; List.rev
</code></pre>
<h3>Transform Module</h3>
<p>Pure transformations on the document AST:</p>
<pre><code class="language-fsharp">module Transform =
    /// Boilerplate prefixes that should be excluded from code blocks.
    let boilerplatePrefixes = [ "module "; "namespace " ]

    /// Remove Hidden blocks from the document.
    let filterHidden (doc: Document) : Document =
        doc
        |&gt; List.filter (function
            | Hidden _ -&gt; false
            | _ -&gt; true)

    /// Check if code lines are empty or boilerplate-only.
    /// Filters standalone module/namespace declarations (e.g., "module Foo" or "namespace Bar")
    /// but keeps module definitions with bodies (e.g., "module Foo =").
    let private isBoilerplate (lines: string list) : bool =
        let code = lines |&gt; String.concat "\n" |&gt; _.Trim()

        String.IsNullOrWhiteSpace code
        || code.StartsWith "namespace "
        || code.StartsWith "module " &amp;&amp; not (code.Contains "=")

    /// Remove empty or boilerplate-only code blocks.
    let filterBoilerplate (doc: Document) : Document =
        doc
        |&gt; List.filter (function
            | FSharpCode lines when isBoilerplate lines -&gt; false
            | _ -&gt; true)
</code></pre>
<h3>MarkdownPrinter Module</h3>
<p>Renders the document AST to markdown:</p>
<pre><code class="language-fsharp">module MarkdownPrinter =
    /// Render a single block to markdown.
    let private printBlock (block: Block) : string =
        match block with
        | Markdown content -&gt; $"{content}\n"
        | FSharpCode lines -&gt;
            let code = lines |&gt; String.concat "\n" |&gt; trimCode
            $"\n```fsharp\n{code}\n```\n\n"
        | PythonCode content -&gt; $"\n```python\n{content}\n```\n\n"
        | IncludePython symbols -&gt;
            // Unresolved - should have been transformed
            let symbolList = String.concat ", " symbols
            $"\n&lt;!-- include-python: {symbolList} (unresolved) --&gt;\n"
        | Hidden _ -&gt; "" // Should have been filtered

    /// Render a document to markdown string.
    let printMarkdown (doc: Document) : string =
        doc |&gt; List.map printBlock |&gt; String.concat ""
</code></pre>
<h3>Pipeline Module</h3>
<p>Composes the phases into a complete pipeline:</p>
<pre><code class="language-fsharp">module Pipeline =
    /// Standard processing pipeline.
    let standard (pythonContent: string option) (lines: string seq) : string =
        lines
        |&gt; Parser.parse
        |&gt; Transform.filterHidden
        |&gt; Transform.filterBoilerplate
        |&gt; Transform.resolvePythonIncludes pythonContent
        |&gt; MarkdownPrinter.printMarkdown
</code></pre>
<h3>Including Generated Python Code</h3>
<p>One of Fable.Literate's unique features is the ability to show the generated Python alongside the F# source. The include-python directive extracts specific symbols from the transpiled output.</p>
<p>When you pass <code>--python-file path</code> to Fable.Literate, it reads the transpiled Python and extracts the named symbols (functions, classes, or variables). This lets readers see exactly what Python code Fable generates from the F#.</p>
<p>The extraction is smart about Python syntax:</p>
<ul>
<li><p>It finds the symbol definition by matching patterns like def symbol or class symbol</p>
</li>
<li><p>It walks backwards to include any decorators</p>
</li>
<li><p>For multi-line definitions, it captures everything until the next top-level definition</p>
</li>
<li><p>It stops before dunder methods to avoid pulling in too much</p>
</li>
</ul>
<p>For example, the extractSymbol function in F# generates this Python:</p>
<pre><code class="language-python">def extract_symbol(symbol: str, lines: Array[str]) -&gt; str | None:
    """Extracts a single symbol definition from Python source lines."""

    def mapping(def_index: int32, symbol: Any = symbol, lines: Any = lines) -&gt; str:
        start_index: int32 = find_decorator_start(lines, def_index)
        if is_multiline_definition(lines[def_index]):
            return extract_multiline_body(start_index, def_index, lines)

        else:
            return lines[def_index]

    return erase(map(mapping, find_definition_index(symbol, lines)))
</code></pre>
<h3>Main Entry Point</h3>
<p>Read the input file, convert it, and print the result:</p>
<pre><code class="language-fsharp">/// Main entry point. Converts a literate F# file to Markdown.
/// Use --increase-headers flag to bump all header levels by one.
/// Use --python-file &lt;path&gt; to enable include-python directives.
#if !TESTING
[&lt;EntryPoint&gt;]
#endif
let main (args: string[]) =
    let hasFlag flag = args |&gt; Array.contains flag
    let pythonFilePath = getFlagValue "--python-file" args
    let files = getPositionalArgs args

    if files.Length &lt; 1 then
        printfn "Usage: python app.py [--increase-headers] [--python-file &lt;path.py&gt;] &lt;input.fs&gt;"
        1
    else
        // Thanks to the contributor! (Fable-style)
        eprintln $"Fable.Literate: Thanks to the contributor! {randomContributor ()}"

        // Load Python file content if provided
        let pythonContent = pythonFilePath |&gt; Option.map readFile

        let content = readFile files.[0]
        let lines = content.Split('\n')

        // Pipeline: parse -&gt; transform -&gt; print
        let markdown = lines |&gt; Pipeline.standard pythonContent

        let output =
            if hasFlag "--increase-headers" then
                MarkdownPrinter.adjustHeaderLevels markdown
            else
                markdown

        printRaw output
        0
</code></pre>
<h3>Building and Running</h3>
<pre><code class="language-bash"># Transpile to Python
dotnet fable Fable.Literate/ --lang python -o output/Fable.Literate/

# Convert a literate file
python output/Fable.Literate/app.py chapters/introduction.fs &gt; docs/introduction.md
</code></pre>
<p>That's it! A complete literate programming converter in under 200 lines of F#, compiled to Python, processing this very blog post.</p>
<h2>Summary</h2>
<p>We've covered a lot of ground in this guide:</p>
<ul>
<li><p><strong>Introduction</strong>: What Fable.Python is and why it matters</p>
</li>
<li><p><strong>F# for Python Developers</strong>: Bridging the conceptual gap between languages</p>
</li>
<li><p><strong>Getting Started</strong>: Setting up your first Fable.Python project</p>
</li>
<li><p><strong>Interop</strong>: Seamlessly calling Python libraries from F#</p>
</li>
<li><p><strong>Bindings</strong>: Creating type-safe wrappers for Python code</p>
</li>
<li><p><strong>Compatibility</strong>: Understanding what F# features work (and which don't)</p>
</li>
<li><p><strong>Async Programming</strong>: Mapping F# async to Python's asyncio</p>
</li>
<li><p><strong>Testing</strong>: Running F# code with pytest and other Python test runners</p>
</li>
<li><p><strong>Fable v5</strong>: The latest features including the Rust core and PyPI packages</p>
</li>
<li><p><strong>Pydantic</strong>: Building validated data models with Python's favorite library</p>
</li>
<li><p><strong>FastAPI</strong>: Building type-safe web APIs in the Python ecosystem</p>
</li>
<li><p><strong>Units of Measure</strong>: Compile-time dimensional analysis that vanishes (erased) at runtime</p>
</li>
<li><p><strong>Fable.Literate</strong>: A self-documenting literate programming converter</p>
</li>
</ul>
<h3>The Punchline</h3>
<p>If you're reading this, the code worked.</p>
<p>This entire blog post - every chapter, every code example, every explanation - was processed by F# code compiled to Python, and output as Markdown. The proof is in the reading.</p>
<h3>Get Involved</h3>
<p>The source code for this entire project is available on GitHub:</p>
<p><a href="https://github.com/cardamomcode/fable-python"><strong>github.com/cardamomcode/fable-python</strong></a></p>
<p>The repository contains:</p>
<ul>
<li><p>All the chapter source files (literate F#)</p>
</li>
<li><p>The Fable.Literate converter</p>
</li>
<li><p>Build scripts and configuration</p>
</li>
<li><p>The generated blog post</p>
</li>
</ul>
<p>Found a typo? Want to improve an explanation? Have a better example? Pull requests are welcome! This is a living document, and contributions from the community make it better for everyone.</p>
<h3>Resources</h3>
<ul>
<li><p><a href="https://fable.io/docs/">Fable Documentation</a></p>
</li>
<li><p><a href="https://github.com/fable-compiler/Fable.Python/">Fable.Python on GitHub</a></p>
</li>
<li><p><a href="https://fsharp.org/">F# Software Foundation</a></p>
</li>
</ul>
<p>... now go build something.</p>
]]></content:encoded></item><item><title><![CDATA[Python Type Annotations (part 3)]]></title><description><![CDATA[Python Type Annotations is a tutorial in 3 parts: Part 1 | Part 2 | Part 3 (this post)

Table of contents

Variance in Generics
Covariance
Contravariance
Invariance
References


Variance in Generics
Variance in generics refers to how subtyping relati...]]></description><link>https://cardamomcode.dev/python-type-annotations-part-3</link><guid isPermaLink="true">https://cardamomcode.dev/python-type-annotations-part-3</guid><category><![CDATA[Python]]></category><category><![CDATA[Type Annotation]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[Python advanced]]></category><category><![CDATA[type annotations]]></category><dc:creator><![CDATA[Dag Brattli]]></dc:creator><pubDate>Sat, 08 Feb 2025 11:49:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738948681251/c7dc46de-741c-453b-a8b5-0f9f002c62d6.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p><strong>Python Type Annotations</strong> is a tutorial in 3 parts: <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-1">Part 1</a> | <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2">Part 2</a> | Part 3 (this post)</p>
</blockquote>
<h2 id="heading-table-of-contents">Table of contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-variance-in-generics">Variance in Generics</a></li>
<li><a class="post-section-overview" href="#heading-covariance">Covariance</a></li>
<li><a class="post-section-overview" href="#heading-contravariance">Contravariance</a></li>
<li><a class="post-section-overview" href="#heading-invariance">Invariance</a></li>
<li><a class="post-section-overview" href="#heading-references">References</a></li>
</ul>
<hr />
<h2 id="heading-variance-in-generics">Variance in Generics</h2>
<p>Variance in generics refers to how subtyping relationships behave when they are wrapped in a generic container or function. E.g. if <code>Cat</code> is a subclass of <code>Animal</code>, then subtype polymorphism which you may already be familiar with, explains how a <code>Cat</code> can transform (morph) into, and be used in place of an <code>Animal</code>. In a similar way, variance tells us how e.g. a <code>set[Cat]</code> can transform (vary) and be used in place of a <code>set[Animal]</code> or vice versa.</p>
<p>There are three types of variance:</p>
<ol>
<li><p><strong>Covariance</strong> enables you to use a more specific type than originally specified. Example: If <code>Cat</code> is a subclass of <code>Animal</code>, you can assign an instance of <code>Iterable[Cat]</code> to a variable of type <code>Iterable[Animal]</code>.</p>
</li>
<li><p><strong>Contravariance</strong> enables you to use a more general type than originally specified. Example: If <code>Cat</code> is a subclass of <code>Animal</code>, you can assign an instance of <code>Callable[[Animal], None]</code> to a variable of type <code>Callable[[Cat], None]</code>.</p>
</li>
<li><p><strong>Invariance</strong> means that you can only use the type originally specified. An invariant generic type parameter is neither covariant nor contravariant. Example: If <code>Cat</code> is a subclass of <code>Animal</code>, you cannot assign an instance of <code>list[Animal]</code> to a variable of type <code>list[Cat]</code> or vice versa.</p>
</li>
</ol>
<p>If we look at Python types, there are already many types and combinations of types that have different variance:</p>
<ul>
<li>Function types i.e. callables are covariant on their return type, and contravariant on their arguments.</li>
<li>Mutable containers like <code>list</code> and <code>dict</code> are invariant.</li>
<li>Immutable containers like <code>tuple</code> and <code>set</code> are covariant.</li>
<li>Union types are covariant. This means that optional types are also covariant because they are equivalent to <code>T | None</code>.</li>
</ul>
<p>Understanding variance helps you writing more flexible and type-safe code, especially when working with container types, generics and inheritance.</p>
<h2 id="heading-covariance">Covariance</h2>
<p>Covariance (co- = together) means the subtype relationship goes in the same direction i.e. transform (vary) together with the wrapped type. For example, if <code>Cat</code> is a subtype of <code>Animal</code>, then <code>set[Cat]</code> is a subtype of <code>set[Animal]</code>. The type parameter varies together and in the same direction as the inheritance relationship.</p>
<p>As we mentioned in the introduction, covariance is a type of variance that allows you to use a more specific type than originally specified. For example, if <code>Cat</code> is a subclass of <code>Animal</code>, you can assign an instance of <code>Iterable[Cat]</code> to a variable of type <code>Iterable[Animal]</code>, and if you have a method that takes an <code>Iterable[Animal]</code>, you can safely pass in an <code>Iterable[Cat]</code>.</p>
<p>The definition is that a generic type <code>GenericType[T]</code> is covariant in type parameter <code>T</code> if:</p>
<ul>
<li><code>Derived</code> is subtype of <code>Base</code>.</li>
<li><code>GenericType[Derived]</code> is a subtype of <code>GenericType[Base]</code></li>
</ul>
<p>Examples of covariant types in Python are <code>Iterable</code>, <code>set</code>, <code>tuple</code>, and return types of callables.</p>
<p>Before we start we need to import a few types that we will use in the examples.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> abstractmethod
<span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable, Iterable
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Generic, TypeVar
</code></pre>
<p>Let's define a few classes. We will use <code>Animal</code> as a base class, and define <code>Cat</code> and <code>Dog</code> as subclasses of <code>Animal</code>.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Meow"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Woof"</span>
</code></pre>
<p>Let's first see how this works with basic assignments.</p>
<pre><code class="lang-python">cats = [Cat(), Cat()]
xs: Iterable[Animal] = cats  <span class="hljs-comment"># Ok</span>
ys: list[Animal] = cats  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># Type parameter "_T@list" is invariant, but "Cat" is not the same as "Animal"</span>
</code></pre>
<p>So we see for the first assignment everything is ok, since the type of <code>xs</code> is <code>Iterable[Animal]</code>, which is indeed a subtype of <code>Iterable[Cat]</code>.</p>
<p>But for the second assignment, we get an error. This is because <code>list[Animal]</code> is invariant, and <code>list[Cat]</code> is not a subtype of <code>list[Animal]</code>.</p>
<p>The problem is that lists are mutable. Think for a minute what would happen if we appended a <code>Dog</code> to the list <code>ys</code>. This is fine for <code>ys</code> since the type is <code>list[Animal]</code>, but this means that <code>cats</code> would also be modified and would now contain a <code>Dog</code>, which is not allowed and should come as a surprise to any code still using <code>cats</code>.</p>
<pre><code class="lang-python">cats: list[Cat] = [Cat(), Cat()]
ys: list[Animal] = cats  <span class="hljs-comment"># type: ignore</span>
ys.append(Dog())

print([cat.say() <span class="hljs-keyword">for</span> cat <span class="hljs-keyword">in</span> cats])
<span class="hljs-comment"># Output: ['Meow', 'Meow', 'Woof']</span>
</code></pre>
<h3 id="heading-covariance-and-function-return-types">Covariance And Function Return Types</h3>
<p>Now let's see how this works with function return types for callables. We will define some "getter" functions that returns an instance of <code>Animal</code> or <code>Cat</code>, and also a "setter" function just to show that this does not work.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_animal</span>() -&gt; Animal:</span>
    <span class="hljs-keyword">return</span> Cat()  <span class="hljs-comment"># Cat, but returned as Animal</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_animals</span>() -&gt; Iterable[Animal]:</span>
    <span class="hljs-keyword">return</span> iter([Cat()])


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_cat</span>() -&gt; Cat:</span>
    <span class="hljs-keyword">return</span> Cat()


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_cat</span>(<span class="hljs-params">cat: Cat</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_cats</span>() -&gt; Iterable[Cat]:</span>
    <span class="hljs-keyword">return</span> iter([Cat()])


<span class="hljs-comment"># Cat -&gt; Animal</span>
var1: Animal = Cat()  <span class="hljs-comment"># Ok, polymorphism</span>
var2: Callable[[], Animal] = get_cat  <span class="hljs-comment"># Ok, covariance,</span>
var3: Callable[[], Iterable[Animal]] = get_cats  <span class="hljs-comment"># Ok, covariance</span>
var4: Callable[[Animal], <span class="hljs-literal">None</span>] = set_cat  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># Parameter 1: type "Animal" is incompatible with type "Cat"</span>
</code></pre>
<p>The first assignment of <code>var1</code> is just normal polymorphism. This is just to show the similarity between polymorphism and covariance. For the second and third assignments we see that covariance works for return types in callables since a function that returns a <code>Cat</code> is compatible with a function that returns an <code>Animal</code>.</p>
<p>For the last assignment, we get an error, since <code>set_cat</code> is a function that takes a <code>Cat</code>, and <code>set_cat</code> is not compatible with a function that takes an <code>Animal</code>. This is because callables are not covariant on parameter types.</p>
<p>In the next example, we will see how this works when assigning a general type e.g. <code>Animal</code> to a more specific type e.g <code>Cat</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Animal -&gt; Cat</span>
var5: Cat = Animal()  <span class="hljs-comment"># Error: "Animal" is incompatible with "Cat"</span>
var6: Callable[[], Cat] = get_animal  <span class="hljs-comment"># Error: ...</span>
var7: Callable[[], Iterable[Cat]] = get_animals  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># Type parameter "_T_co@Iterable" is covariant, but "Animal" is not a subtype of "Cat"</span>
</code></pre>
<p>For the first assignment of <code>var5</code>, we get an error, since <code>Animal</code> is not a subtype of <code>Cat</code>. This is because a function that returns an <code>Animal</code> is not compatible with a function that returns a <code>Cat</code>. This is because the function might return a Dog.</p>
<p>For the second assignment of <code>var6</code>, and third assignments of <code>var7</code>, we also get errors, since <code>Animal</code> is not a subtype of <code>Cat</code>, hence a <code>Dog</code> might be returned from <code>get_animal</code> or <code>get_animals</code> which is incompatible with <code>Cat</code>.</p>
<h3 id="heading-custom-covariant-generic-classes">Custom Covariant Generic Classes</h3>
<p>We can also define our own covariant generic classes. Let's see how this works. To define a covariant generic class, we need to use the <code>covariant</code> keyword argument for the <code>T_co</code> type variable.` This is by the way similar to how we declared type variables before Python 3.12.</p>
<p>We will make a <code>Rabbit</code> class that is a subclass of <code>Animal</code>, and a <code>Hat</code> class that is covariant in its type parameter. This means that a <code>Hat[Rabbit]</code> is a subtype of <code>Hat[Animal]</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738417766106/260c442e-424c-4d34-b2f5-03443ab96996.png" alt class="image--center mx-auto" /></p>
<p>Let's see how this looks in code.</p>
<pre><code class="lang-python">T_co = TypeVar(<span class="hljs-string">"T_co"</span>, covariant=<span class="hljs-literal">True</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rabbit</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Squeak"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hat</span>(<span class="hljs-params">Generic[T_co]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, value: T_co</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.value = value

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pull</span>(<span class="hljs-params">self</span>) -&gt; T_co:</span>
        <span class="hljs-keyword">return</span> self.value
</code></pre>
<p>One way to think about covariance is that covariant types are "out" types and can only be used as return types. If a class were to allow inserting and setting values of the generic type, it would violate the principle of covariance and could lead to type safety issues.</p>
<p>Let's see what happens if we try to add a method that takes the generic type as input for a class with the covariant type variable.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidHat</span>(<span class="hljs-params">Hat[T_co]</span>):</span>
    <span class="hljs-string">"""Just to show that in parameters are not allowed"""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span>(<span class="hljs-params">self, value: T_co</span>) -&gt; <span class="hljs-keyword">None</span>:</span>  <span class="hljs-comment"># Error: Covariant type variable cannot be used in parameter type</span>
        self.value = value
</code></pre>
<p>As we see in the example above, we get an error when we try to add a method that takes the generic type in the parameter. This is because covariant types can only be used as return types. If a class were to allow inserting and setting values of the generic type, it could lead to type safety issues.</p>
<p>But wait a minute. The constructor or the initializer <code>__init__</code> method is taking the generic type as an argument. Why isn't that a problem?  The reason why this is okay is that the object is being created at that point, and the type is being established. Once the object is created, its type is fixed and won't change.</p>
<p>But for abstract covariant classes or protocols that do not have constructors, we can only use the generic type as on out type, i.e. only in the return type of a method.</p>
<pre><code class="lang-python">hat_with_animal: Hat[Animal] = Hat[Rabbit](Rabbit())


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fetch_rabbit_hat</span>() -&gt; Hat[Rabbit]:</span>
    <span class="hljs-keyword">return</span> Hat(Rabbit())


fetch_animal_hat: Callable[[], Hat[Animal]] = fetch_rabbit_hat
</code></pre>
<p>In the example above we defined <code>Hat[Rabbit]</code> and assign it to a variable that has the type <code>Hat[Animal]</code>. This would have given an error if the generic type <code>T_co</code> was not covariant.</p>
<p>Note: we specify <code>Hat[Rabbit](Rabbit())</code> to avoid that the constructor uses polymorphism from <code>Rabbit</code> to <code>Animal</code> so we do create a <code>Hat[Rabbit]</code> and not a <code>Hat[Animal]</code>.</p>
<h3 id="heading-covariance-summarized">Covariance summarized</h3>
<p>Covariance is in may ways similar to polymorphism in the way we think about and use the type in our code. We use it for "out" types we have in our methods, i.e. methods that returns the generic type like <code>Iterable</code>, <code>Set</code>, <code>Tuple</code>, and also return types of <code>Callable</code>.</p>
<p>When we define a type to be covariant, we are able to assign a container i.e. generic class of e.g. <code>Rabbit</code>, to a variable that is annotated as a generic class of <code>Animal</code>. This would not have been possible if the type was not covariant.</p>
<h1 id="heading-contravariance">Contravariance</h1>
<p>Contravariance (contra- = against/opposite) means the subtype relationship goes in the opposite direction. If <code>Cat</code> is a subtype of <code>Animal</code>, then e.g <code>Observer[Animal]</code> is a subtype of <code>Observer[Cat]</code>. The type parameter varies in the opposite direction from the inheritance relationship of the wrapped type.</p>
<p>As mentioned introduction, contravariance is a type of variance that allows you to use a more general type in place of a more specific type. This  might sound counterintuitive at first. It usually goes against what you would expect, and it's safe to say that this is something most developers don't know about.</p>
<p>In Python, contravariance is typically experienced for function arguments in callables, or push-based containers such as observables (<a target="_blank" href="https://github.com/ReactiveX/RxPY">RxPY</a>). This means that it might help to think in terms of callbacks when you try to understand contravariance. This is because callbacks are usually functions that usually takes one or more arguments and returns nothing.</p>
<p>The definition is that a generic type <code>GenericType[T]</code> is contravariant in type parameter <code>T</code> if:</p>
<ul>
<li><code>Derived</code> is subtype of <code>Base</code>.</li>
<li><code>GenericType[Base]</code> is a subtype of <code>GenericType[Derived]</code>.</li>
</ul>
<p>Examples of contravariant types in Python are callables, and function arguments. The Observer class in RxPY and similar classes using the "consumer"" pattern e.g (<code>send</code>, <code>throw</code>, <code>close</code>) style of methods that take generic type <code>T</code> as an argument and return nothing.</p>
<p>First, we need to import a few types that we will use in the examples.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> abstractmethod
<span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable, Iterable
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Generic, TypeVar
</code></pre>
<p>Let's define a few classes, the same as we used with covariance so we can compare the two. We will use <code>Animal</code> as a base class, and define <code>Cat</code> and <code>Dog</code> as subclasses of Animal.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Meow"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Woof"</span>
</code></pre>
<h2 id="heading-example-function-arguments">Example: Function Arguments</h2>
<p>Let's see how this works with function arguments for callables. Callables are generic types that are contravariant in their argument types, e.g. you can assign an instance of <code>Callable[[Animal], None]</code> to a variable of type <code>Callable[[Cat], None]</code></p>
<p>We can define a few setter functions that takes an argument of type <code>Animal</code> or <code>Cat</code> and returns nothing. These are the opposites of the <code>getter</code> functions we defined for covariance.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_animal</span>(<span class="hljs-params">animal: Animal</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_animals</span>(<span class="hljs-params">animals: Iterable[Animal]</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_cat</span>(<span class="hljs-params">cat: Cat</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_cats</span>(<span class="hljs-params">cats: Iterable[Cat]</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">pass</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_animal</span>() -&gt; Animal:</span>
    <span class="hljs-keyword">return</span> Cat()  <span class="hljs-comment"># Cat, but returned as Animal</span>


<span class="hljs-comment"># Cat -&gt; Animal</span>
var1: Animal = Cat()  <span class="hljs-comment"># Ok, polymorphism</span>
<span class="hljs-comment"># This works because a function that takes a Cat is compatible with a function that</span>
<span class="hljs-comment"># takes an Animal.</span>
var2: Callable[[Cat], <span class="hljs-literal">None</span>] = set_animal  <span class="hljs-comment"># Ok, since Callable is contravariant for arguments</span>
var3: Callable[[Iterable[Cat]], <span class="hljs-literal">None</span>] = set_animals  <span class="hljs-comment"># Ok, contravariance</span>
var4: Callable[[], Cat] = get_animal  <span class="hljs-comment"># Error: "Animal" is incompatible with "Cat"</span>
</code></pre>
<p>We start in a similar way as we did with covariance. We see that for the first assignment, everything is ok, since the type of <code>var1</code> is <code>Animal</code>, which is a base class of <code>Cat</code>. This is normal polymorphism.</p>
<p>For the second and third assignments, we start to see how contravariance works for function arguments. This works because a function that takes an <code>Animal</code> can be assigned to a variable that is annotated as a callable that takes a <code>Cat</code>. We can always call a callback that takes an <code>Animal</code> with a <code>Cat</code>.</p>
<p>For the last assignment, we get an error, since <code>get_animal</code> is a function that returns an <code>Animal</code>, and <code>get_animal</code> is not compatible with a function that returns a <code>Cat</code>. This is because callables are not contravariant on return types.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Animal -&gt; Cat</span>
var5: Cat = Animal()  <span class="hljs-comment"># Error: "Animal" is incompatible with "Cat"</span>
<span class="hljs-comment"># We get an error here because a function that takes an Animal is not compatible with</span>
<span class="hljs-comment"># a function that takes a Cat. This is because the function might take a Dog.</span>
var6: Callable[[Animal], <span class="hljs-literal">None</span>] = set_cat  <span class="hljs-comment"># Error: ...</span>
var7: Callable[[Iterable[Animal]], <span class="hljs-literal">None</span>] = set_cats  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># (*) Type parameter "_T_co@Iterable" is covariant, but "Animal" is not a subtype of "Cat"</span>
</code></pre>
<p>For the first assignment, we get an error, since <code>Animal</code> is not a subtype of <code>Cat</code>. For the second and third assignments, we get an error because <code>Animal</code> is not a subtype of <code>Cat</code>. If you think about the callable as a callback, then it's easier to see that you cannot give an <code>Animal</code> e.g. a <code>Dog</code> to a function that takes a <code>Cat</code>.</p>
<h1 id="heading-custom-contravariant-generic-classes">Custom Contravariant Generic Classes</h1>
<p>We can also define our own contravariant generic classes, similar to how we made covariant classes. To define a contravariant generic class, we need to use the <code>contravariant</code> keyword argument for the <code>T_contra</code> type variable.` This is by the way similar to how we declared type variables before Python 3.12.</p>
<p>We will make a <code>Rabbit</code> class that is a subclass of <code>Animal</code>, and a <code>Hat</code> class that is contravariant in its type parameter. This means that a <code>Hat[Animal]</code> is a subtype of <code>Hat[Rabbit]</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738417782922/4bac24f9-c168-47fb-9ba0-5fe6a81c781c.png" alt class="image--center mx-auto" /></p>
<p>Let's see how this looks in code.</p>
<pre><code class="lang-python">T_contra = TypeVar(<span class="hljs-string">"T_contra"</span>, contravariant=<span class="hljs-literal">True</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rabbit</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Squeak"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hat</span>(<span class="hljs-params">Generic[T_contra]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, value: T_contra</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.value = value

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span>(<span class="hljs-params">self, value: T_contra</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.value = value


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Callable_</span>(<span class="hljs-params">Generic[T_contra]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span>(<span class="hljs-params">self, value: T_contra</span>) -&gt; <span class="hljs-keyword">None</span>:</span> ...
</code></pre>
<p>Let's see what happens if we try to add a method that returns the generic type for a class with a contravariant type variable.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidHat</span>(<span class="hljs-params">Hat[T_contra]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, value: T_contra</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.value = value

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span>(<span class="hljs-params">self</span>) -&gt; T_contra:</span>  <span class="hljs-comment"># Error: Contravariant type variable cannot be used as a return type</span>
        <span class="hljs-keyword">return</span> self.value
</code></pre>
<p>As we see in the example above, we get an error when we try to add a method that returns the generic type. This is because contravariant types can only be used as function argument types. If a class were to allow returning values of the generic type, it could lead to type safety issues.</p>
<p>One way to think about contravariance is that contravariant types are "in" types and can only be used as function argument types. If a class were to allow returning values of the generic type, it would violate the principle of contravariance and could lead to type safety issues.</p>
<p>We see that we get the opposite of what we saw with covariance. The <code>get_value</code> method now has an error, since we cannot return a value of type <code>T_contra</code>. But the <code>set_value</code> method works, since we can set the value to a value of type <code>T_contra</code>.</p>
<pre><code class="lang-python">animal: Animal = Rabbit()
hat_with_rabbit: Hat[Rabbit] = Hat[Animal](animal)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fetch_animal_hat</span>() -&gt; Hat[Animal]:</span>
    <span class="hljs-keyword">return</span> Hat(Rabbit())


fetch_rabbit_hat: Callable[[], Hat[Rabbit]] = fetch_animal_hat
</code></pre>
<p>In the example above we defined <code>Hat[Animal]</code> and assign it to a variable that has the type <code>Hat[Rabbit]</code>. This would have given an error if the generic type <code>T_contra</code> was not contravariant.</p>
<h1 id="heading-summary">Summary</h1>
<p>Contravariance is the opposite of covariance, and this makes it quite a bit harder to understand since <code>Hat[Rabbit]</code> is not a subtype of <code>Hat[Animal]</code> perhaps as you might expect. It is actually the other way around. With contravariance <code>Hat[Animal]</code> becomes a subtype of <code>Hat[Rabbit]</code>.</p>
<p>When we define a type to be contravariant, we are able to assign a container i.e. generic class of e.g. <code>Animal</code>, to a variable that is annotated as a generic class of <code>Rabbit</code>. This would not have been possible if the type was not contravariant.</p>
<h2 id="heading-invariance-in-generics">Invariance in Generics</h2>
<p>Invariance (in- = un/not) means that the type is not variant, and will not transform (vary) together with the wrapped type. This means that you can use only the type originally specified, and neither a more specific nor a more general type. This is the default behavior for generic types in Python.</p>
<p>An invariant generic type parameter is neither covariant nor contravariant. You cannot assign an instance of <code>Hat[Animal]</code> to a variable of type <code>Hat[Rabbit]</code> or vice versa.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> abstractmethod


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">render</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rabbit</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">render</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Rabbit!"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hat</span>[<span class="hljs-title">T</span>]:</span>
<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span>(<span class="hljs-params">self, value: T</span>) -&gt; <span class="hljs-keyword">None</span>:</span> ...

<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pull</span>(<span class="hljs-params">self</span>) -&gt; T:</span> ...


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RabbitHat</span>(<span class="hljs-params">Hat[Rabbit]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span>(<span class="hljs-params">self, value: Rabbit</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        print(<span class="hljs-string">f"Putting <span class="hljs-subst">{value.render()}</span> in the hat"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pull</span>(<span class="hljs-params">self</span>) -&gt; Rabbit:</span>
        <span class="hljs-string">"""Pull a Rabbit out of the hat"""</span>
        <span class="hljs-keyword">return</span> Rabbit()


<span class="hljs-comment"># This will not work due to invariance</span>
animal_hat: Hat[Animal] = RabbitHat()  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># Type parameter "T@Hat" is invariant, but "Rabbit" is not the same as "Animal"</span>

<span class="hljs-comment"># This also will not work due to invariance</span>
rabbit_hat: Hat[Rabbit] = Hat[Animal]()  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># Type parameter "T@Hat" is invariant, but "Animal" is not the same as "Rabbit"</span>

<span class="hljs-comment"># This is the only valid assignment</span>
rabbit_hat: Hat[Rabbit] = RabbitHat()

<span class="hljs-comment"># We can only put Rabbits in a RabbitHat</span>
rabbit_hat.put(Rabbit())

<span class="hljs-comment"># This will not work, even though Rabbit is a subclass of Animal</span>
rabbit_hat.put(Animal())  <span class="hljs-comment"># Error: ...</span>
<span class="hljs-comment"># "Animal" is incompatible with "Rabbit"</span>

<span class="hljs-comment"># We can only take Rabbits from a RabbitHat</span>
rabbit: Rabbit = rabbit_hat.pull()
</code></pre>
<p>Invariance restricts us to use exactly the type specified. This happens when we use the generic type as both "in" and "out" types, meaning that methods of the type use the generic type both in the parameters, and return types.</p>
<p>This hints that the generic type may be some kind of mutable container and we cannot allow assigning a <code>Hat[Animal]</code> to a <code>Hat[Rabbit]</code> or vice versa since that could easily lead to code adding a <code>Cat</code> into a <code>Hat[Rabbit]</code>.</p>
<h3 id="heading-references">References</h3>
<ul>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">Covariance and contravariance (computer science)</a></li>
<li><a target="_blank" href="https://mypy.readthedocs.io/en/stable/generics.html#variance-of-generic-types">Variance in Python type checking (mypy docs)</a></li>
<li><a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance">Covariance and contravariance in generics</a></li>
<li><a target="_blank" href="https://rednafi.com/python/variance_of_generic_types/">Variance of generic types in Python</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Python Type Annotations (part 2)]]></title><description><![CDATA[Python Type Annotations is a tutorial in 3 parts: Part 1 | Part 2 (this post) | Part 3

Table of contents

Generics
Variadic Generics
Parameter Specification
Overloads
Final notes
References


Generics
Generics provide a powerful way to create flexib...]]></description><link>https://cardamomcode.dev/python-type-annotations-part-2</link><guid isPermaLink="true">https://cardamomcode.dev/python-type-annotations-part-2</guid><category><![CDATA[type annotations]]></category><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Python 3]]></category><dc:creator><![CDATA[Cristina Ferrer]]></dc:creator><pubDate>Tue, 04 Feb 2025 23:55:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738180721614/1fa0d987-ff73-4d84-a574-e526d27c8e34.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p><strong>Python Type Annotations</strong> is a tutorial in 3 parts: <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-1">Part 1</a> | Part 2 (this post) | <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-3">Part 3</a></p>
</blockquote>
<h2 id="heading-table-of-contents">Table of contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-generics">Generics</a></li>
<li><a class="post-section-overview" href="#heading-variadic-generics">Variadic Generics</a></li>
<li><a class="post-section-overview" href="#heading-parameter-specification">Parameter Specification</a></li>
<li><a class="post-section-overview" href="#heading-overloads">Overloads</a></li>
<li><a class="post-section-overview" href="#heading-final-notes">Final notes</a></li>
<li><a class="post-section-overview" href="#heading-references">References</a></li>
</ul>
<hr />
<h2 id="heading-generics">Generics</h2>
<p>Generics provide a powerful way to create flexible and reusable code components that can work with multiple data types while maintaining type safety.</p>
<p>They are a way to defer the specification of types until you actually need to use them in your code. In other words, generics allow you to write a function or a class that can work with any data type.</p>
<p><strong>If Python is a dynamically typed language, and already works with any type,</strong> <strong>why do we need generics?</strong></p>
<p>Sometimes you want functions to handle different types in a consistent way. Consistent here means that we are locking the types of two or more type parameters. If we set the type of one parameter to be <code>int</code>, all other uses of that type parameter must also be <code>int</code>. It's like telling Python, <em>"Hey, whatever type you choose, stick with it throughout this function!"</em></p>
<p>For Python 3.12 and later versions, generic type parameters can be defined by using square brackets after the name of the function, for example <code>[T]</code>. This type parameter can then be used both in the function signature, and in the body of the function.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">identity</span>[<span class="hljs-title">T</span>](<span class="hljs-params">value: T</span>) -&gt; T:</span>
    <span class="hljs-keyword">return</span> value
</code></pre>
<p>This generic identity function locks the return type <code>T</code> to be the same as the input value type <code>T</code>. If we pass an <code>int</code>, the return type will then also have to be an <code>int</code>. Similarly, if we pass a <code>str</code>, the return type will be a <code>str</code>.</p>
<p>We can now use the function with any type such as <code>int</code>, <code>str</code>, etc:</p>
<pre><code class="lang-python">a: int = identity(<span class="hljs-number">10</span>)
b: str = identity(<span class="hljs-string">"test"</span>)
</code></pre>
<hr />
<p><strong>Note</strong>: Before Python 3.12, generic type variables were defined using <code>TypeVar</code> from the <code>typing</code> module.</p>
<p>Similar to private variables in a Python module, we usually prepend a <code>_</code> to the name of the type variable to indicate that it is private and not to be used outside the module.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> TypeVar  <span class="hljs-comment"># noqa: E402</span>

_T = TypeVar(<span class="hljs-string">"_T"</span>)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">identity_</span>(<span class="hljs-params">value: _T</span>) -&gt; _T:</span>
    <span class="hljs-keyword">return</span> value
</code></pre>
<hr />
<p>In the next example, we create a <code>filter</code> function that filters a list of a given data type <code>[T]</code>. The function takes two arguments: a <code>predicate</code> function that filters the list, and the <code>source</code> list to be filtered:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable  <span class="hljs-comment"># noqa: E402</span>
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">filter</span>[<span class="hljs-title">T</span>](<span class="hljs-params">predicate: Callable[[T], bool], source: list[T]</span>) -&gt; list[T]:</span>
    <span class="hljs-keyword">return</span> [x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> source <span class="hljs-keyword">if</span> predicate(x)]
</code></pre>
<p>If we pass in a <code>list[int]</code>, it will expect a <code>predicate</code> function that takes an <code>int</code> and returns a <code>bool</code>. The result of the the function will also be a <code>list[int]</code>:</p>
<pre><code class="lang-python">xs: list[int] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
ys = filter(<span class="hljs-keyword">lambda</span> x: x &gt; <span class="hljs-number">2</span>, xs)
reveal_type(ys)  <span class="hljs-comment"># Type of "ys" is "list[int]"</span>
</code></pre>
<p>Similarly, if we pass in a <code>list[str]</code>, the <code>predicate</code> function will take a <code>str</code> and return a <code>bool</code>. The result of the function will also be a <code>list[str]</code>:</p>
<pre><code class="lang-python">xs_: list[str] = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>]
ys_ = filter(<span class="hljs-keyword">lambda</span> x: len(x) &gt; <span class="hljs-number">1</span>, xs_)
reveal_type(ys_)  <span class="hljs-comment"># Type of "ys_" is "list[str]"</span>
</code></pre>
<p>Note that generic types are only really useful when the declared type parameters are used more than once. In the function below, the type parameter <code>T</code> is only used once, so the type is not locked to any other argument, or to the return type in the function.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">length</span>[<span class="hljs-title">T</span>](<span class="hljs-params">xs: list[T]</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> sum(<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> xs)
</code></pre>
<p>When a type variable is used only once, it is equivalent to using <code>Any</code>, as the function will accept any type. In such cases, the type checker cannot provide any meaningful validation.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">length_</span>(<span class="hljs-params">xs: list[Any]</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> sum(<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> xs)
</code></pre>
<h3 id="heading-generic-classes">Generic classes</h3>
<p>We can also create generic classes by using class type parameters. This will make the class reusable with different types. In the following example, we create a <code>Stack</code> class that can be used with different types of data.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Stack</span>[<span class="hljs-title">T</span>]:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, initial_values: list[T] | None = None</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.items: list[T] = initial_values <span class="hljs-keyword">or</span> []

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">push</span>(<span class="hljs-params">self, item: T</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.items.append(item)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pop</span>(<span class="hljs-params">self</span>) -&gt; T:</span>
        <span class="hljs-keyword">return</span> self.items.pop()


int_stack = Stack([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
int_stack.push(<span class="hljs-number">1</span>)
int_stack.push(<span class="hljs-number">2</span>)

str_stack = Stack[str]()
str_stack.push(<span class="hljs-string">"hello"</span>)
str_stack.push(<span class="hljs-string">"world"</span>)
</code></pre>
<hr />
<p><strong>Note</strong>: Before Python 3.12, generic types required to define a <code>TypeVar</code> and use <code>Generic[_T]</code> to declare a generic type of <code>_T</code>. This approach is still valid but for most cases not needed anymore. The exception is if we want to specify variance, which is not possible with the new syntax. We will explore variance in Part 3.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Generic  <span class="hljs-comment"># noqa: E402</span>

_D = TypeVar(<span class="hljs-string">"_D"</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Stack_</span>(<span class="hljs-params">Generic[_D]</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, initial_values: list[_D] | None = None</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.items: list[_D] = initial_values <span class="hljs-keyword">or</span> []

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">push</span>(<span class="hljs-params">self, item: _D</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.items.append(item)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pop</span>(<span class="hljs-params">self</span>) -&gt; _D:</span>
        <span class="hljs-keyword">return</span> self.items.pop()
</code></pre>
<hr />

<h3 id="heading-generic-type-aliases">Generic Type Aliases</h3>
<p>Type aliases in Python provide a way to create alternate names for complex types, making your code more readable and maintainable. It reduces redundancy and also adds semantic meaning to your type annotations.</p>
<p>In Python 3.12, you can use the new <code>type</code> keyword to define aliases:
</p>
<pre><code class="lang-python">type UserScores = dict[str, list[int]]
</code></pre>
<p>We can make type aliases generic by using square brackets after the name of the type alias similar to how we make generic functions and classes.</p>
<pre><code class="lang-python">type PropertyBag[T] = dict[str, T]

<span class="hljs-comment"># We can now use the generic type alias to create a more specific non-generic type</span>
type StringBag = PropertyBag[str]
</code></pre>
<hr />
<p><strong>Note</strong>: Before python 3.12, type aliases were defined using the <code>TypeAlias</code> feature from the <code>typing</code> module.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> TypeAlias, TypeVar, override  <span class="hljs-comment"># noqa: E402</span>

_B = TypeVar(<span class="hljs-string">"_B"</span>)

PropertyBag_: TypeAlias = dict[str, _B]  <span class="hljs-comment"># noqa: UP040</span>

StringBag_: TypeAlias = PropertyBag_[str]  <span class="hljs-comment"># noqa: UP040</span>
</code></pre>
<hr />
<h3 id="heading-how-to-restrict-generic-type-parameters">How to restrict Generic Type Parameters</h3>
<p>Generic types can be restricted to work with specific types or their subclasses:</p>
<ul>
<li><p><strong>Using a bound type parameter</strong>: It sets an upper bound for the type. This means the type can only be the specified type or a subclass of the type. For example, to create a generic type that accepts <code>float</code> or any subclass of <code>float</code> (like <code>int</code>), you can use a bounded type parameter <code>[T: float]</code>.</p>
</li>
<li><p><strong>Using a constrained type parameter</strong>: It limits the type to a specific set of two or more types, without including their subclasses. For example, to create a generic type that accepts only <code>int</code> or <code>str</code>, you can use a constrained type parameter: <code>[T: (int, str)]</code>.</p>
</li>
</ul>
<hr />
<p><strong>Note</strong>: Before Python 3.12, bound and constrained generic types were defined in the <code>TypeVar</code> definition.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> TypeVar  <span class="hljs-comment"># noqa: E402</span>

<span class="hljs-comment"># Bound type parameter</span>
T = TypeVar(<span class="hljs-string">"T"</span>, bound=float)

<span class="hljs-comment"># Constrained type parameter</span>
S = TypeVar(<span class="hljs-string">"S"</span>, int, str)
</code></pre>
<hr />
<h3 id="heading-why-to-restrict-generic-type-parameters">Why to restrict Generic Type Parameters</h3>
<p>At first, we might think that we could use unions, overloads, or base classes to achieve the same functionality as restricted generic type parameters. However, they will not work the same as restricted generic type parameters.</p>
<p>To explore this, let's define a data model for some animals and their subclasses:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-string">"""A base class for animals"""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name: str</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.name = name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Animal is eating"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rabbit</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-string">"""A subclass of Animal"""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Rabbit is running"</span>

<span class="hljs-meta">    @override</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Rabbit is eating"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Fox</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-string">"""A subclass of Animal"""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Ring-ding-ding-ding!"</span>

<span class="hljs-meta">    @override</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Fox is eating"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedFox</span>(<span class="hljs-params">Fox</span>):</span>
    <span class="hljs-string">"""A subclass of Fox"""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">go_nuts</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Going nuts"</span>


fox = Fox(<span class="hljs-string">"Tod"</span>)
rabbit = Rabbit(<span class="hljs-string">"Harvey"</span>)
red_fox = RedFox(<span class="hljs-string">"Foxy"</span>)
</code></pre>
<p>In the following examples, we'll use these classes to illustrate the differences between using unions, overloads, base classes, constrained generic types and bound generic types. The <code>RedFox</code> subclass will highlight potential problems when working with subclasses and generics.</p>
<hr />
<p><strong>Note</strong>: The <code>@override</code> decorator is used to indicate that a method overrides a base class method. This is useful for static type checkers to verify the method actually overrides a parent method.</p>
<hr />
<h3 id="heading-using-unions">Using unions</h3>
<p>For the first example, let's create a function that takes either a <code>Fox</code> or a <code>Rabbit</code> as an argument, and returns <code>Fox</code> or <code>Rabbit</code> as the output type. We will then try to call some of the methods on the returned object to see what happens.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal</span>(<span class="hljs-params">animal: Fox | Rabbit</span>) -&gt; Fox | Rabbit:</span>
    animal.feed()
    ...
    <span class="hljs-keyword">return</span> animal


care_for_animal(rabbit).run()  <span class="hljs-comment"># Error: Cannot access attribute "run" for class "Fox"</span>
care_for_animal(fox).say()  <span class="hljs-comment"># Error: Cannot access attribute "say" for class "Rabbit"</span>
care_for_animal(red_fox).go_nuts()  <span class="hljs-comment"># Error: Cannot access attribute "go_nuts" for class "Fox"</span>
</code></pre>
<p>In the example above, we can never be sure if the output of <code>care_for_animal</code> will be a <code>Fox</code> or a <code>Rabbit</code>. Therefore, we need to use type narrowing, such as a <code>match</code> statement, to check the type of the result. This creates a lot of noise to our code by requiring type checks before using the result.</p>
<pre><code class="lang-python">result = care_for_animal(rabbit)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "Rabbit | Fox"</span>

match result:
    case Rabbit():
        result.run()
    case Fox():
        result.say()
</code></pre>
<p>As an alternative, we can try using overloads or a constrained generic type parameter.</p>
<h3 id="heading-using-overloads">Using overloads</h3>
<p>Function overloads allow you to specify multiple type signatures for a single function. They're especially useful when a function can accept different parameter types or combinations of parameters. Overloads are explained in more detail in the <a class="post-section-overview" href="#heading-overloads">next</a> section.</p>
<p>Let's define different signatures for the <code>care_for_animal</code> function:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> overload  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_overloads</span>(<span class="hljs-params">animal: Rabbit</span>) -&gt; Rabbit:</span> ...


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_overloads</span>(<span class="hljs-params">animal: Fox</span>) -&gt; Fox:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_overloads</span>(<span class="hljs-params">animal: Fox | Rabbit</span>) -&gt; Fox | Rabbit:</span>
    animal.feed()
    ...
    <span class="hljs-keyword">return</span> animal


care_for_animal_overloads(rabbit).run()
care_for_animal_overloads(fox).say()
care_for_animal_overloads(red_fox).go_nuts()  <span class="hljs-comment"># Error: Cannot access attribute "go_nuts" for class "Fox"</span>
</code></pre>
<p>Using overloads is more verbose, but when we call the function with a <code>Rabbit</code> or a <code>Fox</code>, it returns a <code>Rabbit</code> or <code>Fox</code> respectively, allowing us to call their specific methods. However, subclasses like <code>RedFox</code> are treated as their parent class <code>Fox</code>, so we cannot call the <code>go_nuts</code> method as it's only available on the <code>RedFox</code> class.</p>
<p>To address these limitations, we can explore using a constrained generic type.</p>
<h3 id="heading-using-a-constrained-generic-type">Using a constrained generic type</h3>
<p>Let's make <code>T</code> a constrained type parameter that can only be <code>Rabbit</code> or <code>Fox</code>.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_constrained</span>[<span class="hljs-title">T</span>:</span> (Rabbit, Fox)](animal: T) -&gt; T:
    animal.feed()
    ...
    <span class="hljs-keyword">return</span> animal


care_for_animal_constrained(rabbit).run()
care_for_animal_constrained(fox).say()
care_for_animal_constrained(red_fox).go_nuts()  <span class="hljs-comment"># Error: Cannot access attribute "go_nuts" for class "Fox"</span>
</code></pre>
<p>This approach is less verbose, but it has the same limitation as using overloads. It requires knowing all restricted types upfront and won't work with other animals or even subclasses of the specified animal types. We still cannot call the <code>go_nuts</code> method on the <code>red_fox</code> object because the returned type is <code>Fox</code> and not <code>RedFox</code>.</p>
<p>To address this problem we need to use subclassing of some sort.</p>
<h3 id="heading-using-a-base-class">Using a base class</h3>
<p>Let's try to use the <code>Animal</code> base class as the input type parameter instead of <code>Rabbit</code> and <code>Fox</code>.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_subclassing</span>(<span class="hljs-params">animal: Animal</span>) -&gt; Animal:</span>
    animal.feed()
    ...
    <span class="hljs-keyword">return</span> animal


care_for_animal_subclassing(rabbit).feed()

care_for_animal_subclassing(rabbit).run()  <span class="hljs-comment"># Error: Cannot access attribute "run" for class "Animal"</span>
care_for_animal_subclassing(fox).say()  <span class="hljs-comment"># Error: Cannot access attribute "say" for class "Animal"</span>
care_for_animal_subclassing(red_fox).go_nuts()  <span class="hljs-comment"># Error: Cannot access attribute "go_nuts" for class "Animal"</span>
</code></pre>
<p>With subclassing in itself we will only be able to feed the animals, calling methods that are only available on the subclasses themselves will not work.</p>
<p>Let's try to use a bound generic type parameter instead.</p>
<h3 id="heading-using-a-bound-generic-type-parameter">Using a bound generic type parameter</h3>
<p>Bound generic type parameters work with subclasses, so let's try to use a bound generic type parameter with the base class <code>Animal</code>.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_bound</span>[<span class="hljs-title">T</span>:</span> Animal](animal: T) -&gt; T:
    animal.feed()
    ...
    <span class="hljs-keyword">return</span> animal


care_for_animal_bound(rabbit).run()
care_for_animal_bound(fox).say()
care_for_animal_bound(red_fox).go_nuts()
</code></pre>

<p>Now the output type matches the input type!</p>
<p>Subclassing with a bound generic parameter solves most of the problems we had earlier. However, it still limits us to using the function only with subclasses of <code>Animal</code>, making it less flexible for other types of animals.</p>
<p>Let's try using a protocol as the bound generic type.</p>
<h3 id="heading-using-a-bound-generic-protocol">Using a bound generic protocol</h3>
<p>In <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-1#heading-static-duck-typing-and-protocols">Part 1</a>, we explored protocols, which allow us to define a set of methods that a class must implement without requiring inheritance from a base class.</p>
<p>We can create a protocol that requires the <code>feed</code> method to be implemented, and use this protocol as a bound generic type parameter.
</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Protocol  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CareFor</span>(<span class="hljs-params">Protocol</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_protocol</span>[<span class="hljs-title">T</span>:</span> CareFor](animal: T) -&gt; T:
    animal.feed()
    <span class="hljs-keyword">return</span> animal


care_for_animal_protocol(rabbit).run()
care_for_animal_protocol(fox)
</code></pre>
<p>The <code>care_for_animal_protocol</code> function supports now static duck typing. It will work with any type that implements the <code>feed</code> method, allowing us to use it with any animal, not just subclasses of <code>Animal</code>:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Dog is eating"</span>


care_for_animal_protocol(Dog()).feed()

care_for_animal_protocol(rabbit).run()
care_for_animal_protocol(fox).say()
care_for_animal_protocol(red_fox).go_nuts()
</code></pre>
<p>Using a class that does not implement the <code>feed</code> method will result in a type error:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Robot</span>:</span> ...


care_for_animal_protocol(Robot())  <span class="hljs-comment"># Error: "Robot" is incompatible with protocol "CareFor"</span>
</code></pre>
<h3 id="heading-more-about-bound-generic-protocols">More about bound generic protocols</h3>
<p>Bound generic parameters combined with protocols are particularly useful when we need to ensure that a generic function or method works with a range of compatible types that share certain characteristics or behaviors, without requiring those types to inherit from a common base class.</p>
<p>Here is another example. We can define a generic function that takes a list of values and returns the largest value. This function can work with any type that implements the <code>__gt__</code> method, such as <code>int</code>, <code>float</code>, or custom classes that define the <code>__gt__</code> method.</p>
<p>To achieve this, we will need to create a protocol that requires a type to be comparable using the greater-than operator <code>&gt;</code>. This protocol will then be used to bound the generic type <code>T</code> used in the function:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Iterable  <span class="hljs-comment"># noqa: E402</span>
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any, reveal_type  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IsGreaterThan</span>(<span class="hljs-params">Protocol</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__gt__</span>(<span class="hljs-params">self, value: Any, /</span>) -&gt; bool:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">largest</span>[<span class="hljs-title">T</span>:</span> IsGreaterThan](xs: Iterable[T]) -&gt; T:
    <span class="hljs-keyword">return</span> max(xs)


xs: list[int] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
result = largest(xs)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "int"</span>

ss: list[str] = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>]
result = largest(ss)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "str"</span>
</code></pre>
<p>We can also use the <code>IsGreaterThan</code> Protocol for a function that takes a list of comparable values. We can pass in either a list of <code>int</code> or a list of <code>str</code>. The return type will correspond to the input type, returning a list of <code>int</code> or <code>str</code>, respectively.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sort_list</span>[<span class="hljs-title">T</span>:</span> IsGreaterThan](items: list[T]) -&gt; list[T]:
    <span class="hljs-keyword">return</span> sorted(items)


numbers = [<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>]
sorted_numbers = sort_list(numbers)
reveal_type(sorted_numbers)  <span class="hljs-comment"># Type of "sorted_numbers" is "list[int]"</span>

names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>]
sorted_names = sort_list(names)
reveal_type(sorted_names)  <span class="hljs-comment"># Type of "sorted_names" is "list[str]"</span>
</code></pre>
<h2 id="heading-variadic-generics">Variadic Generics</h2>
<p>In the previous section about <a class="post-section-overview" href="#heading-generics">Generics</a>, we looked at how we can define generic classes using one or more type variables, such as <code>T</code> and <code>U</code>. However, in some cases, we might not know the number of type variables before the class instance or function is created.</p>
<p>Variadic generics allow us to define classes and functions that can take any number of type arguments, similar to tuple arguments such as <code>*args</code>, where both the number of elements and their types depend on the function caller.</p>
<p>Let's explore how we can use variadic generics in classes, functions and callables.</p>
<h3 id="heading-classes-with-variadic-generics">Classes with Variadic Generics</h3>
<p>Variadic generics uses the star <code>*</code> notation for type variables, such as <code>*Ts</code>, to indicate that the type variable can represent any number of type arguments. The lowercase <code>s</code> is a convention used to indicate that the type variable is plural, showing that it represents multiple type arguments, compared to the commonly used <code>T</code> that represents a single type argument.</p>
<p>In the following example, we create two instances of <code>DataPoint</code>, each with a different number of arguments and types.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DataPoint</span>[*<span class="hljs-title">Ts</span>]:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, *values: *Ts</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
        self.values = values


first = DataPoint(<span class="hljs-literal">True</span>)
reveal_type(first)  <span class="hljs-comment"># Type of "first" is "DataPoint[bool]"</span>

second = DataPoint(<span class="hljs-number">1</span>, <span class="hljs-string">"test"</span>, <span class="hljs-number">2.3</span>)
reveal_type(second)  <span class="hljs-comment"># Type of "second" is "DataPoint[int, str, float]"</span>
</code></pre>
<p>--- <strong>Note:</strong> Before Python 3.12, variadic generics were defined using <code>TypeVarTuple</code> instead of using the <code>*Ts</code> syntax. It needs to be imported from the <code>typing</code> module for Python 3.11, and from <code>typing_extensions</code> for versions earlier than 3.11:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> TypeVarTuple  <span class="hljs-comment"># noqa: E402</span>

_Ts = TypeVarTuple(<span class="hljs-string">"_Ts"</span>)  <span class="hljs-comment"># Only needed for Python 3.11 and earlier</span>
</code></pre>
<hr />
<h3 id="heading-functions-with-variadic-generics">Functions with Variadic Generics</h3>
<p>Variadic generics can also be used to define functions that can take any number of arguments and types. This enables us to statically type the so-called <code>*args</code> argument.</p>
<p>Similarly to the previous example, here we define the generic <code>make_datapoint</code> function, which can take any number of arguments and types. The types are inferred from the provided arguments:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_datapoint</span>[*<span class="hljs-title">Ts</span>](<span class="hljs-params">*values: *Ts</span>) -&gt; DataPoint[*Ts]:</span>
    <span class="hljs-keyword">return</span> DataPoint(*values)


point1 = make_datapoint(<span class="hljs-string">"temperature"</span>, <span class="hljs-number">25.5</span>)
point2 = make_datapoint(<span class="hljs-number">1</span>, <span class="hljs-string">"test"</span>, <span class="hljs-number">2.3</span>)

reveal_type(point1)  <span class="hljs-comment"># Type of "point1" is "DataPoint[int, str, float]"</span>
reveal_type(point2)  <span class="hljs-comment"># Type of "point2" is "DataPoint[str, float]"</span>
</code></pre>
<p>Let's take a look at another example. We can define a <code>head</code> function that can take any number of arguments and types and returns the first element of the tuple. This is achieved by defining the type of the tuple argument as <code>tuple[T, *Ts]</code> where <code>T</code> is the first element and <code>Ts</code> the rest of the elements.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">head</span>[<span class="hljs-title">T</span>, *<span class="hljs-title">Ts</span>](<span class="hljs-params">tup: tuple[T, *Ts]</span>) -&gt; T:</span>
    <span class="hljs-keyword">return</span> tup[<span class="hljs-number">0</span>]


x = head((<span class="hljs-number">1</span>, <span class="hljs-string">"test"</span>, <span class="hljs-number">2.3</span>))
reveal_type(x)  <span class="hljs-comment"># Type of "x" is "int"</span>
</code></pre>
<p>Similarly, we can define a <code>tail</code> function that can take any number of arguments and types and return all of them except for the first one.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tail</span>[<span class="hljs-title">T</span>, *<span class="hljs-title">Ts</span>](<span class="hljs-params">tup: tuple[T, *Ts]</span>) -&gt; tuple[*Ts]:</span>
    <span class="hljs-keyword">return</span> tup[<span class="hljs-number">1</span>:]


y = tail((<span class="hljs-number">1</span>, <span class="hljs-string">"test"</span>, <span class="hljs-number">2.3</span>))
reveal_type(y)  <span class="hljs-comment"># Type of "y" is "tuple[str, float]"</span>
</code></pre>
<h3 id="heading-callables-with-variadic-generics">Callables with Variadic Generics</h3>
<p>Variadic generics can also be used to annotate callables.</p>
<p>In this example, we define a function <code>apply</code> that uses variadic generics to accept a callable <code>fn</code> and a variable number of arguments of type <code>Ts</code>. The <code>apply</code> function then calls the provided callable with the given arguments and returns the result.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">apply</span>[*<span class="hljs-title">Ts</span>](<span class="hljs-params">fn: Callable[[*Ts], int], *args: *Ts</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> fn(*args)


a = apply(<span class="hljs-keyword">lambda</span> x, y: x + y, <span class="hljs-number">10</span>, <span class="hljs-number">10</span>)
</code></pre>
<p>In the <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-parameter-specification">next</a> section, we will also see how to use Parameter Specification (<code>ParamSpec</code>) to define both <code>*args</code> and <code>**kwargs</code>.</p>
<h2 id="heading-parameter-specification">Parameter Specification</h2>
<p>While variadic generics are useful for handling a variable number of type arguments, they are limited to only handling positional arguments. Parameter Specification is a special way of annotating callables that captures both positional and keyword arguments. It is specifically designed to be used with decorators, allowing us to forward argument types between functions and decorators. We will explore this and other uses in this section.</p>
<h3 id="heading-type-annotating-decorators">Type Annotating Decorators</h3>
<p>Before parameter specification became available, it was impossible to type-annotate decorators accurately. This is because the decorator function needs to forward the same type of parameters as the function it decorates, which can be positional, named, or both.</p>
<p>In the following example, we define a simple decorator <code>logging_before_paramspec</code> that logs the function call details before executing the function. However, this decorator does not maintain the types of the original function it decorates. Instead, it uses <code>Callable[..., Any]</code> for both the input and output types, which means it accepts and returns any callable. As a result, the type information of the original function is lost.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any, reveal_type


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">logging_before_paramspec</span>(<span class="hljs-params">func: Callable[..., Any]</span>) -&gt; Callable[..., Any]:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span>(<span class="hljs-params">*args: Any, **kwargs: Any</span>) -&gt; Any:</span>
        print(<span class="hljs-string">f"Calling <span class="hljs-subst">{func.__name__}</span> with <span class="hljs-subst">{args}</span> and <span class="hljs-subst">{kwargs}</span>"</span>)
        <span class="hljs-keyword">return</span> func(*args, **kwargs)

    <span class="hljs-keyword">return</span> wrapper


<span class="hljs-meta">@logging_before_paramspec</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func_</span>(<span class="hljs-params">a: int, b: str</span>) -&gt; float:</span>
    <span class="hljs-keyword">return</span> a + float(b)


reveal_type(my_func_)  <span class="hljs-comment"># Type of "my_func_" is "(...) -&gt; Any"</span>
</code></pre>
<p>Now let's make use of <code>ParamSpec</code> to annotate the decorator, ensuring that the type information of the original function is preserved:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">logging</span>[**<span class="hljs-title">P</span>, <span class="hljs-title">R</span>](<span class="hljs-params">func: Callable[P, R]</span>) -&gt; Callable[P, R]:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span>(<span class="hljs-params">*args: P.args, **kwargs: P.kwargs</span>) -&gt; R:</span>
        print(<span class="hljs-string">f"Calling <span class="hljs-subst">{func.__name__}</span> with <span class="hljs-subst">{args}</span> and <span class="hljs-subst">{kwargs}</span>"</span>)
        <span class="hljs-keyword">return</span> func(*args, **kwargs)

    <span class="hljs-keyword">return</span> wrapper


<span class="hljs-meta">@logging</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func</span>(<span class="hljs-params">a: int, b: str</span>) -&gt; float:</span>
    <span class="hljs-keyword">return</span> a + float(b)


reveal_type(my_func)  <span class="hljs-comment"># Type of "my_func" is "(a: int, b: str) -&gt; float</span>
</code></pre>
<p><code>ParamSpec</code> is unique because it's not used like a regular type parameter. Instead, we declare a generic type parameter with double star e.g. <code>**P</code>. The use of <code>P</code> within a function signature <code>Callable[P, R]</code> will capture all of the arguments, both positional and keyword, of a callable function. You can then use <code>P</code> in another function, or use <code>P.args</code> and <code>P.kwargs</code> to refer to positional arguments or keyword arguments.</p>
<hr />
<p><strong>Note:</strong> <code>ParamSpec</code> became available in Python 3.10. It's still possible to use <code>ParamSpec</code> in earlier versions, such as 3.9, by importing it from <code>typing_extensions</code>.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing_extensions <span class="hljs-keyword">import</span> ParamSpec  <span class="hljs-comment"># noqa: E402</span>

_P = ParamSpec(<span class="hljs-string">"_P"</span>)
</code></pre>
<hr />
<h3 id="heading-concatenating-type-parameters">Concatenating Type Parameters</h3>
<p>Parameter specification can also be used to concatenate type parameters. This is useful when you want to define a function that takes a variable number of arguments and need to add or remove some of the positional arguments between the functions.</p>
<p>In the following example, we use the <code>Concatenate</code> type to concatenate the type parameters of the decorated function with the type parameter of the decorator. This allows us to add one positional argument between the two, enabling us to inject a default value for the <code>format</code> argument of the <code>date2str</code> function.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime  <span class="hljs-comment"># noqa: E402</span>
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any, Concatenate  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inject</span>[<span class="hljs-title">T</span>, **<span class="hljs-title">P</span>, <span class="hljs-title">R</span>](<span class="hljs-params">__arg: T</span>) -&gt; Callable[[Callable[Concatenate[T, P], R]], Callable[P, R]]:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">decorator</span>(<span class="hljs-params">func: Callable[Concatenate[T, P], R]</span>) -&gt; Callable[P, R]:</span>
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span>(<span class="hljs-params">*args: P.args, **kwargs: P.kwargs</span>) -&gt; R:</span>
            <span class="hljs-keyword">return</span> func(__arg, *args, **kwargs)

        <span class="hljs-keyword">return</span> wrapper

    <span class="hljs-keyword">return</span> decorator


<span class="hljs-meta">@inject("%Y-%m-%d")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">date2str</span>(<span class="hljs-params">format: str, dt: datetime</span>) -&gt; str:</span>
    <span class="hljs-keyword">return</span> dt.strftime(format)


dt = date2str(datetime.now())  <span class="hljs-comment"># (function) def date2str(dt: datetime) -&gt; str</span>
print(dt)  <span class="hljs-comment"># 2024-07-24</span>
</code></pre>
<p>The <code>inject</code> decorator is defined with the type variable <code>T</code>, which represents the type of the argument to be injected (like a <code>str</code> format), the parameter specification <code>**P</code> which captures the original function's variable arguments, and the type variable <code>R</code> which defines the decorated function's return type.</p>
<p>The <code>decorator</code> function takes a callable with concatenated type parameters <code>Concatenate[T, P]</code> and returns a new function that only requires the original <code>P</code> arguments while preserving the original return type <code>R</code>.</p>
<p>The <code>wrapper</code> function inside the decorator adds the injected argument <code>__arg</code> as the first positional argument when calling the original function.</p>
<p>It's not often used in practice, but it's still a useful feature. Check out overloads for the <code>curry_flip</code> in the <a target="_blank" href="https://github.com/dbrattli/Expression/blob/main/expression/core/curry.py#L165">Expression library</a> for a more practical example.</p>
<h2 id="heading-overloads">Overloads</h2>
<p>Overloads are useful when you need to define functions that can take different combinations of arguments and perhaps different return types depending on the combinations of arguments.</p>
<p>We can define multiple type signatures by using the <code>@overload</code> decorator from the <code>typing</code> module.</p>
<p>Only the main function should contain the implementation and should be type annotated accepting all cases (often using <code>Any</code>). The overloads should be specific and should not overlap.</p>
<p>In the following example, we define a <code>pipe</code> function that takes a value and up to three positional arguments, each of which is a callable. Each callable takes the output of the previous one as its input. The return type of the <code>pipe</code> function is the result of applying all the callables to the initial value.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable
<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any, overload


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pipe</span>[<span class="hljs-title">_A</span>](<span class="hljs-params">value: _A</span>) -&gt; _A:</span> ...


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pipe</span>[<span class="hljs-title">_A</span>, <span class="hljs-title">_B</span>](<span class="hljs-params">value: _A, fn: Callable[[_A], _B], /</span>) -&gt; _B:</span> ...


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pipe</span>[<span class="hljs-title">_A</span>, <span class="hljs-title">_B</span>, <span class="hljs-title">_C</span>](<span class="hljs-params">
    value: _A,
    fn1: Callable[[_A], _B],
    fn2: Callable[[_B], _C],
    /,
</span>) -&gt; _C:</span> ...


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pipe</span>[<span class="hljs-title">_A</span>, <span class="hljs-title">_B</span>, <span class="hljs-title">_C</span>, <span class="hljs-title">_D</span>](<span class="hljs-params">
    value: _A,
    fn1: Callable[[_A], _B],
    fn2: Callable[[_B], _C],
    fn3: Callable[[_C], _D],
    /,
</span>) -&gt; _D:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pipe</span>(<span class="hljs-params">value: Any, *fns: Callable[[Any], Any]</span>) -&gt; Any:</span>
    <span class="hljs-keyword">return</span> reduce(<span class="hljs-keyword">lambda</span> acc, fn: fn(acc), fns, value)
</code></pre>
<hr />
<p><strong>Note:</strong> The overload decorated definitions are only for the benefit of the type checker. At runtime, only the main function is used. The type checker will use the overload decorated definitions to check if the function is called with the correct arguments and return types.</p>
<hr />
<p>Let's take a look at another example. We can define different overloads for different keyword arguments. In this case, we can use <code>*</code> to explicitly define the end of positional arguments.</p>
<pre><code class="lang-python"><span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">keyword_arguments</span>(<span class="hljs-params">value: int, *, name: str, age: int</span>) -&gt; str:</span> ...


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">keyword_arguments</span>(<span class="hljs-params">value: int, *, city: str, location: int</span>) -&gt; str:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">keyword_arguments</span>(<span class="hljs-params">value: int, **kwargs: Any</span>) -&gt; Any:</span>
    <span class="hljs-string">"""This function is used to demonstrate the use of keyword arguments."""</span>
</code></pre>
<p><strong>Why not use union types instead of overloads?</strong></p>
<p>While it might be tempting to use <code>Union</code> types instead of overloads, it can be challenging to get it right. For example, if we have a function with two arguments and each argument can be of different types, <code>Union</code> types cannot specify which combinations of types are valid. This adds the need to validate the arguments before using them.</p>
<p>When using overloads, the type checker will catch invalid combinations of arguments, eliminating the need for manual validation.</p>
<p>Let's demonstrate this with an example. We'll define a function that takes two arguments and returns their sum. First, we'll annotate the function using <code>Union</code> types.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_with_union</span>(<span class="hljs-params">arg1: int | str, arg2: int | str</span>) -&gt; int | str:</span>
    match arg1, arg2:
        case int(), int():
            <span class="hljs-keyword">return</span> arg1 + arg2
        case str(), str():
            <span class="hljs-keyword">return</span> arg1 + arg2
        case _:
            <span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">"Invalid arguments"</span>)
</code></pre>
<p>There are several issues with the example above. When calling the function we can never be sure if the return type will be <code>int</code> or <code>str</code>:</p>
<pre><code class="lang-python">ret1 = process_with_union(<span class="hljs-number">10</span>, <span class="hljs-number">10</span>)
reveal_type(ret1)  <span class="hljs-comment"># Type of "ret1" is "int | str"</span>

ret2 = process_with_union(<span class="hljs-string">"hello"</span>, <span class="hljs-string">" world"</span>)
reveal_type(ret2)  <span class="hljs-comment"># Type of "ret2" is "int | str"</span>
</code></pre>
<p>This means that if you try to use the return type directly, the type checker will give an error. To resolve this, you will need to use type narrowing before using the return values:</p>
<pre><code class="lang-python">ret3 = ret1 + <span class="hljs-number">10</span>  <span class="hljs-comment"># Error: Operator "+" not supported for types "int | str" and "Literal[10]"</span>
ret4 = ret2 + <span class="hljs-string">"!"</span>  <span class="hljs-comment"># Error: Operator "+" not supported for types "int | str" and "Literal['!']"</span>

<span class="hljs-keyword">if</span> isinstance(ret1, int):
    ret5 = ret1 + <span class="hljs-number">10</span>
    reveal_type(ret5)  <span class="hljs-comment"># Type of "ret5" is "int"</span>

<span class="hljs-keyword">if</span> isinstance(ret2, str):
    ret6 = ret2 + <span class="hljs-string">"!"</span>
    reveal_type(ret6)  <span class="hljs-comment"># Type of "ret6" is "str"</span>
</code></pre>
<p>The type checker will also not warn us if we call the function with an invalid combination of arguments. We will only get a <code>TypeError</code> at runtime.</p>
<pre><code class="lang-python">process_with_union(<span class="hljs-string">"hello"</span>, <span class="hljs-number">10</span>)  <span class="hljs-comment"># type checker does not warn of an error</span>
</code></pre>
<p>Let's instead try to use overloads:</p>
<pre><code class="lang-python"><span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_with_overloads</span>(<span class="hljs-params">arg1: int, arg2: int</span>) -&gt; int:</span>
    <span class="hljs-string">"""Process two integers"""</span>


<span class="hljs-meta">@overload</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_with_overloads</span>(<span class="hljs-params">arg1: str, arg2: str</span>) -&gt; str:</span>
    <span class="hljs-string">"""Process two strings"""</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_with_overloads</span>(<span class="hljs-params">arg1: Any, arg2: Any</span>) -&gt; Any:</span>
    <span class="hljs-string">"""Main function"""</span>
    <span class="hljs-keyword">return</span> arg1 + arg2
</code></pre>
<p>Now, we get a return type from the function that we can use directly without needing to narrow the type beforehand.</p>
<pre><code class="lang-python">ret7: int = process_with_overloads(<span class="hljs-number">10</span>, <span class="hljs-number">10</span>)
reveal_type(ret7)  <span class="hljs-comment"># Type of "ret3" is "int</span>

ret8 = process_with_overloads(<span class="hljs-string">"hello"</span>, <span class="hljs-string">" world"</span>)
reveal_type(ret8)  <span class="hljs-comment"># Type of "ret4" is "str"</span>

ret7 = ret7 + <span class="hljs-number">10</span>
ret8 = ret8 + <span class="hljs-string">"!"</span>
</code></pre>
<p>And we also get a type error when calling the function with an invalid combination of arguments.</p>
<pre><code class="lang-python">process_with_overloads(<span class="hljs-string">"hello"</span>, <span class="hljs-number">10</span>)  <span class="hljs-comment"># Error: No overloads for "process_with_overloads" match the provided arguments</span>
</code></pre>
<p>--- <strong>Note:</strong> As we can see in the above example, docstrings can also be added to the overloads instead of the main function to document each overload. If you don't document each overload then the VSCode Python Extension will fallback to show the main function documentation instead when hovering over the function name.</p>
<hr />
<h2 id="heading-final-notes">Final Notes</h2>
<p>Using type annotations will help you write better code that is easier to understand, more maintainable, easier to refactor, and easier to test. It can ensure robustness by catching potential type errors during static type checking and improving the overall coding experience. Additionally, it will make it easier for AI tools to understand your code and assist with code completion, refactoring, adding new features and finding bugs.</p>
<p>However, typing in Python can be a double-edged sword:</p>
<ul>
<li><p>Updates to MyPY and Pyright will break your code with new releases and you need to take this into your budget. You will spend time fixing type errors with every new release of the type checker.</p>
</li>
<li><p>Many common libraries still do not support typing, e.g. pandas, so you might have to write stubs for these libraries yourself, or reduce the type checking when using these libraries by either using <code># type: ignore</code> or setting type checking mode from <code>strict</code> to <code>basic</code>.</p>
</li>
<li><p>There will be both false positives, and false negatives. You <em>never</em> have this problem with languages such as Java, Scala, C#, or F#. Prefer writing simple code to reduce the number of false positives and false negatives and reduce the time you spend fixing type errors.</p>
</li>
<li><p>Static type checkers are not able to analyze dynamic code that changes at runtime. While mocking is useful for testing, it can potentially bypass or confuse static type checkers.</p>
</li>
</ul>
<p>However, the benefits of using type annotations in Python far outweigh the disadvantages. It will make you a better programmer and make your code more robust and maintainable. If you want to write high quality production grade code, then you should definitely use type annotations in Python!</p>
<h2 id="heading-references">References</h2>
<ul>
<li><a target="_blank" href="https://peps.python.org/pep-0484/">PEP 484 - Type Hints</a></li>
<li><a target="_blank" href="https://peps.python.org/pep-0646/">PEP 646 - Variadic Generics</a></li>
<li><a target="_blank" href="https://www.python.org/dev/peps/pep-0612/">PEP 612 - Parameter Specification Variables</a></li>
<li><a target="_blank" href="https://peps.python.org/pep-0695/">PEP 695 - Type Parameter Syntax</a></li>
<li><a target="_blank" href="https://peps.python.org/pep-0484/#function-method-overloading">PEP 484 - Type Hints</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Python Type Annotations (part 1)]]></title><description><![CDATA[Python Type Annotations is a tutorial in 3 parts: Part 1 (this post) | Part 2 | Part 3

Python's dynamic typing is one of its core strengths. The low friction allows for rapid development that makes it a popular choice for new developers. However, as...]]></description><link>https://cardamomcode.dev/python-type-annotations-part-1</link><guid isPermaLink="true">https://cardamomcode.dev/python-type-annotations-part-1</guid><category><![CDATA[Python]]></category><category><![CDATA[Type Annotation]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Python 3]]></category><dc:creator><![CDATA[Cristina Ferrer]]></dc:creator><pubDate>Mon, 03 Feb 2025 23:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738180738362/e6dba81a-36f0-4ded-8dcb-1c03db09cbf2.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p><strong>Python Type Annotations</strong> is a tutorial in 3 parts: Part 1 (this post) | <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2">Part 2</a> | <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-3">Part 3</a></p>
</blockquote>
<p>Python's dynamic typing is one of its core strengths. The low friction allows for rapid development that makes it a popular choice for new developers. However, as projects grow and evolve, the lack of type annotations makes code difficult to understand and maintain. This can lead to unexpected bugs that are hard to track down.</p>
<p>That Python is a dynamically typed language doesn't mean that Python does not have types, it means that types are not known until the code runs. But over the last years static type checking has become more popular. Type annotations provide a Matrix-like view into the code. Once you learn to read type signatures, you'll feel like you have superpowers!</p>
<p>This blog post will explore type annotations in Python, a feature that can improve code readability and catch potential bugs early. We'll show how to use static type checkers and annotate your code with type hints for basic type annotations, type narrowing, structural sub-typing and callables. In <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2">Part 2</a>, we will cover more advanced topics such as generics, variadic generics paramspec and overloads.</p>
<h2 id="heading-table-of-contents">Table of contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-what-are-type-annotations">What are Type Annotations?</a></li>
<li><a class="post-section-overview" href="#heading-why-do-we-need-type-annotations">Why do we need Type Annotations?</a></li>
<li><a class="post-section-overview" href="#heading-static-type-checkers">Static Type Checkers</a></li>
<li><a class="post-section-overview" href="#heading-installing-and-setup">Installing and Setup</a></li>
<li><a class="post-section-overview" href="#heading-basic-type-annotations">Basic Type Annotations</a></li>
<li><a class="post-section-overview" href="#heading-type-inference">Type Inference</a></li>
<li><a class="post-section-overview" href="#heading-type-narrowing">Type Narrowing</a></li>
<li><a class="post-section-overview" href="#heading-static-duck-typing-and-protocols">Static Duck Typing and Protocols</a></li>
<li><a class="post-section-overview" href="#heading-functions-and-callables">Functions and Callables</a></li>
<li><a class="post-section-overview" href="#heading-references">References</a></li>
</ul>
<hr />
<h2 id="heading-what-are-type-annotations">What are Type Annotations?</h2>
<p>Type annotations, also known as type hints, are a way to specify the type of a variable, function, or argument in Python.</p>
<p>For example, in the following function, we are specifying that the function <code>add</code> takes two arguments <code>x</code> and <code>y</code> of type <code>float</code> as input and returns a value of type <code>float</code> as output.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">x: float, y: float</span>) -&gt; float:</span>
    <span class="hljs-keyword">return</span> x + y
</code></pre>
<h2 id="heading-why-do-we-need-type-annotations">Why do we need type annotations?</h2>
<p>Python type annotations offer significant advantages in modern software development:</p>
<ul>
<li><p><strong>Early Bug Detection</strong>. Type annotations help catch potential type-related issues early in the development process. By identifying errors in the IDE or CI pipeline, they prevent bugs from reaching production, saving time and reducing risks. This is especially crucial for industrial-scale applications where downtime is expensive.</p>
</li>
<li><p><strong>Safer refactoring</strong>. Immediate feedback on type mismatches makes code changes smoother and more reliable. Developers can confidently modify code with real-time type checking support.</p>
</li>
<li><p><strong>Reduce the need for unit testing</strong>. Type annotations minimize the need for extensive type-checking tests, allowing developers to focus on writing tests that validate core business logic rather than basic type compatibility.</p>
</li>
<li><p><strong>Better code clarity</strong>. Unlike traditional docstrings, type annotations are compiler-checked and always in sync with the code. They provide clear, immediate insights into function inputs and outputs, making code more readable and self-documenting.</p>
</li>
</ul>
<h3 id="heading-example-type-safety-in-coordinate-handling">Example: Type safety in coordinate handling</h3>
<p>Let's see how type annotations can prevent common errors using a simple geographic coordinate formatting function.</p>
<p><strong>1. Starting Point: Untyped Code</strong></p>
<p>Consider the following function that formats latitude and longitude:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_location_untyped</span>(<span class="hljs-params">lat, lon</span>):</span>  <span class="hljs-comment"># Error: Type of parameter "lat" is unknown</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">f"Latitude: <span class="hljs-subst">{lat:<span class="hljs-number">.6</span>f}</span>, Longitude: <span class="hljs-subst">{lon:<span class="hljs-number">.6</span>f}</span>"</span>
</code></pre>
<p>This code can lead to multiple issues:</p>
<ul>
<li>We can call it with incorrect types (e.g., <code>str</code> instead of <code>float</code>)</li>
</ul>
<pre><code class="lang-python">get_location_untyped(<span class="hljs-string">"59.32"</span>, <span class="hljs-string">"18.06"</span>)
</code></pre>
<ul>
<li>We can accidentally swap latitude and longitude values</li>
</ul>
<pre><code class="lang-python">get_location_untyped(<span class="hljs-number">59.32</span>, <span class="hljs-number">18.06</span>)
get_location_untyped(<span class="hljs-number">18.06</span>, <span class="hljs-number">59.32</span>)
</code></pre>
<ul>
<li>Neither issue will be caught until runtime</li>
</ul>
<p><strong>2. Adding Basic Type Annotations</strong></p>
<p>We can add basic type annotations to the function signature to specify the expected input types and return type.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_location_typed</span>(<span class="hljs-params">lat: float, lon: float</span>) -&gt; str:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">f"Latitude: <span class="hljs-subst">{lat:<span class="hljs-number">.6</span>f}</span>, Longitude: <span class="hljs-subst">{lon:<span class="hljs-number">.6</span>f}</span>"</span>
</code></pre>
<p>The IDE and type checkers can now detect type-related errors before runtime. (e.g., passing <code>str</code> instead of <code>float</code>) at development time</p>
<pre><code class="lang-python">get_location_typed(<span class="hljs-string">"59.32"</span>, <span class="hljs-string">"18.06"</span>)  <span class="hljs-comment"># Error: "Literal['18.06']" is not assignable to "float"</span>
</code></pre>
<p>However, this still doesn't prevent accidentally swapping parameters:</p>
<pre><code class="lang-python">get_location_typed(<span class="hljs-number">59.32</span>, <span class="hljs-number">18.06</span>)
get_location_typed(<span class="hljs-number">18.06</span>, <span class="hljs-number">59.32</span>)
</code></pre>
<p><strong>3. Domain-Driven Type Safety</strong></p>
<p>One feature of domain-driven design is to create distinct types for values that are semantically different. For example, instead of using a generic <code>float</code> type for both a <code>lat</code> and <code>lon</code> you would create specific types like <code>Lat</code> and <code>Lon</code> to represent these values. This helps in making the code more expressive and reducing the possibility of errors.</p>
<p>A less known feature in Python is <code>NewType</code> from the <code>typing</code> module. <code>NewType</code> is a great option for creating distinct types for values that are semantically different but share the same underlying type.</p>
<p>While subclassing is an alternative, <code>NewType</code> offers a more lightweight approach to type safety with zero runtime overhead.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> NewType  <span class="hljs-comment"># noqa: E402</span>

Lat = NewType(<span class="hljs-string">"Lat"</span>, float)
Lon = NewType(<span class="hljs-string">"Lon"</span>, float)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_location</span>(<span class="hljs-params">lat: Lat, lon: Lon</span>) -&gt; str:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">f"Latitude: <span class="hljs-subst">{lat:<span class="hljs-number">.6</span>f}</span>, Longitude: <span class="hljs-subst">{lon:<span class="hljs-number">.6</span>f}</span>"</span>
</code></pre>
<p>Swapping the parameters will now raise a type error at compile time.</p>
<pre><code class="lang-python">get_location(Lat(<span class="hljs-number">59.32</span>), Lon(<span class="hljs-number">18.06</span>))
get_location(Lon(<span class="hljs-number">18.06</span>), Lat(<span class="hljs-number">59.32</span>))  <span class="hljs-comment"># Error: "Lon" is not assignable to "Lat",  "Lat" is not assignable to "Lon"</span>
</code></pre>

<h2 id="heading-static-type-checking">Static Type Checking</h2>
<p>Static type checking occurs without running the program. This is a standard feature in languages like Java and C#, where it is integrated into the compilation phase, and is required for identifying type mismatches and potential errors before an executable can be produced.</p>
<p>In Python, static type checking is optional. You can add type hints to your code, and use a static type checker to catch type errors before running the code. You can think of it as debugging your code up-front.</p>
<h3 id="heading-static-type-checkers">Static Type Checkers</h3>
<p>There are several static type checkers available for Python:</p>
<ul>
<li><a target="_blank" href="https://github.com/microsoft/pyright">Pyright</a> by Microsoft is a full-featured, standards-based static type checker for Python.</li>
<li><a target="_blank" href="https://github.com/python/mypy">MyPY</a> by Dropbox et al. is an optional static type checker for Python that aims to combine the benefits of dynamic (or "duck") typing and static typing.</li>
<li><a target="_blank" href="https://pyre-check.org/">Pyre</a> by Facebook is a performant type checker for Python compliant with <a target="_blank" href="https://peps.python.org/pep-0484/">PEP 484</a>. Pyre can analyze codebases with millions of lines of code incrementally.</li>
<li><a target="_blank" href="https://google.github.io/pytype/">Pytype</a> by Google checks and infers types for your Python code without requiring type annotations.</li>
</ul>
<p>In this blog post, we will use Pyright, a fast type checker for Python that is written in TypeScript and integrated with Visual Studio Code through the Pylance extension.</p>
<h2 id="heading-installing-and-setup">Installing and Setup</h2>
<ol>
<li>Enable <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=ms-python.python">Pylance</a> in Visual Studio Code.</li>
</ol>
<p>If you are using the <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=ms-python.python">Python extension</a> for Visual Studio Code, it will automatically install Pylance.</p>
<ol start="2">
<li>Configure the type checking level in your <code>pyproject.toml</code> file:</li>
</ol>
<pre><code class="lang-toml"><span class="hljs-section">[tool.pyright]</span> <span class="hljs-attr">typeCheckingMode</span> = <span class="hljs-string">"strict"</span>  <span class="hljs-comment"># Options: "off", "basic", "standard", "strict"</span>
</code></pre>

<h2 id="heading-basic-type-annotations">Basic Type Annotations</h2>
<p>For basic type annotations, we will cover annotating primitive types, container types and simple functions.</p>
<h3 id="heading-primitive-types">Primitive types</h3>
<p>These types represent single values rather than collections. Below are examples of the most common primitive type annotations:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Literal

speakers: int = <span class="hljs-number">2</span>
talk: str = <span class="hljs-string">"Python type annotations"</span>
active: bool = <span class="hljs-literal">True</span>
pi: float = <span class="hljs-number">3.14159</span>
number: int | <span class="hljs-literal">None</span> = <span class="hljs-number">42</span>
status: Literal[<span class="hljs-string">"active"</span>, <span class="hljs-string">"inactive"</span>] = <span class="hljs-string">"active"</span>
<span class="hljs-comment"># .. is the same as</span>
status: Literal[<span class="hljs-string">"active"</span>] | Literal[<span class="hljs-string">"inactive"</span>] = <span class="hljs-string">"active"</span>
</code></pre>
<h3 id="heading-container-types">Container types</h3>
<p>A container is a type that can hold multiple values, such as <code>list</code>, <code>tuple</code>, <code>set</code>, and <code>dict</code>. The inner type of the container must also be annotated. For example, a list of integers can be annotated as <code>list[int]</code>.</p>
<p>Below we can see that attempting to add incompatible values to our containers results in errors:</p>
<pre><code class="lang-python">xs: list[int] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
xs.append(<span class="hljs-string">"2"</span>)  <span class="hljs-comment"># Error: "Literal['2']" is not assignable to "int"</span>

ts: tuple[int, str] = (<span class="hljs-number">1</span>, <span class="hljs-string">"test"</span>)

us: set[int | str] = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">"test"</span>}

vs: dict[str, int] = {<span class="hljs-string">"one"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"two"</span>: <span class="hljs-number">2</span>}
vs[<span class="hljs-string">"one"</span>] = <span class="hljs-number">2</span>
vs[<span class="hljs-string">"three"</span>] = <span class="hljs-string">"three"</span>  <span class="hljs-comment"># Error: "Literal['three']" is not assignable to "int"</span>
</code></pre>
<h3 id="heading-annotating-functions">Annotating Functions</h3>
<p>Function annotations in Python allow us to specify both parameter types and return types. The syntax uses colons <code>:</code> for parameter annotation and an arrow <code>-&gt;</code> for the return type annotation.</p>
<p>Here's an example with a <code>divide</code> function that accepts two <code>float</code> parameters, and returns a union type of <code>float</code> or <code>None</code>, this is also called an optional type:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">divide</span>(<span class="hljs-params">a: float, b: float</span>) -&gt; float | <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">if</span> b == <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>
    <span class="hljs-keyword">return</span> a / b
</code></pre>
<p>When working with functions that return union types, the type checker will prevent us from directly using the result since it might be <code>None</code>. For example, the following code will flag an error:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type  <span class="hljs-comment"># noqa: E402</span>

result = divide(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "float | None"</span>

d = result + <span class="hljs-number">1</span>  <span class="hljs-comment"># Error: Operator "+" not supported for "None"</span>
</code></pre>
<p>To safely use the result, we need to perform type narrowing through conditional checks. There is a specific section that will cover <a class="post-section-overview" href="#heading-type-narrowing">type narrowing</a> in more detail, showing various ways to safely work with union types. For now, we will ensure that the result is a <code>float</code> before we can use it in an arithmetic operation.</p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> result <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
    d = result + <span class="hljs-number">1</span>  <span class="hljs-comment"># OK</span>
</code></pre>
<hr />
<p><strong>Note:</strong> In this post we will be using the function <a target="_blank" href="https://docs.python.org/3/library/typing.html#typing.reveal_type"><code>reveal_type</code></a> to ask the static type checker to reveal the inferred type of its argument.</p>
<p>Most type checkers support <code>reveal_type()</code> even if the name is not imported from typing. However, to avoid runtime errors it should be imported from the typing module. At runtime, this function prints the runtime type of its argument and returns the argument unchanged.</p>
<hr />
<h2 id="heading-type-inference">Type Inference</h2>
<p>Type inference is a powerful feature that allows programming languages to automatically determine variable types without explicit type annotations.</p>
<p>MyPy and Pyright will try to infer the type of unannotated variables and parameters based on context. Pyright will also try to infer the type of unannotated return types, while MyPy will interpret unannotated return types as <code>Any</code>.</p>
<p>For example, when a variable is assigned a specific value, type checkers can infer a precise type:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type  <span class="hljs-comment"># noqa: E402</span>

a = <span class="hljs-number">10</span>
reveal_type(a)  <span class="hljs-comment"># Type of "a" is "Literal[10]"</span>
</code></pre>
<h3 id="heading-complementing-type-inference">Complementing Type Inference</h3>
<p>Despite its convenience, type inference has limitations. In some cases, type inference is not enough to understand the type of a variable and developers often need to provide additional type information in several scenarios:</p>
<ul>
<li><strong>Giving a broader type to an object</strong></li>
</ul>
<p>The type checker will assume that if we initialize a list with a homogenous set of objects, probably that's what we intended.</p>
<pre><code class="lang-python">xs = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
reveal_type(xs)  <span class="hljs-comment"># Type of "xs" is "list[int]"</span>
xs.append(<span class="hljs-string">"2"</span>)  <span class="hljs-comment"># Error: "Literal['2']" is not assignable to "int"</span>

<span class="hljs-comment"># allow a broader type than the one inferred</span>
xz: list[int | str] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
xz.append(<span class="hljs-string">"2"</span>)
</code></pre>
<ul>
<li><strong>Restrict new types assigned to a variable</strong></li>
</ul>
<p>To enforce type consistency throughout an application type annotations can ensure that subsequent value assignments are compatible with the initially declared type.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Variable type changes dynamically without type annotation</span>
x = <span class="hljs-number">10</span>
reveal_type(x)  <span class="hljs-comment"># Type of "x" is "Literal[10]"</span>

<span class="hljs-comment"># Redeclare the variable with a different type</span>
x = <span class="hljs-string">"10"</span>
reveal_type(x)  <span class="hljs-comment"># Type of "x" is "Literal['10']"</span>

<span class="hljs-comment"># Annotate a variable to restrict the type</span>
xx: int = <span class="hljs-number">10</span>
reveal_type(xx)  <span class="hljs-comment"># Type of "xx" is "Literal[10]"</span>
xx = <span class="hljs-string">"10"</span>  <span class="hljs-comment"># Error: Type "Literal['10']" is not assignable to declared type "int"</span>
</code></pre>
<ul>
<li><strong>Initialize empty containers</strong></li>
</ul>
<p>Empty containers are initially typed as <code>Any</code>, requiring explicit type specification:</p>
<pre><code class="lang-python">lst_ = []
reveal_type(lst_)  <span class="hljs-comment"># Type of "lst" is "Any"</span>
lst_.append(<span class="hljs-number">1</span>)  <span class="hljs-comment"># Error: Type of "append" is partially unknown</span>

<span class="hljs-comment"># Specify the expected type</span>
lst: list[int] = []
lst.append(<span class="hljs-number">1</span>)
</code></pre>
<ul>
<li><strong>Validating function output types</strong></li>
</ul>
<p>Type annotations serve as a powerful mechanism for ensuring type correctness, particularly when dealing with external or untyped code. By explicitly defining expected return types, developers can intercept type mismatches during code refactoring, prevent silent type-related errors from spreading through the application and create a robust type verification layer for third-party or legacy functions.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Third-party function without type annotations</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_status</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"active"</span>


<span class="hljs-comment"># Enforcing type constraints within the application</span>
type Status = Literal[<span class="hljs-string">"active"</span>, <span class="hljs-string">"inactive"</span>]
status: Status = get_status()
</code></pre>
<h3 id="heading-any-vs-object">Any vs. object</h3>
<p>In Python, <code>Any</code> and <code>object</code> are two special types that can be used to represent unknown, any or arbitrary types. However, they have different semantics and use cases.</p>
<ul>
<li><p><code>object</code> is the base class for all Python objects. It can be used to represent any object in Python, and it is often used when the specific type of an object is not important.</p>
</li>
<li><p><code>Any</code> is a special type that can also represent any value. However, the <code>Any</code> type is far more flexible than <code>object</code> because it effectively opts out of type checking all-together. It will allow any operation to be performed on the value. Any other value can be assigned to the value, and a value with type <code>Any</code> can be assigned to a variable of any other type.</p>
</li>
</ul>
<p>Using <code>Any</code> can be tempting since it solves most type related issues you might have. But you should be very cautious when using <code>Any</code>, since it opts out of type checking and can lead to runtime errors. Using <code>object</code> is a much safer choice when the specific type of an object is not important.</p>
<p>The following example demonstrates the difference between <code>Any</code> and <code>object</code>:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-comment"># This function passes type checking</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_any</span>(<span class="hljs-params">x: Any</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    x.arbitrary_method()  <span class="hljs-comment"># No complaints</span>
    _ = x + <span class="hljs-number">5</span>  <span class="hljs-comment"># No complaints</span>
    x[<span class="hljs-string">"key"</span>]  <span class="hljs-comment"># No complaints</span>


<span class="hljs-comment"># This function raises type checker warnings</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_object</span>(<span class="hljs-params">x: object</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    x.arbitrary_method()  <span class="hljs-comment"># Error: Cannot access attribute "arbitrary_method" for class "object"</span>
    _ = x + <span class="hljs-number">5</span>  <span class="hljs-comment"># Error: Operator "+" not supported for "object" and "Literal[5]"</span>
    x[<span class="hljs-string">"key"</span>]  <span class="hljs-comment"># Error: __getitem__ not supported for class "object"</span>
</code></pre>
<h2 id="heading-type-narrowing">Type Narrowing</h2>
<p>Type narrowing is the act of making the type of a variable more narrow or specific e.g. that <code>Animal</code> is actually a <code>Cat</code>, or that <code>Optional[int]</code> i.e <code>int | None</code> in some cases must be <code>int</code> (or just <code>None</code>). This can be done by using special type narrowing expressions, statements and type guards.</p>
<p>While these checks are performed at runtime, static type checkers also use these constructs for type narrowing during static analysis. By narrowing the type of a variable, we can write more type-safe code and catch errors at compile time rather than at runtime.</p>
<p>In this section, we will also discuss type casting and why it should be avoided.</p>
<p>The following examples will use the <code>divide</code> function from the <a class="post-section-overview" href="#heading-annotating-functions">previous</a> section:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">divide</span>(<span class="hljs-params">a: float, b: float</span>) -&gt; float | <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">if</span> b == <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>
    <span class="hljs-keyword">return</span> a / b


result = divide(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
</code></pre>
<h3 id="heading-type-narrowing-expressions">Type Narrowing Expressions</h3>
<p>There are several built-in expressions in Python that can be used to narrow the type of a variable. These include:</p>
<ul>
<li><code>isinstance</code> - Check if an object is an instance of a class.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type  <span class="hljs-comment"># noqa: E402</span>

<span class="hljs-keyword">if</span> isinstance(result, float):
    reveal_type(result)  <span class="hljs-comment"># Type of "result" is "float"</span>
<span class="hljs-keyword">else</span>:
    reveal_type(result)  <span class="hljs-comment"># Type of "result" is "None"</span>
</code></pre>
<ul>
<li><code>issubclass</code> - Check if a class is a subclass of another class.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># pyright does not support giving expressions to isinstance, https://github.com/microsoft/pyright/issues/3565</span>
<span class="hljs-comment"># so issubclass(type(result), str)  will not narrow the type of result</span>

result_type = type(result)
reveal_type(result_type)  <span class="hljs-comment"># Type of "result_type" is "type[float] | type[None]"</span>

<span class="hljs-keyword">if</span> issubclass(result_type, float):
    reveal_type(result_type)  <span class="hljs-comment"># Type of "result_type" is "type[float]"</span>
<span class="hljs-keyword">else</span>:
    reveal_type(result_type)  <span class="hljs-comment"># Type of "result_type" is "type[None]"</span>
</code></pre>
<ul>
<li><code>callable</code> - Check if an object is callable.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">factory</span>() -&gt; Callable[[float, float], float | <span class="hljs-keyword">None</span>] | <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-keyword">return</span> divide


divide_ = factory()
reveal_type(divide_)  <span class="hljs-comment"># Type of "divide_" is "((float, float) -&gt; (float | None)) | None"</span>

<span class="hljs-keyword">if</span> callable(divide_):
    reveal_type(divide_)  <span class="hljs-comment"># Type of "divide_" is "(float, float) -&gt; (float | None)"</span>
<span class="hljs-keyword">else</span>:
    reveal_type(divide_)  <span class="hljs-comment"># Type of "divide_" is "None"</span>
</code></pre>
<ul>
<li><code>is</code> - Check if two objects are the same.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> result <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
    reveal_type(result)  <span class="hljs-comment"># Type of "result" is "float"</span>
<span class="hljs-keyword">else</span>:
    reveal_type(result)  <span class="hljs-comment"># Type of "result" is "None"</span>
</code></pre>
<h3 id="heading-type-narrowing-statements">Type Narrowing Statements</h3>
<p>There are several built-in statements in Python that can be used to narrow the type of a variable. These include:</p>
<ul>
<li><code>if</code> - Check if a value is truthy.</li>
</ul>
<p>In the previous example, we use an <code>if</code> statement to check if result is not <code>None</code>. This narrows the type of result within the <code>if</code> block to <code>float</code>, allowing us to safely perform operations that require a <code>float</code> type. If result is <code>None</code>, the <code>else</code> block handles that case separately.</p>
<ul>
<li><code>assert</code> - Assert if a value is truthy.</li>
</ul>
<p>An <code>assert</code> statement will be executed at runtime, and if the condition fails, an <code>AssertionError</code> is raised.</p>
<pre><code class="lang-python"><span class="hljs-keyword">assert</span> result
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "float | int"</span>
</code></pre>
<ul>
<li><code>match</code> - Check a value against a series of patterns.</li>
</ul>
<p>The <code>match</code> statement is a new feature in Python 3.10 that allows us to match a value against a series of patterns and execute the corresponding block of code. The <code>match</code> statement can be used to narrow the type of a variable based on the pattern that matches the value. Using the <code>match</code> statement is essentially a more concise way of writing <code>isinstance</code> checks, but it can be more readable and ensures that the check is exhaustive so that no case if left unchecked.</p>
<pre><code class="lang-python">result = divide(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
match result:
    case float(f) | int(f):
        reveal_type(f)  <span class="hljs-comment"># Type of "f" is "float | int"</span>
    case <span class="hljs-literal">None</span>:
        reveal_type(result)  <span class="hljs-comment"># Type of "result" is "None"</span>
</code></pre>
<h3 id="heading-user-defined-type-guards">User Defined Type Guards</h3>
<p>We can add our own type guards for custom types as well. Type guards are special functions that help the type checker narrow down the type of a variable based on runtime checks. They return a boolean value and have a special return type <code>TypeGuard</code>, which indicates to the type checker that the function is a type guard and can be used to narrow the type of a variable to the specified type.</p>
<p>Type guards are functional at runtime, affecting the program's flow based on their checks, but they are also used by static type checkers to narrow the types during static analysis.</p>
<p>In the following example, we define a type guard <code>all_int</code> that checks if all elements in a list are of type <code>int</code>. This allows the type checker to narrow the type of the list based on the result of the check.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Any, TypeGuard  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">all_int</span>(<span class="hljs-params">xs: list[Any]</span>) -&gt; TypeGuard[list[int]]:</span>
    <span class="hljs-keyword">return</span> all(isinstance(x, int) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> xs)


xs = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">"test"</span>]  <span class="hljs-comment"># list[int | str]</span>

<span class="hljs-keyword">if</span> all_int(xs):
    reveal_type(xs)  <span class="hljs-comment"># Type of "xs" is "list[int]"</span>
<span class="hljs-keyword">else</span>:
    reveal_type(xs)  <span class="hljs-comment"># Type of "xs" is "list[int | str]"</span>
</code></pre>
<h3 id="heading-type-casting">Type Casting</h3>
<p>Type casting is used to explicitly specify a different type for a variable. Using <code>cast</code> doesn't actually change the type of a variable at runtime; it only informs the type checker to treat the variable as a different type without performing any runtime type conversion.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> cast  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_data</span>(<span class="hljs-params">data: object</span>) -&gt; str:</span>
    <span class="hljs-comment"># We might know this is actually a string, but the type checker doesn't</span>
    <span class="hljs-keyword">return</span> cast(str, data).upper()


<span class="hljs-comment"># Usage</span>
result = process_data(<span class="hljs-string">"hello"</span>)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "str"</span>
</code></pre>
<p>It should be avoided if possible, since it can hide mistakes and lead to runtime errors.</p>
<pre><code class="lang-python">a = <span class="hljs-number">1</span>
b = cast(str, a)
reveal_type(b)  <span class="hljs-comment"># Type of "b" is "str"</span>

<span class="hljs-comment"># cast is omitted in runtime, so this will raise a TypeError</span>
print(b + <span class="hljs-string">"c"</span>)  <span class="hljs-comment"># TypeError: unsupported operand type(s) for +: 'int' and 'str'</span>
</code></pre>
<h2 id="heading-static-duck-typing-and-protocols">Static Duck Typing and Protocols</h2>
<p>Python is well-known for its duck typing, a programming approach that focuses on what an object can do rather than what it is. If an object implements the methods and attributes we need, we can work with it regardless of its class hierarchy. This flexibility is great, but it raises an interesting challenge:</p>
<p><strong>How do we combine duck typing's dynamic nature with static type annotations?</strong></p>
<p>Let's explore this concept with an example:</p>
<p>We will define two classes <code>Rabbit</code> and <code>Fox</code> which share a common method called feed:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rabbit</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Rabbit is running"</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Rabbit is eating"</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Fox</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Ring-ding-ding-ding!"</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Fox is eating"</span>
</code></pre>
<p>And a function that takes an animal parameter and calls its <code>feed</code> method:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal_untyped</span>(<span class="hljs-params">animal</span>):</span>  <span class="hljs-comment"># Error: Type of parameter "animal" is unknown</span>
    animal.feed()  <span class="hljs-comment"># Error: Type of "feed" is unknown</span>
    ...


care_for_animal_untyped(Rabbit())
care_for_animal_untyped(Fox())
</code></pre>
<p><strong>How can we type the <code>animal</code> parameter?</strong></p>
<p>We have several options, but each has drawbacks:</p>
<ul>
<li>Creating an <code>Animal</code> base class would limit the flexibility of duck typing.</li>
<li>Using <code>Union[Rabbit, Fox]</code> would restrict us to specific types.</li>
<li>Using <code>Any</code> would disable type checking entirely, allowing objects without a feed method.</li>
</ul>
<p>A better approach is to use Protocols, which enable static duck typing (also known as structural subtyping). Protocols let us define a set of methods that a class must implement without requiring inheritance from a base class.</p>
<p>To create a protocol, we use the <code>Protocol</code> base class from the <code>typing</code> module. In the following example, we define a <code>CareFor</code> protocol that requires the implementation of a <code>feed</code> method:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Protocol, runtime_checkable  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-meta">@runtime_checkable</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CareFor</span>(<span class="hljs-params">Protocol</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">feed</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">care_for_animal</span>(<span class="hljs-params">animal: CareFor</span>):</span>
    animal.feed()
    ...


care_for_animal(Rabbit())
care_for_animal(Fox())
</code></pre>
<p>The type checker ensures that the <code>animal</code> parameter in the <code>care_for_animal</code> function will accept any class that has a method <code>feed</code>. Trying to use a class without the required methods will raise a type error:</p>
<pre><code class="lang-python"><span class="hljs-comment"># This would fail type checking</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>:</span>
    <span class="hljs-string">"""A class that doesn't implement the CareFor protocol."""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bark</span>(<span class="hljs-params">self</span>) -&gt; str:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Woof!"</span>


care_for_animal(Dog())  <span class="hljs-comment"># Error: Dog doesn't implement CareFor protocol</span>
</code></pre>
<hr />
<p><strong>Note:</strong> The use of the <code>@runtime_checkable</code> decorator is required to enable runtime type checking using <code>isinstance</code>. Without it, the <code>CareFor</code> protocol would not be recognized as a valid type at runtime:</p>
<pre><code class="lang-python"><span class="hljs-keyword">assert</span> isinstance(Rabbit(), CareFor)
</code></pre>
<hr />
<p>You can use Protocol types just like any other type annotation:</p>
<pre><code class="lang-python">animal: CareFor = Rabbit()
animals: list[CareFor] = [Rabbit(), Fox()]
</code></pre>
<p>Protocols are meant to define a set of methods and attributes that a class must implement, but they are not meant to be instantiated themselves:</p>
<pre><code class="lang-python">animal = CareFor()  <span class="hljs-comment"># Error: Cannot instantiate protocol class "CareFor"</span>
</code></pre>
<h3 id="heading-built-in-protocols">Built-in Protocols</h3>
<p>The <code>typing</code> module includes several protocol classes that represent common Python interfaces. Let's take a look at a few of them:</p>
<ul>
<li><code>Iterable[T]</code>: implements<code>__iter__</code> method.</li>
</ul>
<p>In the example below, the class <code>BookCollection</code> contains an <code>__iter__</code> method, which means it adheres to the iterable protocol and can be used wherever <code>Iterable[T]</code> is expected, such as in a for loop.</p>
<p><code>Iterable[T]</code> is a generic type. <em>Generics</em> are covered in more detail in <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-generics">Part 2</a> of this series.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookCollection</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
        self.books: list[tuple[str, str]] = []

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_book</span>(<span class="hljs-params">self, title: str, author: str</span>):</span>
        self.books.append((title, author))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> iter(self.books)


library = BookCollection()
library.add_book(<span class="hljs-string">"1984"</span>, <span class="hljs-string">"George Orwell"</span>)

<span class="hljs-keyword">for</span> title, author <span class="hljs-keyword">in</span> library:
    print(<span class="hljs-string">f"<span class="hljs-subst">{title}</span> by <span class="hljs-subst">{author}</span>"</span>)
</code></pre>
<ul>
<li><code>Container[T]</code>: implements <code>__contains__</code> method.</li>
</ul>
<p>In the example below, the class <code>TagCollection</code> contains a <code>__contains__</code> method, which means it adheres to the container protocol and can be used wherever <code>Container[T]</code> is expected, such as in the <code>in</code> operator.</p>
<p><code>Container[T]</code> is a generic type. <em>Generics</em> are covered in more detail in <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-generics">Part 2</a> of this series.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TagCollection</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
        self.tags: set[str] = set()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_tag</span>(<span class="hljs-params">self, tag: str</span>):</span>
        self.tags.add(tag.lower())

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span>(<span class="hljs-params">self, item: str</span>):</span>
        <span class="hljs-keyword">return</span> item.lower() <span class="hljs-keyword">in</span> self.tags


tags = TagCollection()
tags.add_tag(<span class="hljs-string">"Python"</span>)
tags.add_tag(<span class="hljs-string">"Programming"</span>)

print(<span class="hljs-string">"python"</span> <span class="hljs-keyword">in</span> tags)  <span class="hljs-comment"># True</span>
print(<span class="hljs-string">"Java"</span> <span class="hljs-keyword">in</span> tags)  <span class="hljs-comment"># False</span>
</code></pre>
<ul>
<li><code>SupportsFloat</code>: implements <code>__float__</code> method.</li>
</ul>
<p>In the example below, the class <code>Temperature</code> contains a <code>__float__</code> method, which means it adheres to the <code>SupportsFloat</code> protocol and can be used wherever <code>SupportsFloat</code> is expected, such as in the <code>float()</code> function.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> SupportsFloat  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Temperature</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, celsius: float</span>):</span>
        self._celsius = celsius

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__float__</span>(<span class="hljs-params">self</span>) -&gt; float:</span>
        <span class="hljs-keyword">return</span> self._celsius


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">celsius_to_kelvin</span>(<span class="hljs-params">celcius: SupportsFloat</span>) -&gt; float:</span>
    <span class="hljs-keyword">return</span> float(celcius) + <span class="hljs-number">273.15</span>


temp = Temperature(<span class="hljs-number">25.0</span>)
kelvin = celsius_to_kelvin(temp)
</code></pre>
<h3 id="heading-protocol-inheritance">Protocol Inheritance</h3>
<p>Protocols can be extended like regular classes, but with an important distinction:</p>
<ul>
<li><p>Just inheriting from an existing <code>Protocol</code> creates a regular class that implements the <code>Protocol</code>.</p>
</li>
<li><p>To create a new <code>Protocol</code>, you must explicitly include <code>Protocol</code> in the inheritance.</p>
</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AdvancedCare</span>(<span class="hljs-params">CareFor, Protocol</span>):</span>
    <span class="hljs-string">"""Protocol for animals requiring advanced care."""</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">groom</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">exercise</span>(<span class="hljs-params">self</span>) -&gt; str:</span> ...
</code></pre>
<h2 id="heading-functions-and-callables">Functions and Callables</h2>
<p>We have already seen that we can annotate functions with type hints. These annotations ensure both proper function usage and correct handling of the return value.</p>
<p>Python functions can take many forms and perform a variety of tasks. You can create functions that accept other functions as inputs, functions that produce new functions as outputs, and functions that handle a flexible number of arguments including default parameters, variadic arguments (<code>*args</code>) and keyword arguments (<code>**kwargs</code>). Functions can also act as generators using <code>yield</code> or run asynchronously using <code>async</code> and <code>await</code>.</p>
<p>In addition, Python also includes a broader concept called "callables" - essentially anything you can execute using parentheses. This category encompasses regular functions, class methods, and classes that implement the <code>__call__</code> method. For more complex scenarios, you can define callable protocols, that can be used to specify precise signatures for these callable objects.</p>
<h3 id="heading-basic-functions">Basic Functions</h3>
<p>Let's review how to annotate basic functions.</p>
<p>Below is a simple function that adds two integers. The type hints <code>a: int</code> and <code>b: int</code> specify that both parameters must be integers, while <code>-&gt; int</code> indicates that the function returns an integer.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> reveal_type


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum_two</span>(<span class="hljs-params">a: int, b: int</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> a + b


result = sum_two(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
reveal_type(result)  <span class="hljs-comment"># Type of "result" is "int"</span>
</code></pre>
<h3 id="heading-variadic-arguments">Variadic Arguments</h3>
<p>Variadic arguments are variable-length arguments often referred to as <code>*args</code> in Python, which allow us to pass a variable number of positional arguments to the function.</p>
<h4 id="heading-annotate-args-with-the-same-type">Annotate *args with the same type</h4>
<p>Let's take a look at how we can annotate variadic arguments of the same type. In the example below, we have a function <code>sum_all</code> that takes a variable number of integers and returns their sum:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum_all</span>(<span class="hljs-params">*args: int</span>) -&gt; int:</span>
    reveal_type(args)  <span class="hljs-comment"># Type of "args" is "tuple[int, ...]"</span>
    <span class="hljs-keyword">return</span> sum(args)


sum_all(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
sum_all(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
</code></pre>
<p>The type of the <code>args</code> variable is a tuple with an ellipsis <code>...</code> at the end. A tuple annotated as <code>tuple[T, ...]</code> means that all the elements are of the same type, in this case the <code>args</code> variable is a tuple of <code>int</code>s.</p>
<h4 id="heading-annotate-args-with-different-types">Annotate *args with different types</h4>
<p>To annotate variadic arguments of different types, we can define a predefined tuple that contains the different types, and we can use it with the star <code>*</code> operator to unpack it. In this example, calling <code>foo</code> with only one argument will raise an error:</p>
<pre><code class="lang-python">type Args = tuple[int, str, float]


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span>(<span class="hljs-params">*args: *Args</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    reveal_type(args)  <span class="hljs-comment"># Type of "args" is "tuple[int, str, float]"</span>
    ...


foo(<span class="hljs-number">42</span>, <span class="hljs-string">"test"</span>, <span class="hljs-number">10.3</span>)
foo(<span class="hljs-number">42</span>)  <span class="hljs-comment"># Error: Arguments missing for parameters "args[1]", "args[2]"</span>
</code></pre>
<p>--- <strong>Note</strong>: Defining a predefined tuple will restrict the number of arguments that can be passed to the function. To allow for a variable number of arguments of different types, we can use <em>Variadic Generics</em>. We will cover this in more detail in <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-variadic-generics">Part 2</a>.</p>
<hr />
<h3 id="heading-keyword-arguments">Keyword Arguments</h3>
<p>Keyword arguments are also variable-length arguments, often referred to as <code>**kwargs</code> in Python, which allow us to pass a variable number of arguments by name in any order, and to specify default values for the arguments.</p>
<h4 id="heading-annotate-kwargs-with-the-same-type">Annotate **kwargs with the same type</h4>
<p>When annotating <code>kwargs</code> with a single type, for example <code>int</code>,  the real type of the <code>kwargs</code> parameter is <code>dict[str, int]</code>, where the keys are always <code>str</code> representing the parameter names:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum_integer_kwargs</span>(<span class="hljs-params">**kwargs: int</span>) -&gt; int:</span>
    reveal_type(kwargs)  <span class="hljs-comment"># Type of "kwargs" is "dict[str, int]"</span>
    <span class="hljs-keyword">return</span> sum(kwargs.values())


sum_integer_kwargs(a=<span class="hljs-number">1</span>, b=<span class="hljs-number">2</span>, c=<span class="hljs-number">3</span>)
</code></pre>
<h4 id="heading-annotate-kwargs-with-different-types">Annotate **kwargs with different types</h4>
<p>To annotate <code>kwargs</code> with different types, we can use <code>TypeDict</code> from the <code>typing</code> module to specify the name and the type of each of the keyword arguments:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> NotRequired, TypedDict, Unpack  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>(<span class="hljs-params">TypedDict</span>):</span>
    name: str
    age: int
    is_student: bool
    address: NotRequired[str]  <span class="hljs-comment"># optional parameter</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">greet</span>(<span class="hljs-params">**kwargs: Unpack[Person]</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    reveal_type(kwargs)  <span class="hljs-comment"># Type of "kwargs" is "Person"</span>
    ...


greet(name=<span class="hljs-string">"John"</span>, age=<span class="hljs-number">20</span>, is_student=<span class="hljs-literal">True</span>)
greet(name=<span class="hljs-string">"John"</span>, age=<span class="hljs-number">20</span>)  <span class="hljs-comment"># Error: Argument missing for parameter "is_student"</span>
</code></pre>

<hr />
<p><strong>Note</strong>:</p>
<ul>
<li><p>We cannot use the <code>**</code> to unpack a <code>TypeDict</code>, instead we need to use <code>Unpack</code> from the <code>typing</code> module.</p>
</li>
<li><p><code>TypeDict</code> does not support assigning default values. To annotate <code>kwargs</code> with default values we can use <em>Callback Protocols</em> which we will cover in the <a class="post-section-overview" href="#heading-callback-protocols">next</a> section.</p>
</li>
<li><p>Similarly as with <code>*args</code>, defining a <code>TypedDict</code> will restrict the number of arguments that can be passed to the function. To annotate <code>kwargs</code> with a variable number of arguments of different types, we can use <em>Parameter Specification</em>. We will cover this in more detail in <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-parameter-specification">Part 2</a>.</p>
</li>
</ul>
<hr />
<p><strong>What is the benefit of unpacking typed tuples and dictionaries instead of just annotating each of the arguments as normal positional and keyword arguments?</strong></p>
<p>The main benefit might not be obvious at first, but it allows us to define a data model outside the function. This way, the function can depend on the data model instead of needing to know about the specific arguments.</p>
<p>In <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2#heading-functions-with-variadic-generics">Part 2</a>, we will see that we can combine tuple unpacking with variadic generics to avoid limiting the number of arguments. For example, the first argument can be pinned to a specific type, while the rest of the arguments can be of any type.</p>
<h3 id="heading-callables">Callables</h3>
<p>We can also annotate callables. This is useful, for example, for functions that take other functions as arguments, or functions that return functions. A callable can be annotated using the <code>Callable</code> form, which takes a list of argument types and a return type. For instance, <code>Callable[[int],str]</code> represents a function that takes an <code>int</code> and returns a <code>str</code>.</p>
<p>In the example below, we have a <code>mapper</code> function that converts an <code>int</code> to a <code>str</code>. We also have a <code>map</code> function that takes a <code>mapper</code> function with a list of <code>int</code>s, and returns a list of <code>str</code>s. The <code>map</code> function applies the mapper to each element in the list, converting each <code>int</code> to a <code>str</code>:
</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Callable  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mapper</span>(<span class="hljs-params">number: int</span>) -&gt; str:</span>
    <span class="hljs-keyword">return</span> str(number)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">map</span>(<span class="hljs-params">
    mapper: Callable[[int], str],
    source: list[int],
</span>) -&gt; list[str]:</span>
    <span class="hljs-keyword">return</span> [mapper(x) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> source]


xs = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
ys = map(mapper, xs)
reveal_type(ys)  <span class="hljs-comment"># Type of "ys" is "list[str]</span>
</code></pre>
<h3 id="heading-lambda-functions">Lambda Functions</h3>
<p>We can also use lambdas to define the <code>mapper</code> function. Using lambdas
can make the code more compact since you don't need to define a separate
function, and naming such a function can sometimes be challenging.</p>
<p>However, it is not possible to annotate lambda parameters with type
hints. The type checker will have to infer the type of the lambda from
the context.</p>
<pre><code class="lang-python">zs = map(<span class="hljs-keyword">lambda</span> x: str(x), xs)
reveal_type(<span class="hljs-keyword">lambda</span> x: str(x))  <span class="hljs-comment"># Type of "lambda x: str(x)" is "(x: Unknown) -&gt; str" # Error: Argument type is unknown</span>
reveal_type(zs)  <span class="hljs-comment"># Type of "zs" is "list[str]"</span>
</code></pre>
<p>You can always try to help the type checker by providing a type
annotation for the variable. However, note that some linters will warn
against assigning lambda functions to a variable.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Warning: Do not assign a `lambda`expression, use a `def`</span>
mapper_: Callable[[int], str] = <span class="hljs-keyword">lambda</span> x: str(x)  <span class="hljs-comment"># noqa: E731</span>
</code></pre>
<h3 id="heading-callback-protocols">Callback Protocols</h3>
<p>One limitation with the <code>Callable</code> form is that it doesn't allow us to specify variadic arguments like <code>*args</code> or <code>**kwargs</code>. Therefore, we cannot specify optional parameters with default values.</p>
<p>In the example below, we have a function <code>callback</code> that takes two arguments, but we have no way of specifying that the second argument is optional. As a result, we get an error when we try to call the <code>cb</code> function with only one argument:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">callback</span>(<span class="hljs-params">a: int, b: int | None = None</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> a + (b <span class="hljs-keyword">or</span> <span class="hljs-number">20</span>)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_callback</span>(<span class="hljs-params">cb: Callable[[int, int | None], int]</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> cb(<span class="hljs-number">10</span>)  <span class="hljs-comment"># Error: Expected 1 more positional argument</span>


use_callback(callback)
</code></pre>
<p>To address these issues, we can use <em>callback protocols</em>. A callback protocol is a <code>Protocol</code> class that defines a <code>__call__</code> method. This method can have any number of positional or keyword parameters with or without default values. The type checker will check that the <code>__call__</code> method matches with the signature of the function that uses such a callback protocol.</p>
<p>In the example below, we define a <code>KeyboardEvent</code> protocol with a <code>__call__</code> method that takes a <code>keycode</code> parameter and an optional <code>completed</code> keyword parameter. The <code>on_event</code> function matches this signature. The <code>KeyboardEvent</code> protocol ensures that any function passed to the <code>register</code> function adheres to this signature:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Protocol  <span class="hljs-comment"># noqa: E402</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">KeyboardEvent</span>(<span class="hljs-params">Protocol</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span>(<span class="hljs-params">self, keycode: int, *, completed: bool = False</span>) -&gt; <span class="hljs-keyword">None</span>:</span> ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_event</span>(<span class="hljs-params">keycode: int, *, completed: bool = False</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    <span class="hljs-comment"># Handle the event</span>
    ...


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">register</span>(<span class="hljs-params">event_callback: KeyboardEvent</span>) -&gt; <span class="hljs-keyword">None</span>:</span>
    ...
    event_callback(keycode=<span class="hljs-number">10</span>)
    ...
    event_callback(keycode=<span class="hljs-number">10</span>, completed=<span class="hljs-literal">True</span>)


register(on_event)
</code></pre>
<hr />
<p>This concludes part 1 of our exploration into type annotations in Python. We've covered the basics of type annotations, type narrowing, structural sub-typing, and callables. By using these techniques, you can improve code readability, catch potential bugs early and ensure type safety in your Python projects.</p>
<p>In <a target="_blank" href="https://cardamomcode.dev/python-type-annotations-part-2">Part 2</a> of this series, we will build on these fundamentals and explore more advanced features such as generics, variadic generics, paramspec, and overloads.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><a target="_blank" href="https://www.python.org/dev/peps/pep-0483/">PEP-483 - The Theory of Type Hints</a></li>
<li><a target="_blank" href="https://www.python.org/dev/peps/pep-0484/">PEP 484 - Type Hints</a></li>
<li><a target="_blank" href="https://www.python.org/dev/peps/pep-0647/">PEP 647 -- User-Defined Type Guards</a></li>
</ul>
]]></content:encoded></item></channel></rss>