Skip to content

Commit d11fc14

Browse files
[pre-commit.ci lite] apply automatic fixes
1 parent ae26308 commit d11fc14

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/pytest_run_parallel/plugin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __init__(self, config):
107107
unsafe_fixtures[item] = False
108108

109109
if self.mark_warnings_as_unsafe:
110-
unsafe_fixtures['recwarn'] = False
110+
unsafe_fixtures["recwarn"] = False
111111

112112
self.unsafe_fixtures = {uf[0] for uf in unsafe_fixtures.items() if not uf[1]}
113113

@@ -144,8 +144,11 @@ def _is_thread_unsafe(self, item):
144144
return True, f"uses thread-unsafe fixture(s): {used_unsafe_fixtures}"
145145

146146
return identify_thread_unsafe_nodes(
147-
item.obj, self.skipped_functions, self.mark_warnings_as_unsafe,
148-
self.mark_ctypes_as_unsafe)
147+
item.obj,
148+
self.skipped_functions,
149+
self.mark_warnings_as_unsafe,
150+
self.mark_ctypes_as_unsafe,
151+
)
149152

150153
@pytest.hookimpl(trylast=True)
151154
def pytest_itemcollected(self, item):

src/pytest_run_parallel/thread_unsafe_detection.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
def is_hypothesis_test(fn):
1616
return False
1717

18-
WARNINGS_IS_THREADSAFE = (
19-
getattr(sys.flags, "context_aware_warnings", 0) and
20-
getattr(sys.flags, "thread_inherit_context", 0)
18+
19+
WARNINGS_IS_THREADSAFE = getattr(sys.flags, "context_aware_warnings", 0) and getattr(
20+
sys.flags, "thread_inherit_context", 0
2121
)
2222

2323
CTYPES_IS_THREADSAFE = sys.version_info > (3, 13)
2424

25+
2526
def construct_base_blocklist(unsafe_warnings, unsafe_ctypes):
2627
safe_warnings = not unsafe_warnings and WARNINGS_IS_THREADSAFE
2728
safe_ctypes = not unsafe_ctypes and CTYPES_IS_THREADSAFE
@@ -37,7 +38,6 @@ def construct_base_blocklist(unsafe_warnings, unsafe_ctypes):
3738
}
3839

3940

40-
4141
THREAD_UNSAFE_FIXTURES = {
4242
"capsys": False,
4343
"monkeypatch": False,
@@ -136,8 +136,11 @@ def _get_child_fn(mod, node):
136136
if child_fn is not None and callable(child_fn):
137137
self.thread_unsafe, self.thread_unsafe_reason = (
138138
identify_thread_unsafe_nodes(
139-
child_fn, self.skip_set, self.unsafe_warnings,
140-
self.unsafe_ctypes, self.level + 1
139+
child_fn,
140+
self.skip_set,
141+
self.unsafe_warnings,
142+
self.unsafe_ctypes,
143+
self.level + 1,
141144
)
142145
)
143146

@@ -184,8 +187,11 @@ def _recursive_analyze_name(self, node):
184187
if callable(child_fn):
185188
self.thread_unsafe, self.thread_unsafe_reason = (
186189
identify_thread_unsafe_nodes(
187-
child_fn, self.skip_set, self.unsafe_warnings,
188-
self.unsafe_ctypes, self.level + 1
190+
child_fn,
191+
self.skip_set,
192+
self.unsafe_warnings,
193+
self.unsafe_ctypes,
194+
self.level + 1,
189195
)
190196
)
191197

@@ -228,8 +234,9 @@ def visit(self, node):
228234
return super().visit(node)
229235

230236

231-
def _identify_thread_unsafe_nodes(fn, skip_set, unsafe_warnings, unsafe_ctypes,
232-
level=0):
237+
def _identify_thread_unsafe_nodes(
238+
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=0
239+
):
233240
if is_hypothesis_test(fn):
234241
return True, "uses hypothesis"
235242

@@ -243,20 +250,22 @@ def _identify_thread_unsafe_nodes(fn, skip_set, unsafe_warnings, unsafe_ctypes,
243250
except Exception:
244251
return False, None
245252

246-
visitor = ThreadUnsafeNodeVisitor(fn, skip_set, unsafe_warnings,
247-
unsafe_ctypes, level=level)
253+
visitor = ThreadUnsafeNodeVisitor(
254+
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=level
255+
)
248256
visitor.visit(tree)
249257
return visitor.thread_unsafe, visitor.thread_unsafe_reason
250258

251259

252260
cached_thread_unsafe_identify = functools.lru_cache(_identify_thread_unsafe_nodes)
253261

254262

255-
def identify_thread_unsafe_nodes(fn, skip_set, unsafe_warnings, unsafe_ctypes,
256-
level=0):
263+
def identify_thread_unsafe_nodes(fn, skip_set, unsafe_warnings, unsafe_ctypes, level=0):
257264
try:
258265
return cached_thread_unsafe_identify(
259-
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=level)
266+
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=level
267+
)
260268
except TypeError:
261269
return _identify_thread_unsafe_nodes(
262-
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=level)
270+
fn, skip_set, unsafe_warnings, unsafe_ctypes, level=level
271+
)

tests/test_thread_unsafe_detection.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ def test_single_thread_warns_4(num_parallel_threads):
115115
]
116116
)
117117

118-
if not (getattr(sys.flags, "context_aware_warnings", 0) or
119-
getattr(sys.flags, "thread_inherit_context", 0)):
120-
118+
if not (
119+
getattr(sys.flags, "context_aware_warnings", 0)
120+
or getattr(sys.flags, "thread_inherit_context", 0)
121+
):
121122
# check that skipping works too
122123
result = pytester.runpytest(
123124
"--parallel-threads=10", "--skip-thread-unsafe=True", "-v"
@@ -132,6 +133,7 @@ def test_single_thread_warns_4(num_parallel_threads):
132133
]
133134
)
134135

136+
135137
def test_warns_detection_config_option(pytester):
136138
# create a temporary pytest test module
137139
pytester.makepyfile("""
@@ -168,7 +170,9 @@ def test_single_thread_warns_4(num_parallel_threads):
168170
""")
169171

170172
# run pytest with the following cmd args
171-
result = pytester.runpytest("--parallel-threads=10", "-v", "--mark-warnings-as-unsafe")
173+
result = pytester.runpytest(
174+
"--parallel-threads=10", "-v", "--mark-warnings-as-unsafe"
175+
)
172176

173177
# fnmatch_lines does an assertion internally
174178
result.stdout.fnmatch_lines(
@@ -182,8 +186,10 @@ def test_single_thread_warns_4(num_parallel_threads):
182186

183187
# check that skipping works too
184188
result = pytester.runpytest(
185-
"--parallel-threads=10", "--skip-thread-unsafe=True", "-v",
186-
"--mark-warnings-as-unsafe"
189+
"--parallel-threads=10",
190+
"--skip-thread-unsafe=True",
191+
"-v",
192+
"--mark-warnings-as-unsafe",
187193
)
188194

189195
result.stdout.fnmatch_lines(
@@ -196,7 +202,6 @@ def test_single_thread_warns_4(num_parallel_threads):
196202
)
197203

198204

199-
200205
def test_thread_unsafe_fixtures(pytester):
201206
# create a temporary pytest test module
202207
pytester.makepyfile("""
@@ -563,6 +568,7 @@ def test_thread_unsafe_ctypes(num_parallel_threads):
563568
]
564569
)
565570

571+
566572
def test_thread_unsafe_ctypes_config_option(pytester):
567573
pytester.makepyfile("""
568574
import ctypes.util
@@ -574,13 +580,15 @@ def test_thread_unsafe_ctypes(num_parallel_threads):
574580
""")
575581

576582
result = pytester.runpytest(
577-
"--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe")
583+
"--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe"
584+
)
578585
result.stdout.fnmatch_lines(
579586
[
580587
"*::test_thread_unsafe_ctypes PASSED*",
581588
]
582589
)
583590

591+
584592
def test_thread_unsafe_ctypes_import_from(pytester):
585593
pytester.makepyfile("""
586594
import sys
@@ -621,7 +629,9 @@ def test_thread_unsafe_not_using_ctypes(num_parallel_threads):
621629
assert num_parallel_threads == 10
622630
""")
623631

624-
result = pytester.runpytest("--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe")
632+
result = pytester.runpytest(
633+
"--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe"
634+
)
625635
result.stdout.fnmatch_lines(
626636
[
627637
"*::test_thread_unsafe_ctypes PASSED*",

0 commit comments

Comments
 (0)