Skip to content

Commit

Permalink
Hide pytest from code snippets (dask#8198)
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky authored Sep 26, 2023
1 parent dc411fd commit a74f7cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
32 changes: 25 additions & 7 deletions distributed/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ def _get_computation_code(
"""
if nframes <= 0:
return ()

ignore_modules = dask.config.get(
"distributed.diagnostics.computations.ignore-modules"
)
Expand All @@ -3030,10 +3031,26 @@ def _get_computation_code(
"Ignored modules must be a list. Instead got "
f"({type(ignore_modules)}, {ignore_modules})"
)
pattern: re.Pattern | None = None
ignore_files = dask.config.get(
"distributed.diagnostics.computations.ignore-files"
)
if not isinstance(ignore_files, list):
raise TypeError(
"Ignored files must be a list. Instead got "
f"({type(ignore_files)}, {ignore_files})"
)

mod_pattern: re.Pattern | None = None
fname_pattern: re.Pattern | None = None
if stacklevel is None:
if ignore_modules:
pattern = re.compile("|".join([f"(?:{mod})" for mod in ignore_modules]))
mod_pattern = re.compile(
"|".join([f"(?:{mod})" for mod in ignore_modules])
)
if ignore_files:
fname_pattern = re.compile(
r".*[\\/](" + "|".join(mod for mod in ignore_files) + r")([\\/]|$)"
)
else:
# stacklevel 0 or less - shows dask internals which likely isn't helpful
stacklevel = stacklevel if stacklevel > 0 else 1
Expand All @@ -3044,12 +3061,13 @@ def _get_computation_code(
):
if len(code) >= nframes:
break
elif stacklevel is not None and i != stacklevel:
if stacklevel is not None and i != stacklevel:
continue
elif pattern is not None and (
pattern.match(fr.f_globals.get("__name__", ""))
or fr.f_code.co_name in ("<listcomp>", "<dictcomp>")
):
if fr.f_code.co_name in ("<listcomp>", "<dictcomp>"):
continue
if mod_pattern and mod_pattern.match(fr.f_globals.get("__name__", "")):
continue
if fname_pattern and fname_pattern.match(fr.f_code.co_filename):
continue

kwargs = dict(
Expand Down
6 changes: 6 additions & 0 deletions distributed/distributed-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ properties:
A list of modules which are ignored when trying to collect the
code context when submitting a computation. Accepts regular
expressions.
ignore-files:
type: array
description: |
A list of files and directories which are ignored when trying to
collect the code context when submitting a computation. Accepts
regular expressions.
erred-tasks:
type: object
properties:
Expand Down
12 changes: 12 additions & 0 deletions distributed/distributed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,27 @@ distributed:
ignore-modules:
- asyncio
- functools
- datashader
- dask
- debugpy
- distributed
- coiled
- cudf
- cuml
- pluggy # part of pytest
- prefect
- rechunker
- xarray
- xgboost
ignore-files:
- runpy\.py # `python -m pytest` (or other module) shell command
- pytest # `pytest` shell command
- py\.test # `py.test` shell command
- pytest-script\.py # `pytest` shell command in Windows
- _pytest # pytest implementation
- pycharm # Run pytest from PyCharm GUI
- vscode_pytest
- get_output_via_markers\.py
erred-tasks:
max-history: 100

Expand Down
11 changes: 6 additions & 5 deletions distributed/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7314,7 +7314,10 @@ def nested_call():
code = Client._get_computation_code()

with dask.config.set(
{"distributed.diagnostics.computations.ignore-modules": ["test_client"]}
{
"distributed.diagnostics.computations.ignore-modules": ["test_client"],
"distributed.diagnostics.computations.ignore-files": [],
}
):
import sys

Expand Down Expand Up @@ -7413,10 +7416,8 @@ def fetch_comp_code(dask_scheduler):
return comp.code[0]

code = client.run_on_scheduler(fetch_comp_code)

assert len(code) == 2
assert code[-1].code == test_function_code
assert code[-2].code == inspect.getsource(sys._getframe(1))
assert len(code) == 1
assert code[0].code == test_function_code


def test_computation_object_code_dask_compute_no_frames_default(client):
Expand Down

0 comments on commit a74f7cf

Please sign in to comment.