1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
From: Talley Lambert <talley.lambert@gmail.com>
Date: Sat, 6 Sep 2025 16:00:59 -0400
Subject: build: drop pydantic 1 (#263)
* drop pydantic 1
* relax pydantic pin
* remove more
Origin: upstream, https://github.com/pyapp-kit/app-model/pull/263
Bug: https://github.com/pyapp-kit/app-model/issues/235
Last-Update: 2025-09-08
---
.pre-commit-config.yaml | 3 +--
pyproject.toml | 7 ++-----
src/app_model/expressions/_expressions.py | 6 ------
src/app_model/types/_action.py | 2 +-
src/app_model/types/_base.py | 2 +-
src/app_model/types/_command_rule.py | 2 +-
src/app_model/types/_icon.py | 9 ++------
src/app_model/types/_keybinding_rule.py | 34 ++++++++-----------------------
src/app_model/types/_keys/_key_codes.py | 3 ---
src/app_model/types/_keys/_keybindings.py | 11 +++-------
src/app_model/types/_menu_rule.py | 17 ++--------------
tests/test_keybindings.py | 11 ++--------
12 files changed, 24 insertions(+), 83 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index c95a2b2..998900a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -28,8 +28,7 @@ repos:
- id: mypy
files: "^src/"
additional_dependencies:
- - pydantic >2
- - pydantic-compat
+ - pydantic >2.8
- in-n-out
- repo: local
diff --git a/pyproject.toml b/pyproject.toml
index e0c95c6..c5e34ec 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -38,8 +38,7 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"psygnal>=0.10",
- "pydantic>=1.10.18",
- "pydantic-compat>=0.1.1",
+ "pydantic>=2.8",
"in-n-out>=0.1.5",
"typing_extensions>=4.12",
]
@@ -53,7 +52,7 @@ qt = ["qtpy>=2.4.0", "superqt[iconify]>=0.7.2"]
pyqt5 = [
"app-model[qt]",
"PyQt5>=5.15.10",
- "pyqt5-qt5<=5.15.2; sys_platform == 'win32'",
+ "pyqt5-qt5==5.15.2; sys_platform == 'win32'",
"pyqt5-qt5>=5.15.4; sys_platform != 'win32'",
]
pyqt6 = ["app-model[qt]", "PyQt6>=6.4.0"]
@@ -153,8 +152,6 @@ filterwarnings = [
"error",
"ignore:Enum value:DeprecationWarning:superqt",
"ignore:Failed to disconnect::pytestqt",
- "ignore:Failing to pass a value to the 'type_params' parameter::pydantic",
- "ignore:`__get_validators__` is deprecated and will be removed",
]
markers = [
"needs_network: This test requires network access",
diff --git a/src/app_model/expressions/_expressions.py b/src/app_model/expressions/_expressions.py
index a285659..2e7b180 100644
--- a/src/app_model/expressions/_expressions.py
+++ b/src/app_model/expressions/_expressions.py
@@ -6,7 +6,6 @@ import ast
from typing import (
TYPE_CHECKING,
Any,
- Callable,
Generic,
SupportsIndex,
TypeVar,
@@ -342,11 +341,6 @@ class Expr(ast.AST, Generic[T]):
rv[1] = tuple(getattr(self, f) for f in self._fields)
return tuple(rv)
- @classmethod
- def __get_validators__(cls) -> Iterator[Callable[[Any], Expr]]:
- """Pydantic validators for this class."""
- yield cls._validate
-
@classmethod
def __get_pydantic_core_schema__(
cls, source: type, handler: GetCoreSchemaHandler
diff --git a/src/app_model/types/_action.py b/src/app_model/types/_action.py
index 669e099..f39a742 100644
--- a/src/app_model/types/_action.py
+++ b/src/app_model/types/_action.py
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Callable, Generic, Optional, TypeVar, Union
-from pydantic_compat import Field, field_validator
+from pydantic import Field, field_validator
from ._command_rule import CommandRule
from ._keybinding_rule import KeyBindingRule
diff --git a/src/app_model/types/_base.py b/src/app_model/types/_base.py
index e17aec8..2776ef5 100644
--- a/src/app_model/types/_base.py
+++ b/src/app_model/types/_base.py
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, ClassVar
-from pydantic_compat import BaseModel
+from pydantic import BaseModel
if TYPE_CHECKING:
from pydantic import ConfigDict
diff --git a/src/app_model/types/_command_rule.py b/src/app_model/types/_command_rule.py
index df06d3e..115c6cc 100644
--- a/src/app_model/types/_command_rule.py
+++ b/src/app_model/types/_command_rule.py
@@ -1,6 +1,6 @@
from typing import Callable, Optional, Union
-from pydantic_compat import Field
+from pydantic import Field
from app_model import expressions
diff --git a/src/app_model/types/_icon.py b/src/app_model/types/_icon.py
index a416728..a076488 100644
--- a/src/app_model/types/_icon.py
+++ b/src/app_model/types/_icon.py
@@ -1,7 +1,6 @@
-from collections.abc import Generator
-from typing import Any, Callable, Optional, TypedDict, Union
+from typing import Any, Optional, TypedDict, Union
-from pydantic_compat import Field, model_validator
+from pydantic import Field, model_validator
from ._base import _BaseModel
@@ -30,10 +29,6 @@ class Icon(_BaseModel):
" keys, such as `fa6s.arrow_down`",
)
- @classmethod
- def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:
- yield cls._validate
-
@classmethod
def _validate(cls, v: Any) -> "Icon":
"""Validate icon."""
diff --git a/src/app_model/types/_keybinding_rule.py b/src/app_model/types/_keybinding_rule.py
index f19d3de..adb99a8 100644
--- a/src/app_model/types/_keybinding_rule.py
+++ b/src/app_model/types/_keybinding_rule.py
@@ -1,6 +1,6 @@
from typing import Any, Callable, Optional, TypedDict, TypeVar, Union
-from pydantic_compat import PYDANTIC2, Field, model_validator
+from pydantic import Field, model_validator
from app_model import expressions
@@ -63,30 +63,14 @@ class KeyBindingRule(_BaseModel):
return self.linux
return self.primary
- # These methods are here to make KeyBindingRule work as a field
- # there are better ways to do this now with pydantic v2... but it still
- # feels a bit in flux. pydantic_compat might not yet work for this (or
- # at least in my playing around i couldn't get it)
- # so sticking with this one conditional method here...
- if PYDANTIC2:
- # for v2
- @model_validator(mode="wrap")
- @classmethod
- def _model_val(
- cls: type[M], v: Any, handler: Callable[[Any], M]
- ) -> "KeyBindingRule":
- if isinstance(v, StandardKeyBinding):
- return v.to_keybinding_rule()
- return handler(v) # type: ignore
-
- else:
-
- @classmethod
- def validate(cls, value: Any) -> "KeyBindingRule":
- """Validate keybinding rule."""
- if isinstance(value, StandardKeyBinding):
- return value.to_keybinding_rule()
- return super().validate(value)
+ @model_validator(mode="wrap")
+ @classmethod
+ def _model_val(
+ cls: type[M], v: Any, handler: Callable[[Any], M]
+ ) -> "KeyBindingRule":
+ if isinstance(v, StandardKeyBinding):
+ return v.to_keybinding_rule()
+ return handler(v) # type: ignore
class KeyBindingRuleDict(TypedDict, total=False):
diff --git a/src/app_model/types/_keys/_key_codes.py b/src/app_model/types/_keys/_key_codes.py
index 7e6c3cf..8594efc 100644
--- a/src/app_model/types/_keys/_key_codes.py
+++ b/src/app_model/types/_keys/_key_codes.py
@@ -199,9 +199,6 @@ class KeyCode(IntEnum):
"""
return _EVENTCODE_TO_KEYCODE.get(event_code, KeyCode.UNKNOWN)
- @classmethod
- def __get_validators__(cls) -> Generator[Callable[..., 'KeyCode'], None, None]:
- yield cls.validate
@classmethod
def __get_pydantic_core_schema__(
diff --git a/src/app_model/types/_keys/_keybindings.py b/src/app_model/types/_keys/_keybindings.py
index bcd6fb3..ee8f5dc 100644
--- a/src/app_model/types/_keys/_keybindings.py
+++ b/src/app_model/types/_keys/_keybindings.py
@@ -1,8 +1,7 @@
import re
-from collections.abc import Generator
-from typing import TYPE_CHECKING, Any, Callable, Optional
+from typing import TYPE_CHECKING, Any, Optional
-from pydantic_compat import PYDANTIC2, BaseModel, Field, model_validator
+from pydantic import BaseModel, Field, model_validator
from app_model.types._constants import OperatingSystem
@@ -166,7 +165,7 @@ class SimpleKeyBinding(BaseModel):
return cls._parse_input(instance)
-MIN1 = {"min_length": 1} if PYDANTIC2 else {"min_items": 1}
+MIN1 = {"min_length": 1}
class KeyBinding:
@@ -277,10 +276,6 @@ class KeyBinding:
def __hash__(self) -> int:
return hash(tuple(self.parts))
- @classmethod
- def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:
- yield cls.validate # pragma: no cover
-
@classmethod
def __get_pydantic_core_schema__(
cls, source: type, handler: "GetCoreSchemaHandler"
diff --git a/src/app_model/types/_menu_rule.py b/src/app_model/types/_menu_rule.py
index 5ec9f88..ee37cb2 100644
--- a/src/app_model/types/_menu_rule.py
+++ b/src/app_model/types/_menu_rule.py
@@ -1,13 +1,11 @@
-from collections.abc import Generator
from typing import (
Any,
- Callable,
Optional,
TypedDict,
Union,
)
-from pydantic_compat import Field, field_validator, model_validator
+from pydantic import Field, field_validator, model_validator
from app_model import expressions
@@ -37,10 +35,6 @@ class MenuItemBase(_BaseModel):
"and the syntax 'group@order'. If not provided, items are sorted by title.",
)
- @classmethod
- def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:
- yield cls._validate
-
@classmethod
def _validate(cls: type["MenuItemBase"], v: Any) -> "MenuItemBase":
"""Validate icon."""
@@ -64,15 +58,8 @@ class MenuRule(MenuItemBase):
id: str = Field(..., description="Menu in which to place this item.")
- # for v1
- @classmethod
- def _validate(cls: type["MenuRule"], v: Any) -> Any:
- if isinstance(v, str):
- v = {"id": v}
- return super()._validate(v)
-
- # for v2
@model_validator(mode="before")
+ @classmethod
def _validate_model(cls, v: Any) -> Any:
"""If a single string is provided, convert to a dict with `id` key."""
return {"id": v} if isinstance(v, str) else v
diff --git a/tests/test_keybindings.py b/tests/test_keybindings.py
index 1223db6..503f818 100644
--- a/tests/test_keybindings.py
+++ b/tests/test_keybindings.py
@@ -1,9 +1,8 @@
import itertools
import sys
-from typing import ClassVar
import pytest
-from pydantic_compat import PYDANTIC2, BaseModel
+from pydantic import BaseModel
from app_model.types import (
KeyBinding,
@@ -179,14 +178,8 @@ def test_in_model() -> None:
class M(BaseModel):
key: KeyBinding
- if not PYDANTIC2:
-
- class Config:
- json_encoders: ClassVar[dict] = {KeyBinding: str}
-
m = M(key="Shift+A B")
- # pydantic v1 and v2 have slightly different json outputs
- assert m.model_dump_json().replace('": "', '":"') == '{"key":"Shift+A B"}'
+ assert m.model_dump_json() == '{"key":"Shift+A B"}'
def test_standard_keybindings() -> None:
|