Skip to content

Commit e1cb7f7

Browse files
šŸ› Fix model_fields_optional compatibility with pydantic 2.11.x and linting
- Use `copy.copy()` fallback for FieldInfo when `_copy()` is unavailable (added in pydantic 2.12.0, but project supports >=2.11.0) - Replace `Union[X, None]` with `X | None` to satisfy ruff UP007/UP045 - Apply ruff formatting fixes
1 parent f7d4f18 commit e1cb7f7

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

ā€Žsqlmodel/main.pyā€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import builtins
4+
import copy
45
import ipaddress
56
import uuid
67
import weakref
@@ -594,15 +595,18 @@ def __new__(
594595
ann = field_info.annotation
595596
# Only wrap in Optional if not already Optional
596597
if ann is not None and not _is_optional_annotation(ann):
597-
pydantic_annotations[field_name] = Union[ann, None]
598+
pydantic_annotations[field_name] = ann | None
598599
else:
599600
pydantic_annotations[field_name] = ann
600601
# Set default to None if the field was required and
601602
# not already defined in the current class
602603
if field_name not in dict_for_pydantic:
603604
# Copy the FieldInfo to preserve metadata like
604605
# min_length, ge, etc.
605-
new_field_info = field_info._copy()
606+
if hasattr(field_info, "_copy"):
607+
new_field_info = field_info._copy()
608+
else:
609+
new_field_info = copy.copy(field_info)
606610
if new_field_info.is_required():
607611
new_field_info.default = None
608612
dict_for_pydantic[field_name] = new_field_info

0 commit comments

Comments
Ā (0)