feat: add default inference behavior for basic types #13
4 changed files with 17 additions and 9 deletions
|
@ -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"
|
||||
egarette
commented
Not only 1.1, should be "else:" Not only 1.1, should be "else:"
|
||||
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:
|
||||
egarette
commented
If variable.type is "number" it will override by "string". Multi value should be supported too (raise if type are different in this case). If variable.type is "number" it will override by "string".
Multi value should be supported too (raise if type are different in this case).
|
||||
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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue
This line is not correct to me.
Example:
is an invalid dictionary.