Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.2.5"
__version__ = "2.3.0"
20 changes: 10 additions & 10 deletions gemd/entity/template/base_template.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Base template."""

from typing import Iterable, Mapping, Union
from typing import Iterable, List, Mapping, Optional, Tuple, Union

from gemd.entity.base_entity import BaseEntity
from gemd.entity.bounds.base_bounds import BaseBounds
from gemd.entity.link_by_uid import LinkByUID
from gemd.entity.template.attribute_template import AttributeTemplate

__all__ = ["BaseTemplate"]
_AttributeTemplateType = Union[AttributeTemplate, LinkByUID]
_AttributePairType = Tuple[_AttributeTemplateType, Optional[BaseBounds]]
_AttributeListPairType = List[Optional[Union[_AttributeTemplateType, BaseBounds]]]
_AttributeInputType = Union[_AttributeTemplateType, _AttributePairType, _AttributeListPairType]


class BaseTemplate(BaseEntity):
Expand Down Expand Up @@ -43,12 +47,8 @@ def __init__(
self.description = description

@staticmethod
def _homogenize_ranges(
template_or_tuple: Union[
AttributeTemplate, LinkByUID, Iterable[Union[AttributeTemplate, BaseBounds]]
],
):
"""Take either a template or pair and turn it into a (template, bounds) pair.
def _homogenize_ranges(template_or_tuple: _AttributeInputType) -> _AttributePairType:
"""Take either a template or pair and turn it into a (template, bounds) tuple.

If no bounds are provided, use the attribute template's default bounds.

Expand All @@ -60,14 +60,14 @@ def _homogenize_ranges(

Returns
-------
List[AttributeTemplate or LinkByUID, BaseBounds]
Tuple[AttributeTemplate or LinkByUID, BaseBounds or None]
The attribute template and bounds that should be applied to the attribute
when used in the context of **this** object.

"""
# if given a template only, use None to represent passthrough bounds
if isinstance(template_or_tuple, (AttributeTemplate, LinkByUID)):
return [template_or_tuple, None]
return (template_or_tuple, None)
# if given a (template, bounds) pair,
# check that the bounds is consistent with that of the template
elif isinstance(template_or_tuple, (tuple, list)):
Expand All @@ -78,5 +78,5 @@ def _homogenize_ranges(
if isinstance(first, AttributeTemplate) and isinstance(second, BaseBounds):
if not first.bounds.contains(second):
raise ValueError("Range and template are inconsistent")
return [first, second]
return (first, second)
raise TypeError("Expected a template or (template, bounds) tuple") # pragma: no cover
20 changes: 7 additions & 13 deletions gemd/entity/template/has_condition_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
__all__ = ["HasConditionTemplates"]
BaseEntityType = TypeVar("BaseEntityType", bound="BaseEntity") # noqa: F821
ConditionType = TypeVar("ConditionType", bound="Condition") # noqa: F821
_ConditionTemplateType = Union[ConditionTemplate, LinkByUID]
_ConditionPairType = Tuple[_ConditionTemplateType, Optional[BaseBounds]]
_ConditionListPairType = List[Optional[Union[_ConditionTemplateType, BaseBounds]]]
_ConditionInputType = Union[_ConditionTemplateType, _ConditionPairType, _ConditionListPairType]


class HasConditionTemplates(HasDependencies):
Expand All @@ -27,18 +31,13 @@ class HasConditionTemplates(HasDependencies):

def __init__(
self,
conditions: Iterable[
Union[
Union[ConditionTemplate, LinkByUID],
Tuple[Union[ConditionTemplate, LinkByUID], Optional[BaseBounds]],
]
],
conditions: Iterable[_ConditionInputType],
):
self._conditions = None
self.conditions = conditions

@property
def conditions(self) -> List[Union[ConditionTemplate, LinkByUID]]:
def conditions(self) -> List[_ConditionPairType]:
"""Get the list of condition template/bounds tuples.

Returns
Expand All @@ -52,12 +51,7 @@ def conditions(self) -> List[Union[ConditionTemplate, LinkByUID]]:
@conditions.setter
def conditions(
self,
conditions: Iterable[
Union[
Union[ConditionTemplate, LinkByUID],
Tuple[Union[ConditionTemplate, LinkByUID], Optional[BaseBounds]],
]
],
conditions: Iterable[_ConditionInputType],
):
"""Set the list of condition templates.

Expand Down
20 changes: 7 additions & 13 deletions gemd/entity/template/has_parameter_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
__all__ = ["HasParameterTemplates"]
ParameterType = TypeVar("ParameterType", bound="Parameter") # noqa: F821
BaseEntityType = TypeVar("BaseEntityType", bound="BaseEntity") # noqa: F821
_ParameterTemplateType = Union[ParameterTemplate, LinkByUID]
_ParameterPairType = Tuple[_ParameterTemplateType, Optional[BaseBounds]]
_ParameterListPairType = List[Optional[Union[_ParameterTemplateType, BaseBounds]]]
_ParameterInputType = Union[_ParameterTemplateType, _ParameterPairType, _ParameterListPairType]


class HasParameterTemplates(HasDependencies):
Expand All @@ -27,18 +31,13 @@ class HasParameterTemplates(HasDependencies):

def __init__(
self,
parameters: Iterable[
Union[
Union[ParameterTemplate, LinkByUID],
Tuple[Union[ParameterTemplate, LinkByUID], Optional[BaseBounds]],
]
],
parameters: Iterable[_ParameterInputType],
):
self._parameters = None
self.parameters = parameters

@property
def parameters(self) -> List[Union[ParameterTemplate, LinkByUID]]:
def parameters(self) -> List[_ParameterPairType]:
"""Get the list of parameter template/bounds tuples.

Returns
Expand All @@ -52,12 +51,7 @@ def parameters(self) -> List[Union[ParameterTemplate, LinkByUID]]:
@parameters.setter
def parameters(
self,
parameters: Iterable[
Union[
Union[ParameterTemplate, LinkByUID],
Tuple[Union[ParameterTemplate, LinkByUID], Optional[BaseBounds]],
]
],
parameters: Iterable[_ParameterInputType],
):
"""Set the list of parameter templates.

Expand Down
20 changes: 7 additions & 13 deletions gemd/entity/template/has_property_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"PropertyAndConditionsType",
bound="PropertyAndConditions", # noqa: F821
)
_PropertyTemplateType = Union[PropertyTemplate, LinkByUID]
_PropertyPairType = Tuple[_PropertyTemplateType, Optional[BaseBounds]]
_PropertyListPairType = List[Optional[Union[_PropertyTemplateType, BaseBounds]]]
_PropertyInputType = Union[_PropertyTemplateType, _PropertyPairType, _PropertyListPairType]


