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`)
This commit is contained in:
gwen 2024-04-30 10:33:55 +02:00
parent 463b57ea2d
commit e006438cf4
3 changed files with 15 additions and 16 deletions

View file

@ -66,6 +66,9 @@ class Annotator(Walk): # pylint: disable=R0903
types = {str: "string", int: "number", bool: "boolean", float: "float"} types = {str: "string", int: "number", bool: "boolean", float: "float"}
# variable's type inference with a default value with a basic type # 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 multi is False and variable.type == "string" and type(variable.default) != str:
if type(variable.default) in types: if type(variable.default) in types:
variable.type = types[type(variable.default)] variable.type = types[type(variable.default)]

View file

@ -55,7 +55,7 @@ from .object_model import (
CONVERT_OPTION, CONVERT_OPTION,
Family, Family,
Dynamic, Dynamic,
_Variable, Variable,
Choice, Choice,
SymLink, SymLink,
CALCULATION_TYPES, CALCULATION_TYPES,
@ -98,7 +98,7 @@ class Property:
class Paths: class Paths:
def __init__(self) -> None: def __init__(self) -> None:
self._data: Dict[str, Union[_Variable, Family]] = {} self._data: Dict[str, Union[Variable, Family]] = {}
self._dynamics: List[str] = [] self._dynamics: List[str] = []
self.path_prefix = None self.path_prefix = None
@ -167,7 +167,7 @@ class Paths:
def __getitem__( def __getitem__(
self, self,
path: str, path: str,
) -> Union[Family, _Variable]: ) -> Union[Family, Variable]:
if not path in self._data: if not path in self._data:
raise AttributeError(f"cannot find variable or family {path}") raise AttributeError(f"cannot find variable or family {path}")
return self._data[path] return self._data[path]
@ -248,18 +248,10 @@ class ParserVariable:
self.is_init = False self.is_init = False
super().__init__() 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): def init(self):
if self.is_init: if self.is_init:
return return
self.variable = self.get_variable() self.variable = Variable
hint = get_type_hints(self.dynamic) hint = get_type_hints(self.dynamic)
self.family_types = hint["type"].__args__ # pylint: disable=W0201 self.family_types = hint["type"].__args__ # pylint: disable=W0201
self.family_attrs = frozenset( # pylint: disable=W0201 self.family_attrs = frozenset( # pylint: disable=W0201
@ -605,6 +597,7 @@ class ParserVariable:
obj, obj,
filename, filename,
family_is_dynamic, family_is_dynamic,
version
) )
if family_is_leadership: if family_is_leadership:
if first_variable: if first_variable:
@ -680,6 +673,7 @@ class ParserVariable:
variable: dict, variable: dict,
filename: str, filename: str,
family_is_dynamic: bool, family_is_dynamic: bool,
version: str
) -> None: ) -> None:
"""Add a new variable""" """Add a new variable"""
if not isinstance(filename, list): if not isinstance(filename, list):
@ -724,7 +718,7 @@ class ParserVariable:
############################################################################################### ###############################################################################################
def set_name( def set_name(
self, self,
obj: Union[_Variable, Family], obj: Union[Variable, Family],
option_prefix: str, option_prefix: str,
): ):
"""Set Tiramisu object name""" """Set Tiramisu object name"""

View file

@ -422,7 +422,7 @@ class Dynamic(Family):
dynamic: Optional[BASETYPE_CALC] dynamic: Optional[BASETYPE_CALC]
class _Variable(BaseModel): class Variable(BaseModel):
name: str name: str
description: Optional[str] = None description: Optional[str] = None
default: Union[List[BASETYPE_CALC], BASETYPE_CALC] = None default: Union[List[BASETYPE_CALC], BASETYPE_CALC] = None
@ -439,11 +439,13 @@ class _Variable(BaseModel):
test: Optional[list] = None test: Optional[list] = None
xmlfiles: List[str] = [] xmlfiles: List[str] = []
path: 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) model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
class Choice(_Variable): class Choice(Variable):
type: Literal["choice"] = "choice" type: Literal["choice"] = "choice"
choices: Union[List[BASETYPE_CALC], Calculation] choices: Union[List[BASETYPE_CALC], Calculation]
@ -451,7 +453,7 @@ class Choice(_Variable):
class SymLink(BaseModel): class SymLink(BaseModel):
name: str name: str
type: Literal["symlink"] = "symlink" type: Literal["symlink"] = "symlink"
opt: _Variable opt: Variable
xmlfiles: List[str] = [] xmlfiles: List[str] = []
path: str path: str