Skip to content

Commit 4fcab81

Browse files
committed
Phase 4
Signed-off-by: Christian Vetter <christian.vetter@here.com>
1 parent da45f7a commit 4fcab81

11 files changed

Lines changed: 75 additions & 61 deletions

File tree

flatdata-generator/flatdata/generator/generators/cpp.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from jinja2 import Environment
77

8+
from typing import Any
9+
810
from flatdata.generator.tree.nodes.resources import Vector, Multivector, Instance, RawData, BoundResource, \
911
ResourceBase, Archive as ArchiveResource
1012
from flatdata.generator.tree.nodes.trivial import Structure, Enumeration, Constant, Field
@@ -24,12 +26,12 @@ def supported_nodes(self) -> list[type]:
2426
def _populate_environment(self, env: Environment) -> None:
2527
env.filters["cpp_doc"] = lambda value: value
2628

27-
def _safe_cpp_string_line(value):
29+
def _safe_cpp_string_line(value: str) -> str:
2830
return value.replace('\\', '\\\\').replace('"', r'\"')
2931

3032
env.filters["safe_cpp_string_line"] = _safe_cpp_string_line
3133

32-
def _cpp_base_type(flatdata_type):
34+
def _cpp_base_type(flatdata_type: Any) -> str:
3335
type_map = {
3436
"bool": "bool",
3537
"i8": "int8_t",
@@ -43,28 +45,28 @@ def _cpp_base_type(flatdata_type):
4345
}
4446
if flatdata_type.name in type_map:
4547
return type_map[flatdata_type.name]
46-
return flatdata_type.name.replace("@@", "::").replace("@", "::")
48+
return str(flatdata_type.name.replace("@@", "::").replace("@", "::"))
4749

4850
env.filters["cpp_base_type"] = _cpp_base_type
4951

50-
def _to_type_params(refs):
52+
def _to_type_params(refs: list[Any]) -> str:
5153
return ', '.join([ref.node.path_with("::") for ref in refs])
5254

5355
env.filters["to_type_params"] = _to_type_params
5456

55-
def _snake_to_upper_camel_case(expr):
57+
def _snake_to_upper_camel_case(expr: str) -> str:
5658
return ''.join(p.title() for p in expr.split('_'))
5759

5860
env.filters["snake_to_upper_camel_case"] = _snake_to_upper_camel_case
5961

60-
def _typedef_name(entity, extra_suffix=""):
62+
def _typedef_name(entity: Any, extra_suffix: str = "") -> str:
6163
assert isinstance(entity, (Field, ResourceBase)), "Got: %s" % entity.__class__
6264
return _snake_to_upper_camel_case(entity.name) + extra_suffix + "Type"
6365

6466
env.filters["typedef_name"] = _typedef_name
6567

66-
def _optional_typedef_usage(resource, extra_suffix=""):
67-
def _wrap_in_optional(declaration):
68+
def _optional_typedef_usage(resource: Any, extra_suffix: str = "") -> str:
69+
def _wrap_in_optional(declaration: str) -> str:
6870
if resource.optional:
6971
return "boost::optional< %s >" % declaration
7072
return declaration
@@ -73,7 +75,7 @@ def _wrap_in_optional(declaration):
7375

7476
env.filters["archive_typedef_usage"] = _optional_typedef_usage
7577

76-
def _resource_provides_incremental_builder(resource):
78+
def _resource_provides_incremental_builder(resource: Any) -> bool:
7779
assert isinstance(resource, ResourceBase)
7880
if isinstance(resource, Instance):
7981
return False
@@ -88,7 +90,7 @@ def _resource_provides_incremental_builder(resource):
8890
env.filters[
8991
"resource_provides_incremental_builder"] = _resource_provides_incremental_builder
9092

91-
def provides_setter(resource):
93+
def provides_setter(resource: Any) -> bool:
9294
assert isinstance(resource, ResourceBase)
9395
if isinstance(resource, Instance):
9496
return True

flatdata-generator/flatdata/generator/generators/dot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from flatdata.generator.tree.nodes.archive import Archive
77
from . import BaseGenerator
88

9+
from typing import Any
10+
911
from jinja2 import Environment
1012

1113
SCOPE_SEPARATOR = "__"
@@ -21,9 +23,9 @@ def __init__(self) -> None:
2123
def _populate_environment(self, env: Environment) -> None:
2224
env.autoescape = True
2325

