Skip to content

Commit

Permalink
DOC: Complete adding docstrings to Pydantic models
Browse files Browse the repository at this point in the history
Also adds them to enums, and renames some classes to remove the
redundant `Enum` in the name or align with other names (i.e.
FMUSomething as opposed to FmuSomething).

Removing descriptions from the `Field(description="...")` also removes
these descriptions from the schema right now. However, with Pydantic 2.7
(when we are able to upgrade to it), moving these descriptions to
docstrings will add them back into the schema description. So this loss
of fidelity is temporary.
  • Loading branch information
mferrera committed Jul 4, 2024
1 parent 6263e89 commit 34b0084
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 430 deletions.
111 changes: 25 additions & 86 deletions schema/definitions/0.8.0/schema/fmu_meta.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/fmu/dataio/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from . import _utils, dataio, types
from ._logging import null_logger
from ._metadata import generate_meta_tracklog
from .datastructure.meta.enums import FmuContext
from .datastructure.meta.enums import FMUContext
from .providers.objectdata._provider import objectdata_provider_factory

logger: Final = null_logger(__name__)
Expand Down Expand Up @@ -230,7 +230,7 @@ def _generate_aggrd_metadata(
template["fmu"]["aggregation"]["id"] = self.aggregation_id

# fmu.context.stage should be 'iteration'
template["fmu"]["context"]["stage"] = FmuContext.iteration.value
template["fmu"]["context"]["stage"] = FMUContext.iteration.value

# next, the new object will trigger update of: 'file', 'data' (some fields) and
# 'tracklog'.
Expand Down
12 changes: 6 additions & 6 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .case import InitializeCase
from .datastructure.configuration import global_configuration
from .datastructure.meta import enums
from .datastructure.meta.enums import FmuContext
from .datastructure.meta.enums import FMUContext
from .preprocessed import ExportPreprocessedData
from .providers._fmu import FmuProvider, get_fmu_context_from_environment

Expand Down Expand Up @@ -662,15 +662,15 @@ def _validate_and_establish_fmucontext(self) -> None:
self.fmu_context = None

else:
self.fmu_context = FmuContext(self.fmu_context.lower())
self.fmu_context = FMUContext(self.fmu_context.lower())
logger.info("FMU context is %s", self.fmu_context)

if self.preprocessed and self.fmu_context == FmuContext.realization:
if self.preprocessed and self.fmu_context == FMUContext.realization:
raise ValueError(
"Can't export preprocessed data in a fmu_context='realization'."
)

if self.fmu_context != FmuContext.case and env_fmu_context == FmuContext.case:
if self.fmu_context != FMUContext.case and env_fmu_context == FMUContext.case:
warn(
"fmu_context is set to 'realization', but unable to detect "
"ERT runpath from environment variable. "
Expand Down Expand Up @@ -727,7 +727,7 @@ def _establish_rootpath(self) -> Path:
)

if self._fmurun:
assert isinstance(self.fmu_context, FmuContext)
assert isinstance(self.fmu_context, FMUContext)
if casepath := FmuProvider(
fmu_context=self.fmu_context,
casepath_proposed=Path(self.casepath) if self.casepath else None,
Expand All @@ -747,7 +747,7 @@ def _establish_rootpath(self) -> Path:
return self._pwd

def _get_fmu_provider(self) -> FmuProvider:
assert isinstance(self.fmu_context, FmuContext)
assert isinstance(self.fmu_context, FMUContext)
return FmuProvider(
model=self.config.get("model"),
fmu_context=self.fmu_context,
Expand Down
26 changes: 13 additions & 13 deletions src/fmu/dataio/datastructure/_internal/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ContentRequireSpecific(BaseModel):


class AllowedContent(BaseModel):
content: Union[meta.enums.ContentEnum, Literal["unset"]]
content: Union[meta.enums.Content, Literal["unset"]]
content_incl_specific: Optional[ContentRequireSpecific] = Field(default=None)

@model_validator(mode="before")
Expand All @@ -81,7 +81,7 @@ def _validate_input(cls, values: dict) -> dict:

if content in ContentRequireSpecific.model_fields and not content_specific:
# 'property' should be included below after a deprecation period
if content == meta.enums.ContentEnum.property:
if content == meta.enums.Content.property:
property_warn()
else:
raise ValueError(f"Content {content} requires additional input")
Expand Down Expand Up @@ -113,7 +113,7 @@ class FMUModelCase(BaseModel):


class Context(BaseModel, use_enum_values=True):
stage: meta.enums.FmuContext
stage: meta.enums.FMUContext


# Remove the two models below when content is required as input.
Expand All @@ -122,7 +122,7 @@ class UnsetContent(meta.content.Data):

@model_validator(mode="after")
def _deprecation_warning(self) -> UnsetContent:
valid_contents = [m.value for m in meta.enums.ContentEnum]
valid_contents = [m.value for m in meta.enums.Content]
warnings.warn(
"The <content> is not provided which will produce invalid metadata. "
"It is strongly recommended that content is given explicitly! "
Expand All @@ -147,15 +147,15 @@ class DataClassMeta(JsonSchemaMetadata):
# TODO: aim to use meta.FMUDataClassMeta as base
# class and disallow creating invalid metadata.
class_: Literal[
meta.enums.FMUClassEnum.surface,
meta.enums.FMUClassEnum.table,
meta.enums.FMUClassEnum.cpgrid,
meta.enums.FMUClassEnum.cpgrid_property,
meta.enums.FMUClassEnum.polygons,
meta.enums.FMUClassEnum.cube,
meta.enums.FMUClassEnum.well,
meta.enums.FMUClassEnum.points,
meta.enums.FMUClassEnum.dictionary,
meta.enums.FMUClass.surface,
meta.enums.FMUClass.table,
meta.enums.FMUClass.cpgrid,
meta.enums.FMUClass.cpgrid_property,
meta.enums.FMUClass.polygons,
meta.enums.FMUClass.cube,
meta.enums.FMUClass.well,
meta.enums.FMUClass.points,
meta.enums.FMUClass.dictionary,
] = Field(alias="class")
fmu: Optional[FMUClassMetaData]
masterdata: Optional[meta.Masterdata]
Expand Down
Loading

0 comments on commit 34b0084

Please sign in to comment.