pandas-contract 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ name: Python Application
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ lint-typing:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - name: Install uv
11
+ uses: astral-sh/setup-uv@v5
12
+ - name: Install project dependencies
13
+ run: uv sync
14
+ - name: 'Type check'
15
+ run: uv run pyright src
16
+ lint:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@v4
20
+ - uses: astral-sh/ruff-action@v3
21
+ with:
22
+ src: "./src"
23
+ args: "check"
24
+ - name: 'Ruff format check'
25
+ run: ruff format --check
26
+ - name: 'Ruff check'
27
+ run: ruff check
28
+ test-old-version:
29
+ runs-on: ubuntu-latest
30
+ steps:
31
+ - uses: actions/checkout@v4
32
+ - name: Set up Python 3.10
33
+ uses: actions/setup-python@v5
34
+ with:
35
+ python-version: "3.10"
36
+ - name: Install uv
37
+ uses: astral-sh/setup-uv@v5
38
+ - name: Install dependencies
39
+ run: uv sync
40
+ - name: Run pytest
41
+ run: |
42
+ # Keep --with in sync pyproject.toml
43
+ # python 3.10 is the latest with pre-compiled pandas 1.4.0 releases.
44
+ uv run --python 3.10 --with pandas==1.4.0 --with pandera==0.10.0 pytest src/tests
45
+ test:
46
+ name: Test Python 🐍 application
47
+ strategy:
48
+ matrix:
49
+ os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
50
+ python-version: ["3.12"]
51
+ # Add all python versions for ubuntu.
52
+ include:
53
+ - os: "ubuntu-latest"
54
+ python-version: "3.9"
55
+ - os: "ubuntu-latest"
56
+ python-version: "3.10"
57
+ - os: "ubuntu-latest"
58
+ python-version: "3.11"
59
+ runs-on: ${{ matrix.os }}
60
+ steps:
61
+ - uses: actions/checkout@v4
62
+ - name: Set up Python ${{ matrix.python-version }}
63
+ uses: actions/setup-python@v5
64
+ with:
65
+ python-version: ${{ matrix.python-version }}
66
+ - name: Install uv
67
+ uses: astral-sh/setup-uv@v5
68
+ - name: Install dependencies
69
+ run: uv sync
70
+ - name: Run pytest
71
+ run: |
72
+ echo ${{ github.ref }}
73
+ uv run --python ${{ matrix.python-version }} pytest src/tests
74
+
75
+ build:
76
+ name: Build package 📦
77
+ runs-on: ubuntu-latest
78
+ steps:
79
+ - uses: actions/checkout@v4
80
+ - name: Set up Python 3.12
81
+ uses: actions/setup-python@v5
82
+ with:
83
+ python-version: 3.12
84
+ - name: Install uv
85
+ uses: astral-sh/setup-uv@v5
86
+ - name: Build package
87
+ run: uv build
88
+ - name: Upload dist-build
89
+ uses: actions/upload-artifact@v4
90
+ with:
91
+ name: python-package-distributions
92
+ path: dist/
93
+
94
+ publish-to-pypi:
95
+ name: >-
96
+ Publish Python 🐍 distribution 📦 to PyPI
97
+ needs:
98
+ - build
99
+ - lint
100
+ - test
101
+ if: success() && startsWith(github.ref, 'refs/tags/v')
102
+ runs-on: ubuntu-latest
103
+ environment:
104
+ name: pypi
105
+ url: https://pypi.org/p/pandas-contract
106
+ permissions:
107
+ id-token: write # IMPORTANT: mandatory for trusted publishing
108
+
109
+ steps:
110
+ - name: Download all the dists
111
+ uses: actions/download-artifact@v4
112
+ with:
113
+ name: python-package-distributions
114
+ path: dist/
115
+ - name: Publish distribution 📦 to PyPI
116
+ uses: pypa/gh-action-pypi-publish@release/v1
117
+
118
+ github-release:
119
+ name: >-
120
+ Sign the Python 🐍 distribution 📦 with Sigstore
121
+ and upload them to GitHub Release
122
+ needs:
123
+ - publish-to-pypi
124
+ runs-on: ubuntu-latest
125
+
126
+ permissions:
127
+ contents: write # IMPORTANT: mandatory for making GitHub Releases
128
+ id-token: write # IMPORTANT: mandatory for sigstore
129
+
130
+ steps:
131
+ - name: Download all the dists
132
+ uses: actions/download-artifact@v4
133
+ with:
134
+ name: python-package-distributions
135
+ path: dist/
136
+ - name: Sign the dists with Sigstore
137
+ uses: sigstore/gh-action-sigstore-python@v3.0.0
138
+ with:
139
+ inputs: >-
140
+ ./dist/*.tar.gz
141
+ ./dist/*.whl
142
+ - name: Create GitHub Release
143
+ env:
144
+ GITHUB_TOKEN: ${{ github.token }}
145
+ run: >-
146
+ gh release create
147
+ '${{ github.ref_name }}'
148
+ --repo '${{ github.repository }}'
149
+ --notes ""
150
+ - name: Upload artifact signatures to GitHub Release
151
+ env:
152
+ GITHUB_TOKEN: ${{ github.token }}
153
+ # Upload to GitHub Release using the `gh` CLI.
154
+ # `dist/` contains the built packages, and the
155
+ # sigstore-produced signatures and certificates.
156
+ run: >-
157
+ gh release upload
158
+ '${{ github.ref_name }}' dist/**
159
+ --repo '${{ github.repository }}'
160
+
@@ -0,0 +1,3 @@
1
+ .idea/**
2
+ **/__pycache__/**
3
+ **/.pyc
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] - 2025-03-31
8
+ _Initial release_
9
+ ### Added
10
+ - Add module `pandas_contract`
11
+ - Add decorators `argument` and `result`
12
+ - Add helper method `from_arg`
13
+ - Add helper handlers `set_mode` and context generators `as_mode`, `raises`, `silent`
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Micha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ format:
2
+ uv run ruff format
3
+ test: test-src lint
4
+ test-src:
5
+ uv run pytest src/tests
6
+
7
+ lint: lint-uv lint-type
8
+ lint-uv:
9
+ uv run ruff check
10
+ uv run ruff format --check
11
+
12
+ lint-type:
13
+ uv run pyright src/
@@ -0,0 +1,108 @@
1
+ Metadata-Version: 2.4
2
+ Name: pandas-contract
3
+ Version: 0.1.0
4
+ Summary: Define input and output columns for functions working on pandas dataframes.
5
+ Project-URL: homepage, https://github.com/schollm/check-dependencies
6
+ Project-URL: issues, https://github.com/schollm/pandas-contract/projects
7
+ Project-URL: source, https://github.com/schollm/check-dependencies
8
+ Project-URL: changelog, https://github.com/schollm/check-dependencies/CHANGELOG.md
9
+ Author-email: Micha Scholl <schollm-git@gmx.com>
10
+ License-File: LICENSE
11
+ Keywords: contract,dependencies,pandas,pandera,validation
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: Implementation :: CPython
21
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.9
24
+ Requires-Dist: pandas>=1.4.0
25
+ Requires-Dist: pandera>=0.10.0
26
+ Description-Content-Type: text/markdown
27
+
28
+ # pandas-contract
29
+ Provide decorators to check functions arguments and return values using pandas DataFrame.
30
+
31
+ The decorators utilize the [pandera.io](https://pandera.readthedocs.io/) library to validate
32
+ data types and constraints of the input arguments and output values of functions.
33
+
34
+
35
+ ## Installation
36
+ ```bash
37
+ pip install pandas-contract
38
+ ```
39
+
40
+ ## Usage
41
+ The library provides decorators to check the input arguments and return values of functions.
42
+
43
+ The following defines a function that takes a DataFrame with a column x of type integer as
44
+ input and returns a DataFrame with the column x of type string as output.
45
+ ```python
46
+ import pandas as pd
47
+ import pandas_contract as pc
48
+ import pandera as pa
49
+
50
+ @pc.argument("df", schema=pa.DataFrameSchema({"x": pa.Int}))
51
+ @pc.result(schema=pa.DataFrameSchema({"x": pa.String}))
52
+ def col_x_to_string(df: pd.DataFrame) -> pd.DataFrame:
53
+ return df.assign(x=df["x"].astype(str))
54
+ ```
55
+ ### Dynamic Arguments and return values
56
+ Required columns and arguments can also be specified dynamically using a function that returns a schema.
57
+ ```python
58
+ import pandas as pd
59
+ import pandas_contract as pc
60
+ import pandera as pa
61
+
62
+ @pc.argument("df", schema=pa.DataFrameSchema({pc.from_arg("val_col"): pa.Int}))
63
+ @pc.result(schema=pa.DataFrameSchema({pc.from_arg("val_col"): pa.String}))
64
+ def col_to_string(df: pd.DataFrame, val_col: str) -> pd.DataFrame:
65
+ return df.assign(**{val_col: df[val_col].astype(str)})
66
+ ```
67
+
68
+ ### Cross-argument and output constraints
69
+ Additionally, it provides checks to ensure cross-argument and output constraints like
70
+ #### Dataframes should have the same index.
71
+ ```python
72
+ import pandas as pd
73
+ import pandas_contract as pc
74
+
75
+ @pc.result(same_index_as=["df1", "df2"])
76
+ def my_func(df1: pd.DataFrame, df2: pd.DataFrame):
77
+ # Output has the same index as input
78
+ return pd.DataFrame(index=df1.index)
79
+
80
+ @pc.argument("df1", same_index_as="df2")
81
+ def my_func(df1, df2):
82
+ # Input dataframes have the same index
83
+ df1["x"] = df2["x"]
84
+ ```
85
+
86
+ #### Size of dataframes should be equal.
87
+ ```python
88
+ import pandas as pd
89
+ import pandas_contract as pc
90
+
91
+ @pc.result(same_size_as="df1")
92
+ def my_func(df1: pd.DataFrame):
93
+ # Output has the same size as input
94
+ return pd.DataFrame(index=df1.index + 1)
95
+ ```
96
+
97
+ Additionally, it allows to extract a dataframe from a more complex argument or return value by specifying a key.
98
+
99
+ ```python
100
+ import pandas as pd
101
+ import pandas_contract as pc
102
+ import pandera as pa
103
+
104
+ @pc.argument(arg="df", schema=pa.DataFrameSchema({"x": pa.Int}))
105
+ @pc.result(key="data", schema=pa.DataFrameSchema({"x": pa.Int}), same_index_as="df")
106
+ def into_dict(df: pd.DataFrame) -> dict[str, pd.DataFrame]:
107
+ return dict(data=df)
108
+ ```
@@ -0,0 +1,81 @@
1
+ # pandas-contract
2
+ Provide decorators to check functions arguments and return values using pandas DataFrame.
3
+
4
+ The decorators utilize the [pandera.io](https://pandera.readthedocs.io/) library to validate
5
+ data types and constraints of the input arguments and output values of functions.
6
+
7
+
8
+ ## Installation
9
+ ```bash
10
+ pip install pandas-contract
11
+ ```
12
+
13
+ ## Usage
14
+ The library provides decorators to check the input arguments and return values of functions.
15
+
16
+ The following defines a function that takes a DataFrame with a column x of type integer as
17
+ input and returns a DataFrame with the column x of type string as output.
18
+ ```python
19
+ import pandas as pd
20
+ import pandas_contract as pc
21
+ import pandera as pa
22
+
23
+ @pc.argument("df", schema=pa.DataFrameSchema({"x": pa.Int}))
24
+ @pc.result(schema=pa.DataFrameSchema({"x": pa.String}))
25
+ def col_x_to_string(df: pd.DataFrame) -> pd.DataFrame:
26
+ return df.assign(x=df["x"].astype(str))
27
+ ```
28
+ ### Dynamic Arguments and return values
29
+ Required columns and arguments can also be specified dynamically using a function that returns a schema.
30
+ ```python
31
+ import pandas as pd
32
+ import pandas_contract as pc
33
+ import pandera as pa
34
+
35
+ @pc.argument("df", schema=pa.DataFrameSchema({pc.from_arg("val_col"): pa.Int}))
36
+ @pc.result(schema=pa.DataFrameSchema({pc.from_arg("val_col"): pa.String}))
37
+ def col_to_string(df: pd.DataFrame, val_col: str) -> pd.DataFrame:
38
+ return df.assign(**{val_col: df[val_col].astype(str)})
39
+ ```
40
+
41
+ ### Cross-argument and output constraints
42
+ Additionally, it provides checks to ensure cross-argument and output constraints like
43
+ #### Dataframes should have the same index.
44
+ ```python
45
+ import pandas as pd
46
+ import pandas_contract as pc
47
+
48
+ @pc.result(same_index_as=["df1", "df2"])
49
+ def my_func(df1: pd.DataFrame, df2: pd.DataFrame):
50
+ # Output has the same index as input
51
+ return pd.DataFrame(index=df1.index)
52
+
53
+ @pc.argument("df1", same_index_as="df2")
54
+ def my_func(df1, df2):
55
+ # Input dataframes have the same index
56
+ df1["x"] = df2["x"]
57
+ ```
58
+
59
+ #### Size of dataframes should be equal.
60
+ ```python
61
+ import pandas as pd
62
+ import pandas_contract as pc
63
+
64
+ @pc.result(same_size_as="df1")
65
+ def my_func(df1: pd.DataFrame):
66
+ # Output has the same size as input
67
+ return pd.DataFrame(index=df1.index + 1)
68
+ ```
69
+
70
+ Additionally, it allows to extract a dataframe from a more complex argument or return value by specifying a key.
71
+
72
+ ```python
73
+ import pandas as pd
74
+ import pandas_contract as pc
75
+ import pandera as pa
76
+
77
+ @pc.argument(arg="df", schema=pa.DataFrameSchema({"x": pa.Int}))
78
+ @pc.result(key="data", schema=pa.DataFrameSchema({"x": pa.Int}), same_index_as="df")
79
+ def into_dict(df: pd.DataFrame) -> dict[str, pd.DataFrame]:
80
+ return dict(data=df)
81
+ ```
@@ -0,0 +1,76 @@
1
+ [project]
2
+ name = "pandas-contract"
3
+ version = "0.1.0"
4
+ description = "Define input and output columns for functions working on pandas dataframes."
5
+ readme = "README.md"
6
+ authors = [
7
+ {name = "Micha Scholl", email = "schollm-git@gmx.com"},
8
+ ]
9
+ keywords = ["pandas", "pandera", "dependencies", "contract", "validation"]
10
+ classifiers = [
11
+ "Development Status :: 4 - Beta",
12
+ "Intended Audience :: Developers",
13
+ "License :: OSI Approved :: MIT License",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3.9",
16
+ "Programming Language :: Python :: 3.10",
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: Implementation :: CPython",
20
+ "Programming Language :: Python :: Implementation :: PyPy",
21
+ "Typing :: Typed"
22
+ ]
23
+
24
+
25
+ requires-python = ">=3.9"
26
+ dependencies = [
27
+ # Keep in sync with .github/workflows/python.yml
28
+ "pandas>=1.4.0",
29
+ "pandera>=0.10.0",
30
+ ]
31
+ [project.urls]
32
+ homepage= "https://github.com/schollm/check-dependencies"
33
+ issues = "https://github.com/schollm/pandas-contract/projects"
34
+ source = "https://github.com/schollm/check-dependencies"
35
+ changelog = "https://github.com/schollm/check-dependencies/CHANGELOG.md"
36
+
37
+ [tool.uv]
38
+ dev-dependencies = [
39
+ "pytest>=8.3.5",
40
+ "ruff>=0.11.0",
41
+ "pandas-stubs>=2.2.2.240807",
42
+ "pytest-mypy>=1.0.0",
43
+ "pyright>=1.1.398",
44
+ ]
45
+
46
+ [tool.ruff]
47
+ line-length = 88
48
+
49
+ [tool.ruff.lint]
50
+ select = ["ALL"]
51
+ ignore = [
52
+ "D213", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible.
53
+ "D203", # `incorrect-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible.
54
+ "COM812", # Missing trailing comma in a single-line list - already applied by ruff format.
55
+ "PD901", # Avoid using generic name 'df' for DataFrame
56
+ "TRY003", # Checks for long exception messages that are not defined in the exception class itself
57
+ "EM101", # Exception must not use a string literal, assign to variable first
58
+ "ANN401", # Dynamically typed expressions (typing.Any) are disallowed: This is also applied to *arg, **kwargs.
59
+ "D205", # 1 blank line required between summary line and description: ruff format disagrees.
60
+ ]
61
+
62
+ [tool.ruff.lint.flake8-pytest-style]
63
+ parametrize-names-type = "csv"
64
+
65
+ [tool.ruff.lint.per-file-ignores]
66
+ "src/tests/**/*.py" = [
67
+ # at least this three should be fine in tests:
68
+ "S101", # asserts allowed in tests...
69
+ "ARG", # Unused function args -> fixtures nevertheless are functionally relevant...
70
+ "FBT", # Don't care about booleans as positional arguments in tests, e.g. via @pytest.mark.parametrize()
71
+ ]
72
+
73
+ [build-system]
74
+ requires = ["hatchling"]
75
+ build-backend = "hatchling.build"
76
+
@@ -0,0 +1,7 @@
1
+ """pandas_contract: Ensure pandas dataframe fit the expectations of the function."""
2
+
3
+ from ._decorator import argument, result
4
+ from ._lib import from_arg
5
+ from .mode import as_mode, raises, set_mode, silent
6
+
7
+ __all__ = ["argument", "as_mode", "from_arg", "raises", "result", "set_mode", "silent"]