Skip to content

Commit cfa1eb1

Browse files
committed
black cleaning
1 parent 5a45526 commit cfa1eb1

File tree

5 files changed

+60
-67
lines changed

5 files changed

+60
-67
lines changed

pytest_parallel/algo.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import operator
55

66

7-
def identity(x):
8-
return x
7+
def identity(elem):
8+
return elem
99

1010

11-
def partition(xs, pred):
11+
def partition(seq, pred):
1212
"""
13-
partitions sequence `xs` into
14-
`xs_true` with elements of `xs` that satisfy predicate `pred`
15-
`xs_false` with elements of `xs` that don't satisfy predicate `pred`
13+
partitions sequence `seq` into
14+
`xs_true` with elements of `seq` that satisfy predicate `pred`
15+
`xs_false` with elements of `seq` that don't satisfy predicate `pred`
1616
then returns `xs_true`, `xs_false`
1717
1818
Complexity:
@@ -25,15 +25,15 @@ def partition(xs, pred):
2525
"""
2626
xs_true = []
2727
xs_false = []
28-
for x in xs:
29-
if pred(x):
30-
xs_true.append(x)
28+
for elem in seq:
29+
if pred(elem):
30+
xs_true.append(elem)
3131
else:
32-
xs_false.append(x)
32+
xs_false.append(elem)
3333
return xs_true, xs_false
3434

3535

36-
def partition_point(xs, pred):
36+
def partition_point(seq, pred):
3737
"""
3838
Gives the partition point of sequence `xs`
3939
That is, the index i where
@@ -52,25 +52,25 @@ def partition_point(xs, pred):
5252
Constant
5353
"""
5454
i = 0
55-
j = len(xs)
55+
j = len(seq)
5656
while i < j:
5757
mid = (i + j) // 2
58-
if pred(xs[mid]):
58+
if pred(seq[mid]):
5959
i = mid + 1
6060
else:
6161
j = mid
6262
return i
6363

6464

65-
def lower_bound(xs, value, key=identity, comp=operator.lt):
66-
def pred(x):
67-
return comp(key(x), value)
65+
def lower_bound(seq, value, key=identity, comp=operator.lt):
66+
def pred(elem):
67+
return comp(key(elem), value)
6868

69-
return partition_point(xs, pred)
69+
return partition_point(seq, pred)
7070

7171

72-
def upper_bound(xs, value, key=identity, comp=operator.lt):
73-
def pred(x):
74-
return not comp(value, key(x))
72+
def upper_bound(seq, value, key=identity, comp=operator.lt):
73+
def pred(elem):
74+
return not comp(value, key(elem))
7575

76-
return partition_point(xs, pred)
76+
return partition_point(seq, pred)

