Tutorials

How to Type-Check Python Projects with ty 1.0 and uv

Astral has released ty 1.0, its first stable Python type checker. Learn how to integrate it with uv, detect typing errors, and gradually introduce static type checking into existing projects.

A Python function can contain an incorrect dictionary key, receive an unexpected argument, or return a value of the wrong type without producing an error when the application starts.

The problem may remain unnoticed until a particular execution path is reached.

Type annotations help describe what a function expects, but Python does not normally enforce those annotations at runtime. A separate tool is needed to examine the code before it executes.

On December 1, 2026, Astral released ty 1.0.0, the first stable version of its Python type checker and language server.

The same day, uv 0.13.11 was released with updated version-selection logic for its integrated uv check command, allowing it to automatically select ty 1.x.

For projects already using uv, this makes introducing static type checking relatively straightforward.

1. Create a Python project with ty

First, check your installed uv version:

uv --version

For the integrated type-checking command discussed later, use uv 0.13.11 or newer.

Create a small Python project:

uv init typing-demo
cd typing-demo

Install ty as a development dependency:

uv add --dev ty==1.0.0

Verify the installation:

uv run ty --version

The dependency declaration in pyproject.toml will include:

[dependency-groups]
dev = [
    "ty==1.0.0",
]

There is no need to include ty among the application’s production dependencies. It examines the source code during development and CI rather than participating in the application’s normal execution.

Keeping its version pinned also ensures that different developers use the same type-checking behavior.

2. Find an error before Python executes the code

Replace the contents of main.py with the following:

from typing import TypedDict


class Product(TypedDict):
    name: str
    price: float


def total(products: list[Product]) -> float:
    return sum(product["prcie"] for product in products)

There is a small mistake in the function.

The dictionary key is written as prcie instead of price.

Python accepts the function definition. Nothing inside it is evaluated merely because the function has been declared.

Add a call:

products: list[Product] = [
    {"name": "Keyboard", "price": 49.50},
    {"name": "Mouse", "price": 25.00},
]

print(total(products))

Running the application:

uv run python main.py

will produce:

KeyError: 'prcie'

The error appears when the function attempts to access the dictionary.

Now check the source code instead:

uv run ty check

ty can identify the invalid key through its invalid-typed-dict-key rule.

The checker already knows which keys belong to Product. It does not need to execute total() or receive actual product data to recognize the mistake.

Correct the function:

def total(products: list[Product]) -> float:
    return sum(product["price"] for product in products)

Run the type checker again.

This time, the invalid dictionary access is gone.

The difference becomes more significant in larger applications, where a function containing an incorrect key might only execute under unusual conditions that ordinary tests do not cover.

3. Check union types and generics

Dictionary keys are only one example.

Suppose a function receives either a string or None:

def normalize_name(name: str | None) -> str:
    return name.strip().lower()

The operation is valid for strings, but not for None.

The annotation explicitly allows both values, while the implementation only accounts for one.

A type checker can detect that inconsistency.

A corrected implementation might be:

def normalize_name(name: str | None) -> str:
    if name is None:
        return ""

    return name.strip().lower()

After the conditional check, ty can narrow the remaining type to str.

Generic collections expose similar problems:

def add_product_id(ids: list[int]) -> None:
    ids.append("42")

Python allows the string to be appended to the list at runtime. The resulting collection, however, no longer satisfies its declared element type.

ty can report the incompatible argument without running the function.

The stable release also brings improvements to generic type inference, return type inference, and the handling of third-party libraries.

Astral has introduced dedicated support for Pydantic, Django, attrs, and pytest. This is particularly useful for libraries whose behavior cannot always be represented through ordinary annotations alone.

It does not mean every dynamic Python pattern is now statically understood, or that a successful type check guarantees the absence of runtime errors.

4. Use uv check instead of calling ty directly

If you already use uv to manage the project, there is another way to perform the same type-checking task:

uv check --preview-features check-command

This command uses ty to analyze the project and resolves imports against the project’s Python environment.

The integration existed before today’s release.

What changed in uv 0.13.11 is how the type checker version is selected when the project does not specify one.

Previously, uv used the following default constraint:

>=0.0,<0.1

That prevented automatic selection of ty 1.0.

The new constraint is:

>=0,<2

uv can therefore select a compatible ty 1.x release without requiring the project to declare ty as a dependency.

