Improve DataFrame/Series static typing for mypy and Pyright - #2398
Open
cosmicbboy wants to merge 4 commits into
Open
cosmicbboy wants to merge 4 commits into
cosmicbboy wants to merge 4 commits into
Conversation
Extend the mypy plugin with column-level __getitem__ and attribute hooks, fix generic DataFrame typing, and add a codegen helper for Pyright/Pylance column inference. Addresses #1487. Co-authored-by: Cursor <cursoragent@cursor.com>
Reverting the runtime wrap in DataFrameModel.validate() to cast() so validate returns the original backend object (plain pandas, dask, etc.) while keeping the DataFrame[Self] return annotation for static typing. Co-authored-by: Cursor <cursoragent@cursor.com>
Remove GitHub issue number references from test modules, docstrings, and docs introduced on this branch: - Rename mypy/pyright test modules named after issue 1487 to describe what they cover (schema retention, check_types output, column access, typed wrapper). - Drop the issue link from the codegen module docstring and the docs seealso block (which also used invalid rST syntax in MyST markdown). - Remove redundant pandas_dataframe_getitem_hook_test.py fixture module, a subset of pandas_dataframe_getitem.py whose *_test.py suffix caused pytest to collect it as a test file. - Remove unused TYPE_CHECKING import and a placeholder-less f-string. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The generate-and-paste workflow of pandera.typing.codegen.generate_typed_dataframe_source made for janky developer experience. Remove the codegen module, its unit tests, the typed_wrapper pyright test module, and the docs section recommending it. The mypy plugin improvements (column __getitem__/attribute inference) and the generic DataFrame parameterization fixes are unchanged; the remaining pyright test modules cover the plugin-free behavior that still works under Pyright/Pylance. The docs now state plainly that column-level inference is a mypy-plugin feature. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
df["col"]anddf.colonDataFrame[Schema], including inherited fields and bare annotations likelabel: strDataFrame[Schema]typing by sharing theTypeVarfromcommon.py;validate()preserves the runtime dataframe type and is annotated asDataFrame[Self]for static analysisfollow_imports = silentrequirement and add mypy/pyright regression testsPyright/Pylance gets schema-level types (
DataFrame[Schema]retention throughvalidate()/check_types) via plugin-free generics; column-level inference is a mypy-plugin feature. Agenerate_typed_dataframe_source()codegen workaround for Pyright column inference was prototyped on this branch and removed as poor devex.Test plan
pytest tests/mypy(30 passed, 13 xfailed)pytest tests/pyright/test_pyright_static_type_checking.pyCloses #1487
Made with Cursor