Skip to content

Commit 4b5ad42

Browse files
svlandegtiangolo
andauthored
💚 Fix linting in CI (#1340)
Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent 6c0410e commit 4b5ad42

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
if: matrix.pydantic-version == 'pydantic-v2'
6868
run: uv pip install --upgrade "pydantic>=2.0.2,<3.0.0"
6969
- name: Lint
70-
if: matrix.pydantic-version == 'pydantic-v2'
70+
if: matrix.pydantic-version == 'pydantic-v2' && matrix.python-version != '3.8'
7171
run: bash scripts/lint.sh
7272
- run: mkdir coverage
7373
- name: Test

sqlmodel/_compat.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ def set_config_value(
103103
model.model_config[parameter] = value # type: ignore[literal-required]
104104

105105
def get_model_fields(model: InstanceOrType[BaseModel]) -> Dict[str, "FieldInfo"]:
106-
return model.model_fields
106+
# TODO: refactor the usage of this function to always pass the class
107+
# not the instance, and then remove this extra check
108+
# this is for compatibility with Pydantic v3
109+
if isinstance(model, type):
110+
use_model = model
111+
else:
112+
use_model = model.__class__
113+
return use_model.model_fields
107114

108115
def get_fields_set(
109116
object: InstanceOrType["SQLModel"],

sqlmodel/main.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def Relationship(
477477
class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
478478
__sqlmodel_relationships__: Dict[str, RelationshipInfo]
479479
model_config: SQLModelConfig
480-
model_fields: Dict[str, FieldInfo] # type: ignore[assignment]
480+
model_fields: ClassVar[Dict[str, FieldInfo]]
481481
__config__: Type[SQLModelConfig]
482482
__fields__: Dict[str, ModelField] # type: ignore[assignment]
483483

@@ -839,7 +839,7 @@ def __tablename__(cls) -> str:
839839
return cls.__name__.lower()
840840

841841
@classmethod
842-
def model_validate(
842+
def model_validate( # type: ignore[override]
843843
cls: Type[_TSQLModel],
844844
obj: Any,
845845
*,
@@ -863,20 +863,25 @@ def model_dump(
863863
mode: Union[Literal["json", "python"], str] = "python",
864864
include: Union[IncEx, None] = None,
865865
exclude: Union[IncEx, None] = None,
866-
context: Union[Dict[str, Any], None] = None,
867-
by_alias: bool = False,
866+
context: Union[Any, None] = None,
867+
by_alias: Union[bool, None] = None,
868868
exclude_unset: bool = False,
869869
exclude_defaults: bool = False,
870870
exclude_none: bool = False,
871871
round_trip: bool = False,
872872
warnings: Union[bool, Literal["none", "warn", "error"]] = True,
873+
fallback: Union[Callable[[Any], Any], None] = None,
873874
serialize_as_any: bool = False,
874875
) -> Dict[str, Any]:
876+
if PYDANTIC_MINOR_VERSION < (2, 11):
877+
by_alias = by_alias or False
875878
if PYDANTIC_MINOR_VERSION >= (2, 7):
876879
extra_kwargs: Dict[str, Any] = {
877880
"context": context,
878881
"serialize_as_any": serialize_as_any,
879882
}
883+
if PYDANTIC_MINOR_VERSION >= (2, 11):
884+
extra_kwargs["fallback"] = fallback
880885
else:
881886
extra_kwargs = {}
882887
if IS_PYDANTIC_V2:
@@ -896,7 +901,7 @@ def model_dump(
896901
return super().dict(
897902
include=include,
898903
exclude=exclude,
899-
by_alias=by_alias,
904+
by_alias=by_alias or False,
900905
exclude_unset=exclude_unset,
901906
exclude_defaults=exclude_defaults,
902907
exclude_none=exclude_none,

0 commit comments

Comments
 (0)