darabos commited on
Commit
19946be
·
1 Parent(s): 96431df

t -> type_hint, remove stray line.

Browse files
lynxkite-graph-analytics/src/lynxkite_graph_analytics/networkx_ops.py CHANGED
@@ -20,20 +20,18 @@ class UnsupportedParameterType(Exception):
20
  _UNSUPPORTED = object()
21
  _SKIP = object()
22
 
23
- nx.ladder_graph
24
 
25
-
26
- def doc_to_type(name: str, t: str) -> type:
27
- t = t.lower()
28
- t = re.sub("[(][^)]+[)]", "", t).strip().strip(".")
29
  if " " in name or "http" in name:
30
  return _UNSUPPORTED # Not a parameter type.
31
- if t.endswith(", optional"):
32
- w = doc_to_type(name, t.removesuffix(", optional").strip())
33
  if w is _UNSUPPORTED:
34
  return _SKIP
35
  return w if w is _SKIP else w | None
36
- if t in [
37
  "a digraph or multidigraph",
38
  "a graph g",
39
  "graph",
@@ -44,9 +42,9 @@ def doc_to_type(name: str, t: str) -> type:
44
  "nx.graph",
45
  "undirected graph",
46
  "undirected networkx graph",
47
- ] or t.startswith("networkx graph"):
48
  return nx.Graph
49
- elif t in [
50
  "digraph-like",
51
  "digraph",
52
  "directed graph",
@@ -55,47 +53,47 @@ def doc_to_type(name: str, t: str) -> type:
55
  "nx.digraph",
56
  ]:
57
  return nx.DiGraph
58
- elif t == "node":
59
  return _UNSUPPORTED
60
- elif t == '"node (optional)"':
61
  return _SKIP
62
- elif t == '"edge"':
63
  return _UNSUPPORTED
64
- elif t == '"edge (optional)"':
65
  return _SKIP
66
- elif t in ["class", "data type"]:
67
  return _UNSUPPORTED
68
- elif t in ["string", "str", "node label"]:
69
  return str
70
- elif t in ["string or none", "none or string", "string, or none"]:
71
  return str | None
72
- elif t in ["int", "integer"]:
73
  return int
74
- elif t in ["bool", "boolean"]:
75
  return bool
76
- elif t == "tuple":
77
  return _UNSUPPORTED
78
- elif t == "set":
79
  return _UNSUPPORTED
80
- elif t == "list of floats":
81
  return _UNSUPPORTED
82
- elif t == "list of floats or float":
83
  return float
84
- elif t in ["dict", "dictionary"]:
85
  return _UNSUPPORTED
86
- elif t == "scalar or dictionary":
87
  return float
88
- elif t == "none or dict":
89
  return _SKIP
90
- elif t in ["function", "callable"]:
91
  return _UNSUPPORTED
92
- elif t in [
93
  "collection",
94
  "container of nodes",
95
  "list of nodes",
96
  ]:
97
  return _UNSUPPORTED
98
- elif t in [
99
  "container",
100
  "generator",
101
  "iterable",
@@ -107,19 +105,19 @@ def doc_to_type(name: str, t: str) -> type:
107
  "list",
108
  ]:
109
  return _UNSUPPORTED
110
- elif t == "generator of sets":
111
  return _UNSUPPORTED
112
- elif t == "dict or a set of 2 or 3 tuples":
113
  return _UNSUPPORTED
114
- elif t == "set of 2 or 3 tuples":
115
  return _UNSUPPORTED
116
- elif t == "none, string or function":
117
  return str | None
118
- elif t == "string or function" and name == "weight":
119
  return str
120
- elif t == "integer, float, or none":
121
  return float | None
122
- elif t in [
123
  "float",
124
  "int or float",
125
  "integer or float",
@@ -130,13 +128,13 @@ def doc_to_type(name: str, t: str) -> type:
130
  "scalar",
131
  ]:
132
  return float
133
- elif t in ["integer or none", "int or none"]:
134
  return int | None
135
  elif name == "seed":
