Skip to content

Commit 71a8a08

Browse files
Bring back pydantic 2 support (#11756) (#11757)
* Loosen pydantic maximum to <3 (allowing for pydantic 2) * Add an internal pydantic shim for getting pydantic BaseSettings reguardless of pydantic v1 vs v2 * Add changie doc (cherry picked from commit 70ad931) Co-authored-by: Quigley Malcolm <[email protected]>
1 parent 17806c6 commit 71a8a08

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Dependencies
2+
body: Allow for either pydantic v1 and v2
3+
time: 2025-06-20T12:36:00.196384-05:00
4+
custom:
5+
Author: QMalcolm
6+
Issue: "11634"

core/dbt/_pydantic_shim.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# type: ignore
2+
3+
"""Shim to allow support for both Pydantic 1 and Pydantic 2.
4+
5+
dbt-core must support both major versions of Pydantic because dbt-core users might be using an environment with
6+
either version, and we can't restrict them to one or the other. Here, we essentially import all Pydantic objects
7+
from version 1 that we use. Throughout the repo, we import these objects from this file instead of from Pydantic
8+
directly, meaning that we essentially only use Pydantic 1 in dbt-core currently, but without forcing that restriction
9+
on dbt users. The development environment for this repo should be pinned to Pydantic 1 to ensure devs get appropriate
10+
type hints.
11+
"""
12+
13+
from importlib.metadata import version
14+
15+
pydantic_version = version("pydantic")
16+
# Pydantic uses semantic versioning, i.e. <major>.<minor>.<patch>, and we need to know the major
17+
pydantic_major = pydantic_version.split(".")[0]
18+
19+
if pydantic_major == "1":
20+
from pydantic import BaseSettings # noqa: F401
21+
elif pydantic_major == "2":
22+
from pydantic.v1 import BaseSettings # noqa: F401
23+
else:
24+
raise RuntimeError(
25+
f"Currently only pydantic 1 and 2 are supported, found pydantic {pydantic_version}"
26+
)

core/dbt/utils/artifact_upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import zipfile
44

55
import requests
6-
from pydantic import BaseSettings
76

87
import dbt.tracking
8+
from dbt._pydantic_shim import BaseSettings # type: ignore
99
from dbt.config.runtime import UnsetProfile, load_project
1010
from dbt.constants import MANIFEST_FILE_NAME, RUN_RESULTS_FILE_NAME
1111
from dbt.events.types import ArtifactUploadSkipped, ArtifactUploadSuccess

core/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
"dbt-common>=1.22.0,<2.0",
7676
"dbt-adapters>=1.15.2,<2.0",
7777
"dbt-protos>=1.0.315,<2.0",
78+
"pydantic<3",
7879
# ----
7980
# Expect compatibility with all new versions of these packages, so lower bounds only.
8081
"packaging>20.9",
8182
"pytz>=2015.7",
8283
"pyyaml>=6.0",
8384
"daff>=1.3.46",
8485
"typing-extensions>=4.4",
85-
"pydantic<2",
8686
# ----
8787
],
8888
zip_safe=False,

0 commit comments

Comments
 (0)