Skip to content

Commit 61e979d

Browse files
authored
Enhancement: Pydantic v2 Package Shim Compatibility (#55)
* Implements `compatibility` subpackage for easier version compatibility maintenance * Adds Pydantic v1 / Pydantic v2 compatibility via shim * Adds Pydantic v1 / Pydantic v2 to testing CI matrix * Updates examples and adds compatibility notice
1 parent a52b494 commit 61e979d

31 files changed

+86
-80
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
matrix:
1818
os: [ "ubuntu-latest", "macos-13", "windows-latest" ]
1919
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
20+
pydantic-version: [ "1.0", "2.0" ]
2021
fail-fast: false
2122
defaults:
2223
run:
@@ -34,9 +35,11 @@ jobs:
3435
uses: actions/cache@v4
3536
with:
3637
path: .venv
37-
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
38+
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.pydantic-version }}-${{ hashFiles('pyproject.toml') }}
3839
- name: Install Hatch
3940
run: pip install git+https://github.com/pypa/hatch.git@master
41+
- name: Install Pydantic
42+
run: hatch run uv pip install pydantic~=${{ matrix.pydantic-version }}
4043
- name: Run Build Hooks
4144
run: hatch build --hooks-only
4245
- name: Run Tests
@@ -45,7 +48,7 @@ jobs:
4548
uses: coverallsapp/github-action@v2
4649
with:
4750
github-token: ${{ secrets.GITHUB_TOKEN }}
48-
flag-name: ${{ matrix.os }}-${{ matrix.python-version }}
51+
flag-name: ${{ matrix.os }}-${{ matrix.python-version }}-${{ matrix.pydantic-version }}
4952
parallel: true
5053

5154
coverage:

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
## Help
3939
See [documentation](https://pydantic-argparse.supimdos.com) for help.
4040

41+
## Requirements
42+
Requires Python 3.8+, and is compatible with the Pydantic v1 API.
43+
4144
## Installation
4245
Installation with `pip` is simple:
4346
```console
@@ -46,7 +49,7 @@ $ pip install pydantic-argparse
4649

4750
## Example
4851
```py
49-
import pydantic
52+
import pydantic.v1 as pydantic
5053
import pydantic_argparse
5154

5255

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
provides declarative *typed* argument parsing using `pydantic` models.
4242

4343
## Requirements
44-
`pydantic-argparse` requires Python 3.8+
44+
`pydantic-argparse` requires Python 3.8+, and is compatible with the Pydantic
45+
v1 API.
4546

4647
## Installation
4748
Installation with `pip` is simple:

docs/showcase.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The `pydantic-argparse` command-line interface construction is simple.
77

88
=== "Pydantic Argparse"
99
```python
10-
import pydantic
10+
import pydantic.v1 as pydantic
1111
import pydantic_argparse
1212

1313
# Declare Arguments

examples/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
# Third-Party
5-
import pydantic
5+
import pydantic.v1 as pydantic
66
import pydantic_argparse
77

88
# Typing

examples/simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
# Third-Party
5-
import pydantic
5+
import pydantic.v1 as pydantic
66
import pydantic_argparse
77

88

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ authors = [{ name = "Hayden Richards", email = "[email protected]" }]
99
readme = "README.md"
1010
license = { file = "LICENSE" }
1111
requires-python = ">=3.8"
12-
dependencies = ["pydantic<2"]
12+
dependencies = ["pydantic"]
1313
classifiers = [
1414
"Development Status :: 4 - Beta",
1515
'Programming Language :: Python',
@@ -102,7 +102,7 @@ warn_return_any = true
102102
warn_unused_ignores = true
103103
no_implicit_optional = true
104104
show_error_codes = true
105-
plugins = ["pydantic.mypy"]
105+
plugins = ["pydantic.v1.mypy"]
106106

107107
[tool.pydantic-mypy]
108108
init_forbid_extra = true

src/pydantic_argparse/argparse/actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"""
88

99

10-
# Standard
11-
import argparse
10+
# Local
11+
from pydantic_argparse.compatibility import argparse
1212

1313
# Typing
1414
from typing import Any, Callable, Iterable, List, Optional, Sequence, Tuple, TypeVar, Union, cast

src/pydantic_argparse/argparse/parser.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717

1818
# Standard
19-
import argparse
2019
import sys
2120

22-
# Third-Party
23-
import pydantic
24-
2521
# Local
2622
from pydantic_argparse import parsers
2723
from pydantic_argparse import utils
2824
from pydantic_argparse.argparse import actions
29-
from pydantic_argparse.argparse import patches # noqa: F401
25+
from pydantic_argparse.compatibility import argparse
26+
from pydantic_argparse.compatibility import pydantic
3027

3128
# Typing
3229
from typing import Any, Dict, Generic, List, NoReturn, Optional, Type, TypeVar
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Compatibiltity Shims for Declarative Typed Argument Parsing.
2+
3+
This package contains compatibility shims for the `pydantic` and `argparse`
4+
modules, so that we can properly maintain version compatibility in one place.
5+
6+
The public interface exposed by this package is the module shims themselves.
7+
"""
8+
9+
# Local
10+
from pydantic_argparse.compatibility.argparse import argparse as argparse
11+
from pydantic_argparse.compatibility.pydantic import pydantic as pydantic

0 commit comments

Comments
 (0)