Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic v2 migration #47

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions redbird/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass
import warnings

from pydantic import BaseModel, Field, validator
from pydantic import ConfigDict, BaseModel, Field, validator

from redbird.exc import ConversionWarning, DataToItemError, KeyFoundError, ItemToDataError, _handle_conversion_error
from redbird.utils.case import to_case
Expand Down Expand Up @@ -142,16 +142,18 @@ class BaseRepo(ABC, BaseModel):
cls_result: ClassVar[Type[BaseResult]]

model: Type = dict
id_field: Optional[str]
id_field: Optional[str] = None

query_model: Optional[Type[BaseModel]] = BasicQuery

errors_query: Literal['raise', 'warn', 'discard'] = 'raise'
field_access: Literal['attr', 'key', 'infer'] = 'infer'

# Attributes that specifies how the repo behaves
ordered: bool = Field(default=False, const=True)
ordered: bool = Field(default=False)

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator('id_field', always=True)
def set_id_field(cls, value, values):
if value is None:
Expand Down Expand Up @@ -376,3 +378,4 @@ def set_field_value(self, item: Item, key, value):
}[field_access]

func(item, key, value)
model_config = ConfigDict(arbitrary_types_allowed=True)
2 changes: 1 addition & 1 deletion redbird/repos/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CSVFileRepo(TemplateRepo):
kwds_csv: dict = {}

_session = PrivateAttr()
ordered: bool = Field(default=True, const=True)
ordered: bool = Field(default=True)

def insert(self, item):
file_non_zero = self.filename.exists() and self.filename.stat().st_size > 0
Expand Down
2 changes: 1 addition & 1 deletion redbird/repos/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MemoryRepo(TemplateRepo):
#cls_result = MemoryResult
collection: List[Any] = []

ordered: bool = Field(default=False, const=True)
ordered: bool = Field(default=False)
_session = PrivateAttr()

def insert(self, item):
Expand Down
12 changes: 6 additions & 6 deletions redbird/repos/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class MongoRepo(TemplateRepo):
repo = MongoRepo(client=MongoClient("mongodb://localhost:27017"))
"""
# cls_result = MongoResult
default_id_field = "_id"
cls_session = MongoSession
default_id_field: str = "_id"
cls_session: MongoSession = MongoSession

__operators__ = {
GreaterThan: "$gt",
Expand All @@ -145,11 +145,11 @@ class MongoRepo(TemplateRepo):
NotEqual: "$ne",
In: "$in",
}
session: Any
database: Optional[str]
collection: Optional[str]
session: Any = None
database: Optional[str] = None
collection: Optional[str] = None

ordered: bool = Field(default=True, const=True)
ordered: bool = Field(default=True)

def __init__(self, *args, uri=None, client=None, session=None, **kwargs):
if uri is not None:
Expand Down
4 changes: 2 additions & 2 deletions redbird/repos/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ class RESTRepo(TemplateRepo):

repo = RESTRepo(url="http://example.com/api", url_params={"fields": "car_type,car_model,registration_number"})
"""
result: Optional[Union[str, Callable]]
result: Optional[Union[str, Callable]] = None
url: str
url_params: dict = {}
headers: dict = {}

_session = PrivateAttr()

ordered: bool = Field(default=False, const=True)
ordered: bool = Field(default=False)

def insert(self, item):
json = self.item_to_dict(item, exclude_unset=False)
Expand Down
14 changes: 7 additions & 7 deletions redbird/repos/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ class Car(BaseModel):
repo = SQLRepo(model=Car, model_orm=CarORM, engine=engine)
"""

model_orm: Optional[Any]
table: Optional[str]
session: Any
engine: Optional[Any]
model_orm: Optional[Any] = None
table: Optional[str] = None
session: Any = None
engine: Optional[Any] = None
autocommit: bool = Field(default=True, description="Whether to automatically commit the writes (create, delete, update)")

ordered: bool = Field(default=True, const=True)
ordered: bool = Field(default=True)
_Base = PrivateAttr()

@classmethod
Expand Down Expand Up @@ -303,8 +303,8 @@ def _create_table(self, session, model, name, primary_column=None):

class SQLExprRepo(TemplateRepo):

table: Optional[str]
engine: Optional[Any]
table: Optional[str] = None
engine: Optional[Any] = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
9 changes: 4 additions & 5 deletions redbird/test/common/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from redbird.repos.mongo import MongoRepo
from redbird.oper import greater_equal, greater_than, less_equal, less_than, not_equal

from pydantic import BaseModel, Field
from pydantic import ConfigDict, BaseModel, Field

# ------------------------
# TEST ITEMS
Expand All @@ -27,14 +27,13 @@ class PydanticItem(BaseModel):
__colname__ = 'items'
id: str
name: str
age: Optional[int]
age: Optional[int] = None

class PydanticItemORM(BaseModel):
id: str
name: str
age: Optional[int]
class Config:
orm_mode = True
age: Optional[int] = None
model_config = ConfigDict(from_attributes=True)

class MongoItem(BaseModel):
__colname__ = 'items'
Expand Down
6 changes: 3 additions & 3 deletions redbird/test/logging/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class LogRecord(BaseModel):
pathname: str
filename: str
module: str
exc_info: Optional[tuple]
exc_text: Optional[str]
stack_info: Optional[tuple]
exc_info: Optional[tuple] = None
exc_text: Optional[str] = None
stack_info: Optional[tuple] = None
lineno: int
funcName: str
created: float
Expand Down
2 changes: 1 addition & 1 deletion redbird/test/repo/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Item(BaseModel):
id: str
name: str
age: Optional[int]
age: Optional[int] = None

def test_filecontent(tmpdir):
file = tmpdir / "test.csv"
Expand Down
2 changes: 1 addition & 1 deletion redbird/test/repo/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Item(BaseModel):
id: str
name: str
age: Optional[int]
age: Optional[int] = None

def sort_items(items, repo, field="id"):
return list(sorted(items, key=lambda x: repo.get_field_value(x, field)))
Expand Down
4 changes: 2 additions & 2 deletions redbird/test/repo/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
class Item(BaseModel):
id: str
name: str
age: Optional[int]
age: Optional[int] = None


class ItemWithCol(BaseModel):
__colname__ = 'items'
id: str
name: str
age: Optional[int]
age: Optional[int] = None

def test_creation_defaults():
pytest.importorskip("pymongo")
Expand Down
5 changes: 2 additions & 3 deletions redbird/test/repo/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
import pytest
from redbird.repos import SQLRepo
from pydantic import BaseModel
from pydantic import ConfigDict, BaseModel

try:
from typing import Literal
Expand All @@ -20,8 +20,7 @@ class MyItemWithORM(BaseModel):
id: str
name: str
age: int
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)

try:
from sqlalchemy.orm import declarative_base
Expand Down
2 changes: 1 addition & 1 deletion redbird/test/sql/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MyModel(BaseModel):
name: str
score: int = 999
birth_date: date
color: Optional[str]
color: Optional[str] = None

tbl = Table("mytable", bind=engine)
tbl.create_from_model(MyModel)
Expand Down