24-
def _field_value_type(field):
25-
type_name = field.type.name.replace("@@", ".").replace("@", ".")
26-
namespace_name = field.parent.parent.path
26+
def _field_value_type(field: Any) -> str:
27+
type_name = str(field.type.name).replace("@@", ".").replace("@", ".")
28+
namespace_name = str(field.parent.parent.path)
2729
if type_name.startswith(namespace_name):
2830
type_name = type_name[len(namespace_name):]
2931
if type_name.startswith("."):

flatdata-generator/flatdata/generator/generators/flatdata.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from jinja2 import Environment
77

8+
from typing import Any
9+
810
from flatdata.generator.tree.nodes.resources import BoundResource
911
from flatdata.generator.tree.nodes.trivial import Structure, Enumeration, Constant
1012
from flatdata.generator.tree.nodes.archive import Archive
@@ -22,19 +24,19 @@ def supported_nodes(self) -> list[type]:
2224
return [Structure, Archive, Constant, Enumeration]
2325

2426
def _populate_environment(self, env: Environment) -> None:
25-
def _is_builtin(node):
27+
def _is_builtin(node: Any) -> bool:
2628
for namespace in SyntaxTree.namespaces(node):
2729
if namespace.name == "_builtin":
2830
return True
2931
return False
3032
env.filters["filter_builtin"] = lambda l: [x for x in l if not _is_builtin(x)]
3133

32-
def _field_type(flatdata_type):
34+
def _field_type(flatdata_type: str) -> str:
3335
return flatdata_type.replace("@@", ".").replace("@", ".")
3436

3537
env.filters["field_type"] = _field_type
3638

37-
def _to_type_params(refs):
39+
def _to_type_params(refs: list[Any]) -> str:
3840
return ', '.join([ref.node.path_with(".") for ref in refs if not _is_builtin(ref.node)])
3941

4042
env.filters["to_type_params"] = _to_type_params

flatdata-generator/flatdata/generator/generators/go.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
'''
55
from jinja2 import Environment
66

7+
from typing import Any
8+
79
from flatdata.generator.tree.nodes.archive import Archive
810
from flatdata.generator.tree.nodes.node import Node
911
from flatdata.generator.tree.nodes.resources import Instance, Vector, Multivector, RawData
@@ -23,25 +25,25 @@ def supported_nodes(self) -> list[type]:
2325
return [Structure, Archive, Constant]
2426

2527
def _populate_environment(self, env: Environment) -> None:
26-
def _decorate_archive_type(value):
28+
def _decorate_archive_type(value: Node) -> str:
2729
assert isinstance(value, Node)
28-
return value.name
30+
return str(value.name)
2931

30-
def to_go_doc(value):
32+
def to_go_doc(value: Any) -> str:
3133
lines = value.doc.splitlines()
3234
return '\n'.join(["// " + s for s in lines if len(s) != 0])
3335

34-
def type_mapping(flatdata_type, _struct):
36+
def type_mapping(flatdata_type: str, _struct: Any) -> str:
3537
if is_bool(flatdata_type):
3638
return "uint8"
3739
return go_mapping(flatdata_type)
3840

39-
def type_mapping_with_bool(flatdata_type):
41+
def type_mapping_with_bool(flatdata_type: str) -> str:
4042
if is_bool(flatdata_type):
4143
return "bool"
4244
return go_mapping(flatdata_type)
4345

44-
def go_mapping(flatdata_type):
46+
def go_mapping(flatdata_type: str) -> str:
4547
return {
4648
"i8": "int8",
4749
"u8": "uint8",
@@ -53,15 +55,15 @@ def go_mapping(flatdata_type):
5355
"i64": "int64"
5456
}[flatdata_type]
5557

56-
def is_bool(flatdata_type):
58+
def is_bool(flatdata_type: str) -> bool:
5759
return flatdata_type == "bool"
5860

59-
def to_go_case(name, exported=True):
61+
def to_go_case(name: str, exported: bool = True) -> str:
6062
if "_" in name:
6163
name = "".join(part.title() for part in name.split("_"))
6264
return (str.upper if exported else str.lower)(str(name[0])) + str(name[1:])
6365

64-
def to_initializer(resource, tree):
66+
def to_initializer(resource: Any, tree: Any) -> str:
6567
if isinstance(resource, Instance):
6668
return _decorate_archive_type(resource.referenced_structures[0].node)
6769
if isinstance(resource, Vector):
@@ -77,10 +79,10 @@ def to_initializer(resource, tree):
7779
return "None"
7880
raise ValueError("Unknown resource type: %s" % (resource.__class__))
7981

80-
def get_types_for_multivector(resource, _tree):
82+
def get_types_for_multivector(resource: Any, _tree: Any) -> list[str]:
8183
return [_decorate_archive_type(t.node) for t in resource.referenced_structures]
8284

83-
def contains_archive_resource(tree):
85+
def contains_archive_resource(tree: Any) -> bool:
8486
for child in tree.root.children[0].children:
8587
for res in child.children:
8688
if isinstance(res, ArchiveResource):

flatdata-generator/flatdata/generator/generators/python.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
'''
55
from jinja2 import Environment
66

