From e006438cf4ad247caa7840422c1e478fcc43278a Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 30 Apr 2024 10:33:55 +0200 Subject: [PATCH] feat: add default inference for basic types preparation of moving the calculation of the variable's type from the class Variable to a dynamic calculation in the annotations (mainly in `annotator/value.py`) --- src/rougail/annotator/value.py | 3 +++ src/rougail/convert.py | 20 +++++++------------- src/rougail/object_model.py | 8 +++++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/rougail/annotator/value.py b/src/rougail/annotator/value.py index 9f5d595ff..b2e04b0ad 100644 --- a/src/rougail/annotator/value.py +++ b/src/rougail/annotator/value.py @@ -66,6 +66,9 @@ class Annotator(Walk): # pylint: disable=R0903 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)] diff --git a/src/rougail/convert.py b/src/rougail/convert.py index d98f76d03..c5024874f 100644 --- a/src/rougail/convert.py +++ b/src/rougail/convert.py @@ -55,7 +55,7 @@ from .object_model import ( CONVERT_OPTION, Family, Dynamic, - _Variable, + Variable, Choice, SymLink, CALCULATION_TYPES, @@ -98,7 +98,7 @@ class Property: class Paths: def __init__(self) -> None: - self._data: Dict[str, Union[_Variable, Family]] = {} + self._data: Dict[str, Union[Variable, Family]] = {} self._dynamics: List[str] = [] self.path_prefix = None @@ -167,7 +167,7 @@ class Paths: def __getitem__( self, path: str, - ) -> Union[Family, _Variable]: + ) -> Union[Family, Variable]: if not path in self._data: raise AttributeError(f"cannot find variable or family {path}") return self._data[path] @@ -248,18 +248,10 @@ class ParserVariable: self.is_init = False super().__init__() - def get_variable(self): - - class Variable(_Variable): - #type: Literal[*convert_options] = convert_options[0] - type: str = self.convert_options[0] - - return Variable - def init(self): if self.is_init: return - self.variable = self.get_variable() + self.variable = Variable hint = get_type_hints(self.dynamic) self.family_types = hint["type"].__args__ # pylint: disable=W0201 self.family_attrs = frozenset( # pylint: disable=W0201 @@ -605,6 +597,7 @@ class ParserVariable: obj, filename, family_is_dynamic, + version ) if family_is_leadership: if first_variable: @@ -680,6 +673,7 @@ class ParserVariable: variable: dict, filename: str, family_is_dynamic: bool, + version: str ) -> None: """Add a new variable""" if not isinstance(filename, list): @@ -724,7 +718,7 @@ class ParserVariable: ############################################################################################### def set_name( self, - obj: Union[_Variable, Family], + obj: Union[Variable, Family], option_prefix: str, ): """Set Tiramisu object name""" diff --git a/src/rougail/object_model.py b/src/rougail/object_model.py index 42816d6a6..224139de8 100644 --- a/src/rougail/object_model.py +++ b/src/rougail/object_model.py @@ -422,7 +422,7 @@ class Dynamic(Family): dynamic: Optional[BASETYPE_CALC] -class _Variable(BaseModel): +class Variable(BaseModel): name: str description: Optional[str] = None default: Union[List[BASETYPE_CALC], BASETYPE_CALC] = None @@ -439,11 +439,13 @@ class _Variable(BaseModel): test: Optional[list] = None xmlfiles: List[str] = [] path: str + # type will be set dynamically in `annotator/value.py`, default is None + type: str = None model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True) -class Choice(_Variable): +class Choice(Variable): type: Literal["choice"] = "choice" choices: Union[List[BASETYPE_CALC], Calculation] @@ -451,7 +453,7 @@ class Choice(_Variable): class SymLink(BaseModel): name: str type: Literal["symlink"] = "symlink" - opt: _Variable + opt: Variable xmlfiles: List[str] = [] path: str