From baa1968b261b8e4896010ad3c5189794f0c46735 Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 30 Apr 2024 15:20:06 +0200 Subject: [PATCH] feat: default inference, calculation of the variable's type --- src/rougail/annotator/value.py | 25 ++++++++++++++----------- src/rougail/convert.py | 1 + src/rougail/object_model.py | 1 + 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/rougail/annotator/value.py b/src/rougail/annotator/value.py index b2e04b0ad..705052859 100644 --- a/src/rougail/annotator/value.py +++ b/src/rougail/annotator/value.py @@ -62,17 +62,20 @@ class Annotator(Walk): # pylint: disable=R0903 variable: dict, ) -> None: multi = self.objectspace.multis.get(variable.path, False) - - types = {str: "string", int: "number", bool: "boolean", float: "float"} - - # variable's type inference with a default value with a basic type - ## XXX hack: convert None to string - variable.type = "string" - ## XXX gérer le comportement suivant les versions - if multi is False and variable.type == "string" and type(variable.default) != str: - if type(variable.default) in types: - variable.type = types[type(variable.default)] - + # variable's 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": + # - 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" # 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 diff --git a/src/rougail/convert.py b/src/rougail/convert.py index c5024874f..11dd9c893 100644 --- a/src/rougail/convert.py +++ b/src/rougail/convert.py @@ -679,6 +679,7 @@ class ParserVariable: if not isinstance(filename, list): filename = [filename] variable["xmlfiles"] = filename + variable["version"] = version variable_type = self.get_family_or_variable_type(variable) obj = { "symlink": SymLink, diff --git a/src/rougail/object_model.py b/src/rougail/object_model.py index 224139de8..a9631dad7 100644 --- a/src/rougail/object_model.py +++ b/src/rougail/object_model.py @@ -439,6 +439,7 @@ class Variable(BaseModel): test: Optional[list] = None xmlfiles: List[str] = [] path: str + version: str # type will be set dynamically in `annotator/value.py`, default is None type: str = None