7+
from typing import Any
8+
79
from flatdata.generator.tree.nodes.resources import Instance, Vector, Multivector, RawData
810
from flatdata.generator.tree.nodes.resources.archive import Archive as ArchiveResource
911
from flatdata.generator.tree.nodes.trivial import Structure
@@ -22,18 +24,18 @@ def supported_nodes(self) -> list[type]:
2224
return [Structure, Archive]
2325

2426
def _populate_environment(self, env: Environment) -> None:
25-
def _decorate_archive_type(tree, value):
27+
def _decorate_archive_type(tree: Any, value: Node) -> str:
2628
assert isinstance(value, Node)
27-
return tree.namespace_path(value, "_") + "_" + value.name
29+
return str(tree.namespace_path(value, "_") + "_" + value.name)
2830

29-
def to_python_doc(value):
31+
def to_python_doc(value: str) -> str:
3032
return '\n'.join(
3133
["# " + line.replace('/**', '', 1).replace('*/', '', 1).replace(" *", '',
3234
1).replace("//", "",
3335
1) for
3436
line in value.splitlines()])
3537

36-
def to_container(resource):
38+
def to_container(resource: Any) -> str:
3739
if isinstance(resource, Instance):
3840
return "flatdata.resources.Instance"
3941
if isinstance(resource, Vector):
@@ -46,22 +48,22 @@ def to_container(resource):
4648
return "flatdata.archive.Archive"
4749
raise ValueError("Unknown resource type: %s" % (resource.__class__))
4850

49-
def to_initializer(resource, tree):
51+
def to_initializer(resource: Any, tree: Any) -> str:
5052
if isinstance(resource, Instance):
51-
return _decorate_archive_type(tree, resource.referenced_structures[0].node)
53+
return str(_decorate_archive_type(tree, resource.referenced_structures[0].node))
5254
if isinstance(resource, Vector):
53-
return _decorate_archive_type(tree, resource.referenced_structures[0].node)
55+
return str(_decorate_archive_type(tree, resource.referenced_structures[0].node))
5456
if isinstance(resource, Multivector):
5557
return "[{}]".format(
5658
','.join([_decorate_archive_type(tree, t.node) for t in
5759
resource.referenced_structures]))
5860
if isinstance(resource, ArchiveResource):
59-
return _decorate_archive_type(tree, resource.children[0].node) # type: ignore[attr-defined] # child is an ArchiveReference which has .node
61+
return str(_decorate_archive_type(tree, resource.children[0].node)) # type: ignore[attr-defined] # child is an ArchiveReference which has .node
6062
if isinstance(resource, RawData):
6163
return "None"
6264
raise ValueError("Unknown resource type: %s" % (resource.__class__))
6365

64-
def to_dtype(field):
66+
def to_dtype(field: Any) -> str:
6567
type_map = {
6668
"bool": "?",
6769
"i8": "b",
@@ -75,9 +77,9 @@ def to_dtype(field):
7577
}
7678
if field.type.name in type_map:
7779
return type_map[field.type.name]
78-
return type_map[field.type_reference.node.type.name]
80+
return str(type_map[field.type_reference.node.type.name])
7981

80-
def _safe_py_string_line(value):
82+
def _safe_py_string_line(value: str) -> str:
8183
return value.replace('\\', '\\\\').replace('"', r'\"')
8284

8385
env.filters["safe_py_string_line"] = _safe_py_string_line

flatdata-generator/flatdata/generator/generators/rust.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from jinja2 import Environment
88

9+
from typing import Any
10+
911
from flatdata.generator.tree.nodes.resources import (Vector, Multivector, Instance, RawData, BoundResource,
1012
Archive as ArchiveResource)
1113
from flatdata.generator.tree.nodes.trivial import Structure, Constant, Enumeration
@@ -43,18 +45,18 @@ def _format_numeric_literal(value: str) -> str:
4345
return value
4446

