Skip to content

Commit

Permalink
Merge pull request #30 from alliander-opensource/pydanticv2
Browse files Browse the repository at this point in the history
Pydanticv 2
  • Loading branch information
guillaume-alliander authored Nov 16, 2023
2 parents 7aee624 + 909eee7 commit b917331
Show file tree
Hide file tree
Showing 536 changed files with 18,705 additions and 12,184 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ the work. For more information see the Code review guideline.

## Style guide

We use black, pylint, ruff, mypy, pytest and coverage to enforce good quality.
We use black, ruff, pyright, pytest and coverage to enforce good quality.
They are all checked via Github actions, and can be run locally (after installing the
dev dependency of the project) with `scons all`. Look at the [workflow](.github/workflows/_core.yaml) to see the specific steps.

Expand Down
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ If this is a leaf node (for instance `ACLineSegment`), it "just works". If you w
class higher in the hierarchy (for instance `Equipment`) there is a lot more work to do.

```python
@dataclass(config=DataclassConfig)
@dataclass
class CustomBay(Bay):
colour: str = Field(
default="Red",
Expand Down Expand Up @@ -98,15 +98,15 @@ class CustomProfile(BaseProfile):
And use it everywhere you would use a profile:

```python
from pycgmes.utils.dataclassconfig import DataclassConfig

@dataclass(config=DataclassConfig)
@dataclass
class CustomBayAttr(Bay):
colour: str = Field(
default="Red",
in_profiles=[
CustomProfile.CUS,
],
json_schema_extra={
"in_profiles": [
CustomProfile.CUS,
],
}
)

# And for instance:
Expand All @@ -124,10 +124,9 @@ In the case of a custom attribute defined via a sub class, the result would be:
from pydantic.dataclasses import dataclass

from pycgmes.resources.ACLineSegment import ACLineSegment
from pycgmes.utils.dataclassconfig import DataclassConfig


@dataclass(config=DataclassConfig)
@dataclass
class ACLineSegmentCustom(ACLineSegment):
@classmethod
def apparent_name(cls):
Expand Down Expand Up @@ -157,28 +156,31 @@ from pydantic.dataclasses import dataclass
from pydantic import Field

from pycgmes.resources import ACLineSegment
from pycgmes.resources.Base import DataclassConfig, Profile


@dataclass(config=DataclassConfig)
@dataclass
class ACLineSegmentCustom(ACLineSegment):
colour: str = Field(
default="Red",
in_profiles=[
Profile.EQ, # Do not do this, see chapter "create a new profile"
],
namespace="custom",
json_schema_extra={
"in_profiles": [
Profile.EQ, # Do not do this, see chapter "create a new profile"
],
"namespace": "custom",
},
)

size: str = Field(
default="Big",
in_profiles=[
Profile.EQ, # Do not do this, see chapter "create a new profile"
],
json_schema_extra={
"in_profiles": [
Profile.EQ, # Do not do this, see chapter "create a new profile"
],
}
)

@property
def namesapce(self) -> str:
def namespace(self) -> str:
return "custom ns class"

@classmethod
Expand Down Expand Up @@ -225,7 +227,7 @@ Generated from the modernpython serialisation of [cimgen](https://github.com/sog

The CI happens in GitHub actions.

The standard black/mypy/autoflake/isort/pylint/ruff/mypy are run there, via scons.
The standard black/pyright/ruff are run there, via scons.

### CD

Expand Down
6 changes: 2 additions & 4 deletions SConstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ def _exec(command: str, env: Mapping | None = None) -> int:
if "lock" in COMMAND_LINE_TARGETS:
_exec("poetry check --lock")

if "lint" in COMMAND_LINE_TARGETS or "lint" in COMMAND_LINE_TARGETS:
_exec(f"pylint --rcfile=pyproject.toml {_SUBJECT}")


if "type" in COMMAND_LINE_TARGETS or "pyright" in COMMAND_LINE_TARGETS:
# https://mypy.readthedocs.io/en/stable/running_mypy.html#library-stubs-not-installed
_exec("pyright", env=os.environ)
_target_found = True

Expand All @@ -96,6 +92,8 @@ def _exec(command: str, env: Mapping | None = None) -> int:
_exec("coverage report --show-missing")

if "license" in COMMAND_LINE_TARGETS or "licence" in COMMAND_LINE_TARGETS:
# To fix:
# reuse annotate --copyright="Alliander" --license=Apache-2.0 --recursive pycgmes/{resources,utils}
_exec("reuse lint")

if not _target_found:
Expand Down
195 changes: 143 additions & 52 deletions poetry.lock

Large diffs are not rendered by default.

167 changes: 103 additions & 64 deletions pycgmes/resources/ACDCConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
from pydantic import Field
from pydantic.dataclasses import dataclass

from ..utils.dataclassconfig import DataclassConfig
from ..utils.profile import BaseProfile, Profile
from .ConductingEquipment import ConductingEquipment


@dataclass(config=DataclassConfig)
@dataclass
class ACDCConverter(ConductingEquipment):
"""
A unit with valves for three phases, together with unit control equipment, essential protective and switching
Expand Down Expand Up @@ -65,146 +64,186 @@ class ACDCConverter(ConductingEquipment):

baseS: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

idleLoss: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

maxUdc: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

minUdc: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

numberOfValves: int = Field(
default=0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

ratedUdc: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

resistiveLoss: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

switchingLoss: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

valveU0: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

maxP: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

minP: float = Field(
default=0.0,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

PccTerminal: Optional[str] = Field(
default=None,
in_profiles=[
Profile.EQ,
],
json_schema_extra={
"in_profiles": [
Profile.EQ,
]
},
)

# *Association not used*
# Type M:0..n in CIM # pylint: disable-next=line-too-long
# DCTerminals : list = Field(default_factory=list, in_profiles = [Profile.EQ, ])
# Type M:0..n in CIM
# DCTerminals : list = Field(default_factory=list, json_schema_extra={"in_profiles":[Profile.EQ, ]})

idc: float = Field(
default=0.0,
in_profiles=[
Profile.SV,
],
json_schema_extra={
"in_profiles": [
Profile.SV,
]
},
)

poleLossP: float = Field(
default=0.0,
in_profiles=[
Profile.SV,
],
json_schema_extra={
"in_profiles": [
Profile.SV,
]
},
)

uc: float = Field(
default=0.0,
in_profiles=[
Profile.SV,
],
json_schema_extra={
"in_profiles": [
Profile.SV,
]
},
)

udc: float = Field(
default=0.0,
in_profiles=[
Profile.SV,
],
json_schema_extra={
"in_profiles": [
Profile.SV,
]
},
)

p: float = Field(
default=0.0,
in_profiles=[
Profile.SSH,
],
json_schema_extra={
"in_profiles": [
Profile.SSH,
]
},
)

q: float = Field(
default=0.0,
in_profiles=[
Profile.SSH,
],
json_schema_extra={
"in_profiles": [
Profile.SSH,
]
},
)

targetPpcc: float = Field(
default=0.0,
in_profiles=[
Profile.SSH,
],
json_schema_extra={
"in_profiles": [
Profile.SSH,
]
},
)

targetUdc: float = Field(
default=0.0,
in_profiles=[
Profile.SSH,
],
json_schema_extra={
"in_profiles": [
Profile.SSH,
]
},
)

@cached_property
Expand Down
Loading

0 comments on commit b917331

Please sign in to comment.