The selection process follows this general logic:

flowchart TD
    A["uv check"] --> B{"Explicit ty version?"}
    B -->|Yes| C["Use requested version"]
    B -->|No| D{"ty locked as project dependency?"}
    D -->|Yes| E["Use locked version"]
    D -->|No| F["Resolve compatible ty >=0,<2"]
    C --> G["Type-check project"]
    E --> G
    F --> G

You can explicitly request version 1.0.0:

uv check --preview-features check-command --ty-version 1.0.0

Alternatively, keep using the development dependency installed earlier:

uv run ty check

Both approaches use the same underlying type checker.

However, uv check remains a preview feature, despite ty itself having reached a stable release.

For CI pipelines where predictable command behavior matters, invoking a pinned ty dependency directly is a reasonable approach.

5. Can ty fix errors automatically?

ty supports automatic corrections through:

uv run ty check --fix

When a diagnostic has an applicable safe fix, the checker can modify the source code.

But detecting an error and knowing how the developer intended to resolve it are different problems.

Consider the earlier misspelled dictionary key.

Replacing prcie with price appears obvious. Nevertheless, changing a dictionary access can change application behavior. Astral’s documentation illustrates why such corrections may be classified as unsafe rather than applied automatically.

The --fix option therefore should not be understood as a command that resolves every reported type error.

Review the modifications:

git diff

Then run the checker and application tests again.

6. Introduce type checking into an existing project

Adding ty to a new project is relatively simple.

An established application may be more difficult. Running a type checker for the first time can reveal hundreds of existing diagnostics.

Fixing all of them before enabling type checking in CI might require substantial work.

ty provides diagnostic baselines for this situation.

A baseline records existing diagnostics so subsequent checks can focus on violations that are not already recorded.

Add the following configuration to pyproject.toml:

[tool.ty]
baseline = "ty-baseline.json"

Generate the initial baseline:

uv run ty check --update-baseline

The resulting JSON file should be committed alongside the project configuration and lockfile.

Subsequent checks use it automatically:

uv run ty check

Existing matching diagnostics are suppressed. Newly introduced violations remain visible.

This makes gradual adoption possible without disabling entire categories of type checks.

There is, however, a mistake that would undermine this approach: automatically running --update-baseline before every CI check.

Doing so could record newly introduced errors before the checker has a chance to report them.

Baseline updates should instead be deliberate and reviewed, particularly when removing resolved violations or introducing stricter rules.

Diagnostic baselines are currently a preview feature. Their file format may change between ty releases.

7. Run ty in GitHub Actions

Once ty is installed as a development dependency, adding it to CI requires little configuration.

Create .github/workflows/type-check.yml:

name: Python Type Check

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  type-check:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: astral-sh/setup-uv@v6
        with:
          version: "0.13.11"

      - name: Install dependencies
        run: uv sync --locked

      - name: Run type checker
        run: uv run --locked ty check

Commit pyproject.toml and uv.lock to the repository.

If you use a diagnostic baseline, commit that file as well.

The --locked option ensures that CI does not silently update the project’s dependency resolution when the lockfile is outdated.

The type-checking command can then fail the workflow when it detects violations that require attention.

There is no need to use the preview uv check command here. Running ty directly already provides the necessary functionality.

What does the stable release mean for existing Python projects?

Astral’s transition to version 1.0 also introduces a versioning policy.

Patch releases are reserved for bug fixes, while minor releases may improve type-checking behavior in ways that expose additional errors.

This distinction is relevant to CI.

An application can remain unchanged while a newer type checker becomes capable of detecting a problem that an earlier version missed.

For that reason, a newly failing type check after a tool upgrade does not necessarily indicate that application code has changed.

Pinning the version and reviewing upgrades helps separate those two situations.

ty 1.0 is now recommended by Astral for production use, but adopting it does not require replacing every existing development tool.

Ruff can continue handling linting and formatting, pytest can execute tests, and ty can examine type consistency.

Each observes a different aspect of the same application.

The main advantage of introducing ty through uv is that the type checker becomes part of the project’s reproducible development environment.

A misspelled dictionary key is a small example. The underlying idea applies to much larger problems: errors that Python may only encounter during execution can sometimes be discovered long before that execution takes place.

External Sources used for this article

Related Articles

View all →