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,
) -> None:
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"}
if variable.version == "1.0":
# - version: 1.0, the default value is of type "string" by default
if variable.type is None:
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
if multi is False and variable.type is None:
if type(variable.default) in basic_types:
variable.type = basic_types[type(variable.default)]
else:
# XXX FIXME strange. weird. not good
variable.type = "string"
if variable.type is None:
variable.type = "string"
# a boolean must have value, the default value is "True"
if variable.type == "boolean" and multi is False and variable.default is None:
variable.default = True

View file

@ -79,6 +79,11 @@ class Annotator(Walk): # pylint: disable=R0903
for variable in self.get_variables():
if variable.type == "symlink":
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)
def _convert_variable(

View file

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

View file

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