class HasPropertyTemplates(HasDependencies):
Expand All @@ -31,18 +35,13 @@ class HasPropertyTemplates(HasDependencies):

def __init__(
self,
properties: Iterable[
Union[
Union[PropertyTemplate, LinkByUID],
Tuple[Union[PropertyTemplate, LinkByUID], Optional[BaseBounds]],
]
],
properties: Iterable[_PropertyInputType],
):
self._properties = None
self.properties = properties

@property
def properties(self) -> List[Tuple[Union[PropertyTemplate, LinkByUID], Optional[BaseBounds]]]:
def properties(self) -> List[_PropertyPairType]:
"""Get the list of property template/bounds tuples.

Returns
Expand All @@ -56,12 +55,7 @@ def properties(self) -> List[Tuple[Union[PropertyTemplate, LinkByUID], Optional[
@properties.setter
def properties(
self,
properties: Iterable[
Union[
Union[PropertyTemplate, LinkByUID],
Tuple[Union[PropertyTemplate, LinkByUID], Optional[BaseBounds]],
]
],
properties: Iterable[_PropertyInputType],
):
"""Set the list of property templates.

Expand Down
8 changes: 4 additions & 4 deletions tests/entity/template/test_measurement_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_mixins():
for x in (obj.properties, obj.conditions, obj.parameters):
assert isinstance(x, ValidList)
for y in x:
assert isinstance(y, list)
assert isinstance(y, tuple)
assert len(y) == 2
assert isinstance(y[0], AttributeTemplate)
if y[1] is not None:
Expand Down Expand Up @@ -125,9 +125,9 @@ def test_mixins():
"Unmatched property and bad value didn't validate."
)

second.conditions[0][1] = None
second.parameters[0][1] = None
second.properties[0][1] = None
second.conditions[0] = (second.conditions[0][0], None)
second.parameters[0] = (second.parameters[0][0], None)
second.properties[0] = (second.properties[0][0], None)
assert second.validate_condition(Condition("Name", value=good_val)), (
"Condition and good value with passthrough didn't validate."
)
Expand Down
Loading