feat: add default inference behavior for basic types #13

Closed
gremond wants to merge 10 commits from basic_types_inference into develop
4 changed files with 17 additions and 9 deletions
Showing only changes of commit 035fd7bd7e - Show all commits

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

This line is not correct to me.

Example:

my_var:
  type: string
  default: 3

is an invalid dictionary.

This line is not correct to me. Example: ``` my_var: type: string default: 3 ``` is an invalid dictionary.
if variable.type is None:
variable.type = "string"

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:

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

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):