Skip to content

[feature] : Use properties in pydantic dataclasses to eliminate None uncertainty for to-be-assigned attributes #1335

@SylviaWhittle

Description

@SylviaWhittle

Is your feature request related to a problem?

This is a gentle suggestion.

Currently we experience a lot of this:

Image

There are several ways to alleviate this issue.

I have used a requires_<attribute>() method pattern in the past:

    def require_grain_crops(self) -> dict[int, GrainCrop]:
        """
        Return a non-None grain_crops mapping or raise and exception.

        Returns
        -------
        dict[int, GrainCrop]
            The grain crops for the molecule.

        Raises
        ------
        RuntimeError
            If grain_crops is None.
        """
        if self.grain_crops is None:
            raise RuntimeError("grain_crops is None")
        return self.grain_crops
Image

But since we are using pydantic, we could use @property instead:

    @property
    def property_grain_crops(self) -> dict[int, GrainCrop]:
        if self.grain_crops is None:
            raise RuntimeError("grain_crops is None")
        return self.grain_crops

Which would also solve the issue.

But of course this would require a nomenclature change in all classes, to use _grain_crops for the internal attribute name, and then grain_crops for the property` and that would be a bit of a hassle.

Describe the solution you would like.

Implement one of the above to get rid of the numerous type issues in the codebase?

Describe the alternatives you have considered.

Not essential but would be nice.

Additional context

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions