Mypy plugin that resolves Pandera DataFrameModel column attributes as str
Project description
pandera-mypy
A mypy plugin that resolves Pandera
DataFrameModelcolumn attributes asstr, eliminating false type errors when passing column names topolars.col()or any otherstr-expecting API.
The Problem
Pandera's DataFrameModel supports a convenient bare-type shorthand for
column annotations, where a plain Python type is used instead of the full
Series[T] form:
import pandera.polars as pa
class MySchema(pa.DataFrameModel):
quantity: int # shorthand for Series[int]
label: str # shorthand for Series[str]
At runtime, accessing a column attribute always returns its name as a
plain str:
>>> MySchema.quantity
'quantity'
>>> MySchema.label
'label'
Without the plugin, mypy infers the declared type (int, str, …) rather
than str, causing spurious errors when column names are passed to
polars.col() or any other str-expecting API:
import polars as pl
pl.col(MySchema.quantity)
# ^ error: Argument 1 to "col" has incompatible type "int"; expected "str"
Series[T]annotations: Pandera's bundled stubs already type class-level attribute access asstrforSeries[T]-annotated columns across all supported versions (0.21.1-0.31.1). The plugin ensures uniformstrinference when schemas mixSeries[T]and bare-type annotations.
The Solution
This plugin overrides the inferred type of every column-annotated attribute on
a DataFrameModel subclass to str, matching the actual runtime behaviour —
no changes to your Pandera models required.
# With the plugin enabled:
pl.col(MySchema.quantity) # ✅ no error
col_name: str = MySchema.quantity # ✅ no error
Installation
pip install pandera-mypy
# or with uv:
uv add pandera-mypy
Configuration
Add the plugin to your mypy configuration and that's it:
mypy.ini
[mypy]
plugins = pandera_mypy.plugin
pyproject.toml
[tool.mypy]
plugins = ["pandera_mypy.plugin"]
setup.cfg
[mypy]
plugins = pandera_mypy.plugin
Compatibility
| Python | Supported |
|---|---|
| 3.10 | ✅ |
| 3.11 | ✅ |
| 3.12 | ✅ |
| 3.13 | ✅ |
| 3.14 | ✅ |
Requires mypy >= 1.0.0 and works with both the Polars and Pandas backends of Pandera.
How It Works
The plugin implements mypy's get_class_attribute_hook (for MySchema.price style class-level access) and get_attribute_hook (for instance-level access).
For every attribute access, it checks whether:
- The owning class inherits from a Pandera
DataFrameModel(detected via the MRO, so indirect subclasses work too). - The attribute is annotated with a Pandera column type — either
Series[T]or a bare type such asint.
When both conditions hold, the plugin replaces the inferred type with builtins.str.
Why
get_class_attribute_hook?
Mypy routes class-level attribute access (e.g.MySchema.price) throughget_class_attribute_hookinmypy.checkmember, not throughget_attribute_hook. Using onlyget_attribute_hookwould leave the commonpl.col(MySchema.price)pattern uncorrected.
Development
# Install all dependencies including the dev group
uv sync --group dev
# Run the test suite with coverage
uv run pytest
# Type-check the plugin
uv run mypy src
# Lint (report only)
uv run ruff check .
# Lint and auto-fix
uv run ruff check --fix .
# Format
uv run ruff format .
# Check formatting without changing files
uv run ruff format --check .
# Verify the lockfile is up to date with pyproject.toml
uv lock --check
# Build the wheel and sdist
uv build
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pandera_mypy-0.0.1.tar.gz.
File metadata
- Download URL: pandera_mypy-0.0.1.tar.gz
- Upload date:
- Size: 62.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c0f0b973327ad22c5f17489e65f1f1ccd22e048f95b910a473464029c23c6ab
|
|
| MD5 |
a3821d019b4ae35725ec25eff5b175e1
|
|
| BLAKE2b-256 |
623529f409cbeb0d8d58504324ad9d9a4d1a8554855a8fdd91a3904e9b45723a
|
Provenance
The following attestation bundles were made for pandera_mypy-0.0.1.tar.gz:
Publisher:
publish.yml on TymoteuszChatys/pandera-mypy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pandera_mypy-0.0.1.tar.gz -
Subject digest:
6c0f0b973327ad22c5f17489e65f1f1ccd22e048f95b910a473464029c23c6ab - Sigstore transparency entry: 1489406012
- Sigstore integration time:
-
Permalink:
TymoteuszChatys/pandera-mypy@a0decf9fc619ed3f728a3bbdb956231cd810c989 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/TymoteuszChatys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0decf9fc619ed3f728a3bbdb956231cd810c989 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pandera_mypy-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pandera_mypy-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c456c917b3f0ec56fa9e0e3d603996876fd74528d7f6f87b2a912325182eaca5
|
|
| MD5 |
8580323d2ec292431c152cbb5b8b6ac7
|
|
| BLAKE2b-256 |
16ef3ff6dd66e27216a63c3f65e0ffb0730e8ac74712db678c9d4e93328b4ce9
|
Provenance
The following attestation bundles were made for pandera_mypy-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on TymoteuszChatys/pandera-mypy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pandera_mypy-0.0.1-py3-none-any.whl -
Subject digest:
c456c917b3f0ec56fa9e0e3d603996876fd74528d7f6f87b2a912325182eaca5 - Sigstore transparency entry: 1489406324
- Sigstore integration time:
-
Permalink:
TymoteuszChatys/pandera-mypy@a0decf9fc619ed3f728a3bbdb956231cd810c989 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/TymoteuszChatys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0decf9fc619ed3f728a3bbdb956231cd810c989 -
Trigger Event:
push
-
Statement type: