Skip to content

Commit

Permalink
Fix #434, pydantic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskoenig committed Aug 2, 2023
1 parent 87aa60a commit 174324b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
13 changes: 13 additions & 0 deletions release-notes/0.8.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Release notes for sbmlutils 0.8.5
![sbmlutils](https://github.com/matthiaskoenig/sbmlutils/raw/develop/docs_builder/images/sbmlutils-logo-60.png)

We are pleased to release the next version of sbmlutils including the
following changes:

# Features

- fix #434, pydantic breaking changes
- minor fixes in type comparison
- bump pydantic version to >2.1 via pymetadata>=0.3.10

Your sbmlutils team
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ keywords =
zip_safe = True
python_requires = >=3.9
install_requires =
pymetadata>=0.3.9
pymetadata>=0.3.10

depinfo
rich
lxml
requests
jinja2
xmltodict
pydantic

numpy>=1.21.1
python-libsbml>=5.20.1
Expand Down
4 changes: 2 additions & 2 deletions src/sbmlutils/comp/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ def create_ports(
data = metaIdRefs

# dictionary, port ids are provided
if type(data) == dict:
if isinstance(data, dict):
for pid, ref in data.items():
kwargs = {"pid": pid, ptype: ref}
ports.append(_create_port(model, portType=portType, **kwargs))

# only a list of references, port ids created via suffix appending
elif type(data) in [list, tuple]:
elif isinstance(data, (list, tuple)):
for ref in data:
pid = ref + suffix
kwargs = {"pid": pid, ptype: ref}
Expand Down
2 changes: 1 addition & 1 deletion src/sbmlutils/converters/odefac.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def to_custom_template(self, output_file: Path, template_file: Path) -> None:
template_file=template_file.name,
index_offset=0,
replace_symbols=False,
template_dir=template_file.parent
template_dir=template_file.parent,
)
with open(output_file, "w") as f:
f.write(content)
Expand Down
40 changes: 24 additions & 16 deletions src/sbmlutils/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Type, Union
from typing import (
Any,
ClassVar,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Type,
Union,
)

import libsbml
import numpy as np
import xmltodict # type: ignore
from numpy import NaN
from pint import UndefinedUnitError, UnitRegistry
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from pymetadata.core.creator import Creator

from sbmlutils.console import console
Expand Down Expand Up @@ -751,11 +762,6 @@ def __init__(
if not self.name:
self.name = self.definition

class Config:
"""Pydantic configuration."""

arbitrary_types_allowed = True

def create_sbml(self, model: libsbml.Model) -> Optional[libsbml.UnitDefinition]:
"""Create libsbml.UnitDefinition."""
if isinstance(self.definition, int):
Expand Down Expand Up @@ -3103,6 +3109,11 @@ class ModelDict(TypedDict, total=False):
class Model(Sbase, FrozenClass, BaseModel):
"""Model."""

model_config = ConfigDict(
extra="allow",
arbitrary_types_allowed=True,
)

sid: str
name: Optional[str]
sboTerm: Optional[str]
Expand Down Expand Up @@ -3140,12 +3151,7 @@ class Model(Sbase, FrozenClass, BaseModel):
# layout
layouts: Optional[List]

class Config:
"""Pydantic configuration."""

arbitrary_types_allowed = True

_keys = {
_keys: ClassVar[Dict[str, Any]] = {
"sid": None,
"name": None,
"sboTerm": None,
Expand Down Expand Up @@ -3181,7 +3187,7 @@ class Config:
"layouts": list,
}

_supported_packages = {
_supported_packages: ClassVar[Set[str]] = {
Package.COMP,
Package.COMP_V1,
Package.DISTRIB,
Expand All @@ -3193,8 +3199,10 @@ class Config:

def __str__(self) -> str:
"""Get string."""
field_str = ", ".join(f"{a}={v!r}" for a, v in self.__repr_args__() if a and v)
return f"{self.__class__.__name__}({field_str})"
# FIXME: issue with access
# field_str = ", ".join(f"{a}={v!r}" for a, v in self.__repr_args__() if a and v and not a.startswith("_"))
# return f"{self.__class__.__name__}({field_str})"
return f"{self.__class__.__name__}"

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/sbmlutils/report/sbmlinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def sbase_dict(cls, sbase: libsbml.SBase) -> Dict[str, Any]:

# comp
item_comp = sbase.getPlugin("comp")
if item_comp and type(item_comp) == libsbml.CompSBasePlugin:
if item_comp and isinstance(item_comp, libsbml.CompSBasePlugin):
# ReplacedBy
if item_comp.isSetReplacedBy():
replaced_by = item_comp.getReplacedBy()
Expand Down

0 comments on commit 174324b

Please sign in to comment.