Skip to content

Commit d7d4e2e

Browse files
authored
Rewrites len(..) == 0 into not .. (#51)
* Rewrites len(..) == 0 into not .. * fix bug
1 parent 885dd6a commit d7d4e2e

File tree

18 files changed

+41
-46
lines changed

18 files changed

+41
-46
lines changed

_unittests/ut_light_api/test_light_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def list_ops_missing(self, n_inputs):
138138
methods.append("")
139139
new_missing.append(m)
140140
text = "\n".join(methods)
141-
if len(new_missing) > 0:
141+
if new_missing:
142142
raise AssertionError(
143143
f"n_inputs={n_inputs}: missing method for operators "
144144
f"{new_missing}\n{text}"

_unittests/ut_validation/test_f8.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def test_search_float32_into_fe5m2(self):
344344
add = value
345345
else:
346346
add = v - value
347-
if len(w) > 0:
347+
if w:
348348
raise AssertionError(
349349
f"A warning was thrown for v={v}, "
350350
f"value={value}, w={w[0]}."

_unittests/ut_xrun_doc/test_documentation_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def import_source(module_file_path, module_name):
2626
class TestDocumentationExamples(ExtTestCase):
2727
def run_test(self, fold: str, name: str, verbose=0) -> int:
2828
ppath = os.environ.get("PYTHONPATH", "")
29-
if len(ppath) == 0:
29+
if not ppath:
3030
os.environ["PYTHONPATH"] = ROOT
3131
elif ROOT not in ppath:
3232
sep = ";" if is_windows() else ":"
@@ -42,7 +42,7 @@ def run_test(self, fold: str, name: str, verbose=0) -> int:
4242
res = p.communicate()
4343
out, err = res
4444
st = err.decode("ascii", errors="ignore")
45-
if len(st) > 0 and "Traceback" in st:
45+
if st and "Traceback" in st:
4646
if '"dot" not found in path.' in st:
4747
# dot not installed, this part
4848
# is tested in onnx framework

onnx_array_api/ext_test_case.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ def assertRaise(self, fct: Callable, exc_type: Exception):
226226
raise AssertionError("No exception was raised.")
227227

228228
def assertEmpty(self, value: Any):
229-
if value is None:
230-
return
231-
if len(value) == 0:
229+
if not value:
232230
return
233231
raise AssertionError(f"value is not empty: {value!r}.")
234232

@@ -240,7 +238,7 @@ def assertNotEmpty(self, value: Any):
240238
if value is None:
241239
raise AssertionError(f"value is empty: {value!r}.")
242240
if isinstance(value, (list, dict, tuple, set)):
243-
if len(value) == 0:
241+
if value:
244242
raise AssertionError(f"value is empty: {value!r}.")
245243

246244
def assertStartsWith(self, prefix: str, full: str):

onnx_array_api/light_api/emitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def render_attribute_value(self, value: Any) -> Tuple[List[str], str]:
8585
if isinstance(v, str):
8686
return [], f"{v!r}"
8787
if isinstance(v, np.ndarray):
88-
if len(v.shape) == 0:
88+
if not v.shape:
8989
return [], str(v)
9090
if len(v.shape) == 1:
9191
if value[0].type in (

onnx_array_api/light_api/translate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def export(self, as_str, single_line: bool = False) -> Union[str, List[str]]:
5151
else:
5252
raise ValueError(f"Unexpected type {type(self.proto_)} for proto.")
5353

54-
if len(sparse_initializers) != 0:
54+
if sparse_initializers:
5555
raise NotImplementedError("Sparse initializer not supported yet.")
5656

5757
rows.extend(

onnx_array_api/npx/npx_graph_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ def to_onnx(
919919
[(var, i, None) for i in range(var.n_var_outputs)]
920920
)
921921

922-
if len(possible_types) > 0:
922+
if possible_types:
923923
# converts possibles types into a dictionary
924924
map_types = {}
925925
for var, i, dt in possible_types:

onnx_array_api/npx/npx_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _process_attributes(attributes):
4747
nodes = []
4848
modified = False
4949
for node in graph.node:
50-
if len(set(node.input) & set_rep) == 0:
50+
if not (set(node.input) & set_rep):
5151
modified = True
5252
new_inputs = [replacements.get(i, i) for i in node.input]
5353
atts = _process_attributes(node.attribute) or node.attribute
@@ -66,7 +66,7 @@ def _process_attributes(attributes):
6666
if not modified:
6767
return None
6868

69-
if len(set(i.name for i in graph.input) & set_rep) == 0:
69+
if not (set(i.name for i in graph.input) & set_rep):
7070
return make_graph(nodes, graph.name, graph.input, graph.output)
7171

7272
new_inputs = []

onnx_array_api/npx/npx_jit_eager.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def to_jit(self, *values, **kwargs):
253253
"""
254254
self.info("+", "to_jit", args=values, kwargs=kwargs)
255255
annotations = self.f.__annotations__
256-
if len(annotations) > 0:
256+
if annotations:
257257
input_to_kwargs = {}
258258
kwargs_to_input = {}
259259
names = list(annotations.keys())
@@ -352,10 +352,10 @@ def to_jit(self, *values, **kwargs):
352352
if iname in constraints
353353
]
354354
names = [i.name for i in inputs]
355-
if len(new_kwargs) > 0:
355+
if new_kwargs:
356356
# An attribute is not named in the numpy API
357357
# but is the ONNX definition.
358-
if len(kwargs) == 0:
358+
if not kwargs:
359359
kwargs = new_kwargs
360360
else:
361361
kwargs = kwargs.copy()
@@ -375,13 +375,13 @@ def to_jit(self, *values, **kwargs):
375375
target_opsets=self.target_opsets,
376376
ir_version=self.ir_version,
377377
)
378-
if len(values) > 0 and len(values[0].shape) == 0:
378+
if values and not values[0].shape:
379379
inps = onx.graph.input[0]
380380
shape = []
381381
for d in inps.type.tensor_type.shape.dim:
382382
v = d.dim_value if d.dim_value > 0 else d.dim_param
383383
shape.append(v)
384-
if len(shape) != 0:
384+
if shape:
385385
raise RuntimeError(
386386
f"Shape mismatch, values[0]={values[0]} "
387387
f"and inputs={onx.graph.input}."
@@ -441,7 +441,7 @@ def move_input_to_kwargs(
441441
f"self.input_to_kwargs_ is not initialized for function {self.f} "
442442
f"from module {self.f.__module__!r}."
443443
)
444-
if len(self.input_to_kwargs_) == 0:
444+
if not self.input_to_kwargs_:
445445
return values, kwargs
446446
new_values = []
447447
new_kwargs = kwargs.copy()

onnx_array_api/npx/npx_numpy_tensors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def __bool__(self):
220220
)
221221
if self.shape == (0,):
222222
return False
223-
if len(self.shape) != 0:
223+
if self.shape:
224224
warnings.warn(
225225
f"Conversion to bool only works for scalar, not for {self!r}, "
226226
f"bool(...)={bool(self._tensor)}."
@@ -233,7 +233,7 @@ def __bool__(self):
233233

234234
def __int__(self):
235235
"Implicit conversion to int."
236-
if len(self.shape) != 0:
236+
if self.shape:
237237
raise ValueError(
238238
f"Conversion to bool only works for scalar, not for {self!r}."
239239
)
@@ -255,7 +255,7 @@ def __int__(self):
255255

256256
def __float__(self):
257257
"Implicit conversion to float."
258-
if len(self.shape) != 0:
258+
if self.shape:
259259
raise ValueError(
260260
f"Conversion to bool only works for scalar, not for {self!r}."
261261
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy