feat: add default inference behavior for basic types #13
2 changed files with 23 additions and 22 deletions
|
@ -76,6 +76,9 @@ class Annotator(Walk): # pylint: disable=R0903
|
|||
|
||||
def convert_variable(self):
|
||||
"""convert variable"""
|
||||
# default type inference from a default value with :term:`basic types`
|
||||
basic_types = {str: "string", int: "number", bool: "boolean", float: "float"}
|
||||
|
||||
for variable in self.get_variables():
|
||||
if variable.type == "symlink":
|
||||
continue
|
||||
|
@ -86,28 +89,26 @@ class Annotator(Walk): # pylint: disable=R0903
|
|||
variable.type = "choice"
|
||||
self._convert_variable(variable)
|
||||
|
||||
multi = self.objectspace.multis.get(variable.path, False)
|
||||
# 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"
|
||||
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)]
|
||||
elif isinstance(variable.default, list):
|
||||
if variable.multi is None or variable.multi is False:
|
||||
variable.multi = True
|
||||
multi = self.objectspace.multis.get(variable.path, False)
|
||||
if variable.version == "1.0":
|
||||
# - version: 1.0, the default value is of type "string" by default
|
||||
if variable.type is None:
|
||||
variable.type = basic_types[type(variable.default[0])]
|
||||
#else:
|
||||
# variable.type = "string"
|
||||
# let's update the "multi" attribute in the tiramisu cache then
|
||||
if variable.multi == True:
|
||||
self.objectspace.multis[variable.path] = True
|
||||
variable.type = "string"
|
||||
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)]
|
||||
elif isinstance(variable.default, list):
|
||||
if variable.multi is None or variable.multi is False:
|
||||
variable.multi = True
|
||||
if variable.type is None:
|
||||
variable.type = basic_types[type(variable.default[0])]
|
||||
#else:
|
||||
# variable.type = "string"
|
||||
# let's update the "multi" attribute in the tiramisu cache then
|
||||
if variable.multi == True:
|
||||
self.objectspace.multis[variable.path] = True
|
||||
|
||||
def _convert_variable(
|
||||
self,
|
||||
|
|
|
@ -442,7 +442,7 @@ 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]
|
||||
choices: Union[None, List[BASETYPE_CALC], Calculation] = None
|
||||
|
||||
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
||||
|
||||
|
|
Loading…
Reference in a new issue