Skip to content

Commit

Permalink
Merge branch 'main' into fix-pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Nov 21, 2024
2 parents 07d3592 + 63f32b0 commit af08786
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ ci:

repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.19
rev: v0.23
hooks:
- id: validate-pyproject

- repo: https://github.com/crate-ci/typos
rev: v1.24.5
rev: typos-dict-v0.11.35
hooks:
- id: typos
args: [--force-exclude] # omit --write-changes

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.4
rev: v0.7.4
hooks:
- id: ruff
args: [--fix, --unsafe-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
rev: v1.13.0
hooks:
- id: mypy
exclude: ^tests|^docs|_napari_plugin|widgets
additional_dependencies:
- pydantic>=2
- pydantic>=2.10
- pydantic-compat
- xsdata==24.2.1
- Pint
Expand Down
4 changes: 3 additions & 1 deletion src/ome_autogen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ def _disp_type(obj: Any) -> str:
else:
_fields = [
f"{k}: {_disp_type(v.annotation)}"
for k, v in sorted(m.model_fields.items())
# this type ignore indicates something that may break in pydantic 3
# but for now, it's confusing and I think it's an error
for k, v in sorted(m.model_fields.items()) # type: ignore
]
if _fields:
module += CLASS.format(
Expand Down
4 changes: 2 additions & 2 deletions src/ome_types/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def _get_ns_elem(elem: ET._Element | AnyElementTree) -> str:
"""Get namespace from an element or element tree."""
root = elem.getroot() if hasattr(elem, "getroot") else elem
# return root.nsmap[root.prefix] this only works for lxml
return root.tag.split("}", 1)[0].lstrip("{")
return str(root.tag).split("}", 1)[0].lstrip("{")


def _get_ns_file(source: FileLike) -> str:
Expand All @@ -582,7 +582,7 @@ def _get_root_ome_type(xml: FileLike | AnyElementTree) -> type[OMEType]:
if hasattr(xml, "seek"):
xml.seek(0)
_, root = next(ET.iterparse(xml, events=("start",)))
localname = cast("ET._Element", root).tag.rsplit("}", 1)[-1]
localname = str(root.tag).rsplit("}", 1)[-1]

if hasattr(xml, "seek"):
xml.seek(0)
Expand Down
7 changes: 6 additions & 1 deletion src/ome_types/_pydantic_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def field_type(field: FieldInfo) -> Any:
return field.annotation

def field_regex(obj: type[BaseModel], field_name: str) -> str | None:
field_info = obj.model_fields[field_name]
# typing is incorrect at the moment, but may indicate breakage in pydantic 3
field_info = obj.model_fields[field_name] # type: ignore [index]
meta = field_info.json_schema_extra or {}
# if a "metadata" key exists... use it.
# After pydantic-compat 0.2, this is where it will be.
Expand All @@ -35,6 +36,10 @@ def field_regex(obj: type[BaseModel], field_name: str) -> str | None:

kw: dict = {"validated_data": {}} if pydantic_version >= (2, 10) else {}

def get_default(f: FieldInfo) -> Any:
return f.get_default(call_default_factory=True, **kw)


def get_default(f: FieldInfo) -> Any:
return f.get_default(call_default_factory=True, **kw)
else:
Expand Down
4 changes: 2 additions & 2 deletions src/xsdata_pydantic_basemodel/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class AnyElement(PydanticCompatMixin, BaseModel):
tail: Optional[str] = Field(default=None)
children: List["AnyElement"] = Field(
default_factory=list,
metadata={"type": XmlType.WILDCARD}, # type: ignore [call-arg]
metadata={"type": XmlType.WILDCARD}, # type: ignore [call-arg,call-overload]
)
attributes: Dict[str, str] = Field(
default_factory=dict,
metadata={"type": XmlType.ATTRIBUTES}, # type: ignore [call-arg]
metadata={"type": XmlType.ATTRIBUTES}, # type: ignore [call-arg,call-overload]
)

model_config: ClassVar["ConfigDict"] = {"arbitrary_types_allowed": True}
Expand Down
2 changes: 1 addition & 1 deletion src/xsdata_pydantic_basemodel/pydantic_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ def dataclass_fields(obj: type[M]) -> tuple[dc.Field, ...]:
"""Return a tuple of dataclass fields for the given pydantic model class."""
return tuple(
_pydantic_field_to_dataclass_field(name, f)
for name, f in obj.model_fields.items()
for name, f in obj.model_fields.items() # type: ignore
)

0 comments on commit af08786

Please sign in to comment.