feat: set automatically the choice type

This commit is contained in:
gwen 2024-05-21 15:38:41 +02:00
parent baa1968b26
commit 035fd7bd7e
4 changed files with 17 additions and 9 deletions

View file

@ -62,20 +62,22 @@ class Annotator(Walk): # pylint: disable=R0903
variable: dict, variable: dict,
) -> None: ) -> None:
multi = self.objectspace.multis.get(variable.path, False) multi = self.objectspace.multis.get(variable.path, False)
# variable's type inference from a default value with :term:`basic types`
# default type inference from a default value with :term:`basic types`
basic_types = {str: "string", int: "number", bool: "boolean", float: "float"} basic_types = {str: "string", int: "number", bool: "boolean", float: "float"}
if variable.version == "1.0": if variable.version == "1.0":
# - version: 1.0, the default value is of type "string" by default # - version: 1.0, the default value is of type "string" by default
if variable.type is None: if variable.type is None:
variable.type = "string" variable.type = "string"
elif variable.version == "1.1": else: #if variable.version != "1.0":
# - version: 1.1, the default value has no type by default # - version: 1.1, the default value has no type by default
if multi is False and variable.type is None: if multi is False and variable.type is None:
if type(variable.default) in basic_types: if type(variable.default) in basic_types:
variable.type = basic_types[type(variable.default)] variable.type = basic_types[type(variable.default)]
else: else:
# XXX FIXME strange. weird. not good if variable.type is None:
variable.type = "string" variable.type = "string"
# a boolean must have value, the default value is "True" # a boolean must have value, the default value is "True"
if variable.type == "boolean" and multi is False and variable.default is None: if variable.type == "boolean" and multi is False and variable.default is None:
variable.default = True variable.default = True

View file

@ -79,6 +79,11 @@ class Annotator(Walk): # pylint: disable=R0903
for variable in self.get_variables(): for variable in self.get_variables():
if variable.type == "symlink": if variable.type == "symlink":
continue continue
# choice type inference from the `choices` attribute
if variable.version != "1.0":
if variable.choices is not None:
if variable.type is None:
variable.type = "choice"
self._convert_variable(variable) self._convert_variable(variable)
def _convert_variable( def _convert_variable(

View file

@ -56,7 +56,7 @@ from .object_model import (
Family, Family,
Dynamic, Dynamic,
Variable, Variable,
Choice, #Choice,
SymLink, SymLink,
CALCULATION_TYPES, CALCULATION_TYPES,
Calculation, Calculation,
@ -237,7 +237,7 @@ class ParserVariable:
# #
self.family = Family self.family = Family
self.dynamic = Dynamic self.dynamic = Dynamic
self.choice = Choice self.choice = Variable #Choice
# #
self.exclude_imports = [] self.exclude_imports = []
self.informations = Informations() self.informations = Informations()

View file

@ -442,13 +442,14 @@ class Variable(BaseModel):
version: str version: str
# type will be set dynamically in `annotator/value.py`, default is None # type will be set dynamically in `annotator/value.py`, default is None
type: str = None type: str = None
choices: Union[None, List[BASETYPE_CALC], Calculation]
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True) model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
class Choice(Variable): #class Choice(Variable):
type: Literal["choice"] = "choice" # type: Literal["choice"] = "choice"
choices: Union[List[BASETYPE_CALC], Calculation] # choices: Union[List[BASETYPE_CALC], Calculation]
class SymLink(BaseModel): class SymLink(BaseModel):