136
  return int | None
137
  elif name == "weight":
138
  return str
139
- elif t == "object":
140
  return _UNSUPPORTED
141
  return _SKIP
142
 
 
20
  _UNSUPPORTED = object()
21
  _SKIP = object()
22
 
 
23
 
24
+ def doc_to_type(name: str, type_hint: str) -> type:
25
+ type_hint = type_hint.lower()
26
+ type_hint = re.sub("[(][^)]+[)]", "", type_hint).strip().strip(".")
 
27
  if " " in name or "http" in name:
28
  return _UNSUPPORTED # Not a parameter type.
29
+ if type_hint.endswith(", optional"):
30
+ w = doc_to_type(name, type_hint.removesuffix(", optional").strip())
31
  if w is _UNSUPPORTED:
32
  return _SKIP
33
  return w if w is _SKIP else w | None
34
+ if type_hint in [
35
  "a digraph or multidigraph",
36
  "a graph g",
37
  "graph",
 
42
  "nx.graph",
43
  "undirected graph",
44
  "undirected networkx graph",
45
+ ] or type_hint.startswith("networkx graph"):
46
  return nx.Graph
47
+ elif type_hint in [
48
  "digraph-like",
49
  "digraph",
50
  "directed graph",
 
53
  "nx.digraph",
54
  ]:
55
  return nx.DiGraph
56
+ elif type_hint == "node":
57
  return _UNSUPPORTED
58
+ elif type_hint == '"node (optional)"':
59
  return _SKIP
60
+ elif type_hint == '"edge"':
61
  return _UNSUPPORTED
62
+ elif type_hint == '"edge (optional)"':
63
  return _SKIP
64
+ elif type_hint in ["class", "data type"]:
65
  return _UNSUPPORTED
66
+ elif type_hint in ["string", "str", "node label"]:
67
  return str
68
+ elif type_hint in ["string or none", "none or string", "string, or none"]:
69
  return str | None
70
+ elif type_hint in ["int", "integer"]:
71
  return int
72
+ elif type_hint in ["bool", "boolean"]:
73
  return bool
74
+ elif type_hint == "tuple":
75
  return _UNSUPPORTED
76
+ elif type_hint == "set":
77
  return _UNSUPPORTED
78
+ elif type_hint == "list of floats":
79
  return _UNSUPPORTED
80
+ elif type_hint == "list of floats or float":
81
  return float
82
+ elif type_hint in ["dict", "dictionary"]:
83
  return _UNSUPPORTED
84
+ elif type_hint == "scalar or dictionary":
85
  return float
86
+ elif type_hint == "none or dict":
87
  return _SKIP
88
+ elif type_hint in ["function", "callable"]:
89
  return _UNSUPPORTED
90
+ elif type_hint in [
91
  "collection",
92
  "container of nodes",
93
  "list of nodes",
94
  ]:
95
  return _UNSUPPORTED
96
+ elif type_hint in [
97
  "container",
98
  "generator",
99
  "iterable",
 
105
  "list",
106
  ]:
107
  return _UNSUPPORTED
108
+ elif type_hint == "generator of sets":
109
  return _UNSUPPORTED
110
+ elif type_hint == "dict or a set of 2 or 3 tuples":
111
  return _UNSUPPORTED
112
+ elif type_hint == "set of 2 or 3 tuples":
113
  return _UNSUPPORTED
114
+ elif type_hint == "none, string or function":
115
  return str | None
116
+ elif type_hint == "string or function" and name == "weight":
117
  return str
118
+ elif type_hint == "integer, float, or none":
119
  return float | None
120
+ elif type_hint in [
121
  "float",
122
  "int or float",
123
  "integer or float",
 
128
  "scalar",
129
  ]:
130
  return float
131
+ elif type_hint in ["integer or none", "int or none"]:
132
  return int | None
133
  elif name == "seed":
134
  return int | None
135
  elif name == "weight":
136
  return str
137
+ elif type_hint == "object":
138
  return _UNSUPPORTED
139
  return _SKIP
140