feat: add default inference behavior for basic types #13

Closed
gremond wants to merge 10 commits from basic_types_inference into develop
3 changed files with 15 additions and 16 deletions
Showing only changes of commit e006438cf4 - Show all commits

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

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.
variable.type = "string"
## XXX gérer le comportement suivant les versions

Not only 1.1, should be "else:"

Not only 1.1, should be "else:"
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