4547
def _populate_environment(self, env: Environment) -> None:
46-
def _camel_to_snake_case(expr):
48+
def _camel_to_snake_case(expr: str) -> str:
4749
step1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', expr)
4850
return re.sub('([a-z0-9])(A-Z)', r'\1_\2', step1).lower()
4951

5052
env.filters["camel_to_snake_case"] = _camel_to_snake_case
5153

52-
def _snake_to_upper_camel_case(expr):
54+
def _snake_to_upper_camel_case(expr: str) -> str:
5355
return ''.join(p.title() for p in expr.split('_'))
5456

5557
env.filters["snake_to_upper_camel_case"] = _snake_to_upper_camel_case
5658

57-
def _rust_doc(expr):
59+
def _rust_doc(expr: str) -> str:
5860
lines = [
5961
re.sub(r'^[ \t]*(/\*\*\s?|/\*\s?|\*/|\*\s?)(.*?)\s*(\*/)?$',
6062
r"/// \2", line).strip()
@@ -70,24 +72,24 @@ def _rust_doc(expr):
7072

7173
env.filters["rust_doc"] = _rust_doc
7274

73-
def _escape_rust_keywords(expr):
75+
def _escape_rust_keywords(expr: str) -> str:
7476
if expr in self.RESERVED_KEYWORDS:
7577
return "{}_".format(expr)
7678
return expr
7779

78-
def _field_type(field):
80+
def _field_type(field: Any) -> str:
7981
if isinstance(field.type, EnumType):
8082
return "{}".format(
8183
_fully_qualified_name(field.parent, field.type_reference.node))
8284
return "{}".format(field.type.name)
8385

84-
def _primitive_type(field):
86+
def _primitive_type(field: Any) -> str:
8587
if isinstance(field.type, EnumType):
8688
return "{}".format(field.type_reference.node.type.name)
8789
return "{}".format(field.type.name)
8890

89-
def _fully_qualified_name(current, node):
90-
return "::".join((current.path_depth() - 1) * ["super"]) + node.path_with("::")
91+
def _fully_qualified_name(current: Any, node: Any) -> str:
92+
return "::".join((current.path_depth() - 1) * ["super"]) + str(node.path_with("::"))
9193

9294
env.globals["fully_qualified_name"] = _fully_qualified_name
9395
env.filters["escape_rust_keywords"] = _escape_rust_keywords

flatdata-generator/flatdata/generator/tree/nodes/resources/multivector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def index_reference(self) -> BuiltinStructureReference:
4040

4141
@property
4242
def builtins(self) -> list[Structure]:
43-
class MemberDict(dict):
44-
def __getattr__(self, attr):
43+
class MemberDict(dict): # type: ignore[type-arg] # intentionally unparameterized: mimics dynamic attribute access
44+
def __getattr__(self, attr: str) -> Any:
4545
return self.get(attr)
4646
decorations = [MemberDict({"range" : MemberDict({"name":"range"})})]
4747
field = MemberDict({"decorations":decorations, "name":"value", "width":self._width, "type":"u64"})

flatdata-generator/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ namespace_packages = true
4747
explicit_package_bases = true
4848
warn_return_any = true
4949
warn_unused_configs = true
50-
disallow_untyped_defs = false
50+
disallow_untyped_defs = true
5151
check_untyped_defs = true
5252
warn_redundant_casts = true
5353
warn_unused_ignores = true
5454
no_implicit_optional = true
5555
strict_equality = true
56+
disallow_any_generics = true
5657

5758
[[tool.mypy.overrides]]
5859
module = "Levenshtein"

flatdata-py/flatdata/lib/archive_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ def __set_multivector(self, storage: Any, name: str, value: list[list[dict[str,
197197
for index, obj_type in enumerate(initializer_list[1:]):
198198
initializers[obj_type._NAME] = (index, obj_type)
199199

200-
def valid_structure_name(_obj):
200+
def valid_structure_name(_obj: dict[str, Any]) -> bool:
201201
return _obj['name'] in [_initializer._NAME for _initializer in initializer_list[1:]]
202202

203-
def validate_fields(_obj):
203+
def validate_fields(_obj: dict[str, Any]) -> None:
204204
matched_obj_list = [
205205
_initializer for _initializer in initializer_list[1:] \
206206
if _initializer._NAME == _obj['name']]

0 commit comments

Comments
 (0)