Skip to content
Snippets Groups Projects
Commit 7312d68d authored by Derval Guillaume's avatar Derval Guillaume
Browse files

Fix a bug in tree modifier + typing

parent 4f502cce
No related branches found
No related tags found
No related merge requests found
import typing
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Optional from typing import Optional
...@@ -15,4 +16,7 @@ class GBOMLObject: ...@@ -15,4 +16,7 @@ class GBOMLObject:
@dataclass @dataclass
class NamedGBOMLObject(GBOMLObject): class NamedGBOMLObject(GBOMLObject):
name: str name: str
\ No newline at end of file
AnyGBOMLObject = typing.TypeVar('AnyGBOMLObject', bound=GBOMLObject)
\ No newline at end of file
...@@ -46,7 +46,7 @@ from gboml.ast import * ...@@ -46,7 +46,7 @@ from gboml.ast import *
from gboml.tools.tree_modifier import modify from gboml.tools.tree_modifier import modify
def remove_redundant_definitions(elem: GBOMLObject) -> GBOMLObject: def remove_redundant_definitions(elem: AnyGBOMLObject) -> AnyGBOMLObject:
return modify(elem, {Node: _modify_node, HyperEdge: _modify_hyperedge}) return modify(elem, {Node: _modify_node, HyperEdge: _modify_hyperedge})
......
...@@ -124,7 +124,8 @@ def _modify_gbomlobject(obj, by): ...@@ -124,7 +124,8 @@ def _modify_gbomlobject(obj, by):
# if we now what to do with the current object, let's do it # if we now what to do with the current object, let's do it
for x in family_list[obj.__class__]: for x in family_list[obj.__class__]:
if x in by: if x in by:
return by[x](obj) obj = by[x](obj)
break
# and now the hard part # and now the hard part
interesting_fields = set() interesting_fields = set()
...@@ -141,7 +142,10 @@ def _modify_gbomlobject(obj, by): ...@@ -141,7 +142,10 @@ def _modify_gbomlobject(obj, by):
return obj return obj
def modify(element: typing.Any, by: dict[typing.Type[GBOMLObject], typing.Callable[[GBOMLObject], GBOMLObject]]): T = typing.TypeVar('T')
def modify(element: T, by: dict[typing.Type[AnyGBOMLObject], typing.Callable[[AnyGBOMLObject], AnyGBOMLObject]]) -> T:
""" """
Recursively modifies a GBOMLGraph tree (or any part of it) according to rules set in the dict `by`. Recursively modifies a GBOMLGraph tree (or any part of it) according to rules set in the dict `by`.
`by` entries should be in the form `(cls: fun)`, where cls is a class derivating from GBOMLObject and `by` entries should be in the form `(cls: fun)`, where cls is a class derivating from GBOMLObject and
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment