Skip to content

Commit

Permalink
remove compatibility with pydantic.v1, not pydantic v1
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Feb 9, 2024
1 parent b1d9ce4 commit 807b1a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
53 changes: 36 additions & 17 deletions src/psygnal/_dataclass_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ class Foo:

# Add metadata for pydantic dataclass
if is_pydantic_model(cls):
# Pydantic v2
if not hasattr(cls, "model_fields"):
return
"""
Example
-------
Expand All @@ -335,12 +332,14 @@ class Foo:
from pydantic import BaseModel, Field
# Only works with Pydantic v2
class Foo(BaseModel):
bar: Annotated[
str,
{'__psygnal_metadata': {"alias": "bar_alias"}}
] = Field(...)
# Working with Pydantic v2 and partially with v1
# Alternative, using Field `json_schema_extra` keyword argument
class Bar(BaseModel):
bar: str = Field(
Expand All @@ -358,20 +357,40 @@ class Bar(BaseModel):
)
"""
for field_name, p_field in cls.model_fields.items():
if not p_field.frozen or not exclude_frozen:
metadata_list = getattr(p_field, "metadata", [])
metadata = {}
for field in metadata_list:
metadata.update(field.get(PSYGNAL_METADATA, {}))
# Compatibility with using Field `json_schema_extra` keyword argument
if isinstance(getattr(p_field, "json_schema_extra", None), Mapping):
extra = cast(Mapping, p_field.json_schema_extra)
metadata.update(extra.get(PSYGNAL_METADATA, {}))
metadata = sanitize_field_options_dict(metadata)
options = FieldOptions(field_name, p_field.annotation, **metadata)
yield options
return
if hasattr(cls, "model_fields"):
# Pydantic v2
for field_name, p_field in cls.model_fields.items():
if not p_field.frozen or not exclude_frozen:
metadata_list = getattr(p_field, "metadata", [])
metadata = {}
for field in metadata_list:
metadata.update(field.get(PSYGNAL_METADATA, {}))
# Compat with using Field `json_schema_extra` keyword argument
if isinstance(getattr(p_field, "json_schema_extra", None), Mapping):
meta_dict = cast(Mapping, p_field.json_schema_extra)
metadata.update(meta_dict.get(PSYGNAL_METADATA, {}))
metadata = sanitize_field_options_dict(metadata)
options = FieldOptions(field_name, p_field.annotation, **metadata)
yield options
return

else:
# Pydantic v1, metadata is not always working
for pv1_field in cls.__fields__.values(): # type: ignore [attr-defined]
if pv1_field.field_info.allow_mutation or not exclude_frozen: # type: ignore [attr-defined]
meta_dict = getattr(pv1_field.field_info, "extra", {}).get( # type: ignore [attr-defined]
"json_schema_extra", {}
)
metadata = meta_dict.get(PSYGNAL_METADATA, {})

metadata = sanitize_field_options_dict(metadata)
options = FieldOptions(
pv1_field.name, # type: ignore [attr-defined]
pv1_field.outer_type_, # type: ignore [attr-defined]
**metadata,
)
yield options
return

# Add metadata for attrs dataclass
attrs_fields = getattr(cls, "__attrs_attrs__", None)
Expand Down
2 changes: 1 addition & 1 deletion src/psygnal/_group_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _build_dataclass_signal_group(
# Overwrite the signal aliases with user defined
signal_aliases = {**signal_aliases, **_fields_table}

# print(f"Create SignalGroup: {group_name}, {signal_aliases=}, {signals=}")
# Create SignalGroup subclass with the attached signals and signal_aliases
return type(group_name, (SignalGroup,), signals, signal_aliases=signal_aliases)


Expand Down

0 comments on commit 807b1a7

Please sign in to comment.