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"}
# 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)]

View file

@ -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"""

View file

@ -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