pytest_parallel/mark.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ def test_fun(comm):
1717
What is subtle here is that 'comm' means 3 different things
1818
- the first 'comm' (in `parametrize('comm')`) is a "call_spec"
1919
- the second 'comm' (in `indirect=['comm']`) is a "fixture",
20-
which in our case is a function that takes a `request`,
21-
extracts info from it
22-
and returns it to the function `test_fun` as a parameter
20+
which in our case is a function that takes a `request`,
21+
extracts info from it
22+
and returns it to the function `test_fun` as a parameter
2323
- the comm parameter of function `test_fun`, which is the returned value of the fixture
2424
What happens is that:
2525
- PyTest will take the "callspec" 'comm' (of value 2 or 3)
26-
- this will be passed to `pytest_parallel.pytest_collection_modifyitems` that will create a communicator
26+
- this will be passed to `pytest_parallel.pytest_collection_modifyitems`
27+
that will create a communicator
2728
- we ask for `indirect` because we don't want value 2 or 3 as our 'comm' fixture
28-
we rather want the communicator created by `pytest_parallel.pytest_collection_modifyitems`
29-
With `indirect=['comm']`, we tell PyTest to use fixture `comm` to compute the argument
30-
we want to use in the function body
31-
29+
we rather want the communicator created by `pytest_parallel.pytest_collection_modifyitems`
30+
With `indirect=['comm']`, we tell PyTest to use fixture `comm` to compute the argument
31+
we want to use in the function body
3232
3333
"""
3434
if isinstance(n_proc_list, int):

pytest_parallel/mpi_reporter.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1+
import numpy as np
12
import pytest
2-
3-
from mpi4py import MPI
4-
53
from _pytest._code.code import (
64
ExceptionChainRepr,
75
ReprTraceback,
8-
ReprEntry,
96
ReprEntryNative,
107
ReprFileLocation,
118
)
9+
from mpi4py import MPI
1210

11+
from .algo import partition, lower_bound
1312
from .utils import (
1413
number_of_working_processes,
1514
get_n_proc_for_test,
1615
mark_skip,
1716
add_n_procs,
1817
is_dyn_master_process,
1918
)
20-
from .algo import partition, lower_bound
21-
import operator
22-
import numpy as np
2319

2420

2521
def gather_report(mpi_reports, n_sub_rank):
@@ -133,8 +129,8 @@ def pytest_collection_modifyitems(self, config, items):
133129
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
134130
def pytest_runtestloop(self, session) -> bool:
135131
outcome = yield
136-
137-
# prevent return value being non-zero (ExitCode.NO_TESTS_COLLECTED) when no test run on non-master
132+
# prevent return value being non-zero (ExitCode.NO_TESTS_COLLECTED)
133+
# when no test run on non-master
138134
if self.global_comm.Get_rank() != 0 and session.testscollected == 0:
139135
session.testscollected = 1
140136
return True
@@ -156,25 +152,25 @@ def pytest_runtest_logreport(self, report):
156152

157153
def group_items_by_parallel_steps(items, n_workers):
158154
add_n_procs(items)
159-
items.sort(key=lambda item: item.n_procs, reverse=True)
155+
items.sort(key=lambda item: item.n_proc, reverse=True)
160156

161157
remaining_n_procs_by_step = []
162158
items_by_step = []
163159
items_to_skip = []
164160
for item in items:
165-
if item.n_procs > n_workers:
161+
if item.n_proc > n_workers:
166162
items_to_skip += [item]
167163
else:
168164
found_step = False
169165
for idx, remaining_procs in enumerate(remaining_n_procs_by_step):
170-
if item.n_procs <= remaining_procs:
166+
if item.n_proc <= remaining_procs:
171167
items_by_step[idx] += [item]
172-
remaining_n_procs_by_step[idx] -= item.n_procs
168+
remaining_n_procs_by_step[idx] -= item.n_proc
173169
found_step = True
174170
break
175171
if not found_step:
176172
items_by_step += [[item]]
177-
remaining_n_procs_by_step += [n_workers - item.n_procs]
173+
remaining_n_procs_by_step += [n_workers - item.n_proc]
178174

179175
return items_by_step, items_to_skip
180176

@@ -332,24 +328,25 @@ def sub_comm_from_ranks(global_comm, sub_ranks):
332328

333329

334330
def item_with_biggest_admissible_n_proc(items, n_av_procs):
335-
key = lambda item: item.n_procs
331+
def _key(item):
332+
return item.n_proc
333+
336334
# Preconditions:
337335
# sorted(items, key)
338336
# len(items)>0
339337

340338
# best choices: tests requiring the most procs while still 'runnable'
341339
# among those, we favor the first in the array for 'stability' reasons (no reordering when not needed)
342-
idx = lower_bound(items, n_av_procs, key)
343-
if idx == 0 and items[idx].n_procs > n_av_procs: # all items ask too much
340+
idx = lower_bound(items, n_av_procs, _key)
341+
if idx == 0 and items[idx].n_proc > n_av_procs: # all items ask too much
344342
return -1
345-
elif idx < len(items) and items[idx].n_procs == n_av_procs:
343+
if idx < len(items) and items[idx].n_proc == n_av_procs:
346344
# we find the first internal item with matching n_proc
347345
return idx
348-
else:
349-
# we did not find an item with exactly the matching n_proc,
350-
# in this case, the item just before gives the new n_proc we are searching for
351-
max_needed_n_proc = items[idx - 1].n_procs
352-
return lower_bound(items, max_needed_n_proc, key)
346+
# we did not find an item with exactly the matching n_proc,
347+
# in this case, the item just before gives the new n_proc we are searching for
348+
max_needed_n_proc = items[idx - 1].n_proc
349+
return lower_bound(items, max_needed_n_proc, _key)
353350

354351

355352
def mark_original_index(items):
@@ -372,7 +369,7 @@ def mark_original_index(items):
372369

373370
####### Server #######
374371
def schedule_test(item, available_procs, inter_comm):
375-
n_procs = item.n_procs
372+
n_procs = item.n_proc
376373
original_idx = item.original_index
377374

378375
sub_ranks = []
@@ -411,7 +408,7 @@ def wait_test_to_complete(items_to_run, session, available_procs, inter_comm):
411408

412409
# get associated item
413410
item = items_to_run[original_idx]
414-
n_proc = item.n_procs
411+
n_proc = item.n_proc
415412
sub_ranks = item.sub_ranks
416413
assert first_rank_done in sub_ranks
417414

@@ -479,7 +476,7 @@ def pytest_pyfunc_call(self, pyfuncitem):
479476

480477
@pytest.hookimpl(tryfirst=True)
481478
def pytest_runtestloop(self, session) -> bool:
482-
# same begining as PyTest default's
479+
# same beginning as PyTest default's
483480
if (
484481
session.testsfailed
485482
and not session.config.option.continue_on_collection_errors
@@ -499,7 +496,7 @@ def pytest_runtestloop(self, session) -> bool:
499496
add_n_procs(session.items)
500497

501498
## isolate skips
502-
has_enough_procs = lambda item: item.n_procs <= n_workers
499+
has_enough_procs = lambda item: item.n_proc <= n_workers
503500
items_to_run, items_to_skip = partition(session.items, has_enough_procs)
504501

505502
## remember original position
@@ -517,7 +514,7 @@ def pytest_runtestloop(self, session) -> bool:
517514
run_item_test(item, nextitem, session)
518515

519516
# schedule tests to run
520-
items_left_to_run = sorted(items_to_run, key=lambda item: item.n_procs)
517+
items_left_to_run = sorted(items_to_run, key=lambda item: item.n_proc)
521518
available_procs = np.ones(n_workers, dtype=np.int8)
522519

523520
while len(items_left_to_run) > 0:
@@ -576,11 +573,7 @@ def pytest_runtest_logreport(self, report):
576573
report
577574
) # has been 'run' locally: do nothing special
578575
else:
579-
assert (
580-
report.when == "setup"
581-
or report.when == "call"
582-
or report.when == "teardown"
583-
) # only know tags
576+
assert report.when in ("setup", "call", "teardown") # only known tags
584577
tag = WHEN_TAGS[report.when]
585578

586579
# master ranks of each sub_comm must send their report to rank 0

pytest_parallel/test/test_utils.py

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

77
def test_group_items_by_parallel_steps():
88
class callspec_mock:
9-
def __init__(self, n_procs):
10-
self.n_procs = n_procs
9+
def __init__(self, n_proc):
10+
self.n_proc = n_proc
1111

1212
def getparam(self, s):
1313
assert s == "comm"
14-
return self.n_procs
14+
return self.n_proc
1515

1616
class item_mock:
1717
def __init__(self, name, n_procs):
@@ -45,7 +45,7 @@ def __init__(self, name, n_procs):
4545
def test_item_with_biggest_admissible_n_proc():
4646
class item_mock:
4747
def __init__(self, n_proc):
48-
self.n_procs = n_proc
48+
self.n_proc = n_proc
4949

5050
items = [
5151
item_mock(1), # 0

pytest_parallel/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_n_proc_for_test(item: Item) -> int:
3636

3737
def add_n_procs(items):
3838
for item in items:
39-
item.n_procs = get_n_proc_for_test(item)
39+
item.n_proc = get_n_proc_for_test(item)
4040

4141

4242
def mark_skip(item):

0 commit comments

Comments
 (0)