Skip to content

Commit

Permalink
Hide pytest from code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Sep 20, 2023
1 parent 2858930 commit cc133fb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
32 changes: 25 additions & 7 deletions distributed/client.py
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(

Check warning on line 3038 in distributed/client.py

View check run for this annotation

Codecov / codecov/patch

distributed/client.py#L3038

Added line #L3038 was not covered by tests
"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
Expand Up @@ -1050,6 +1050,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
7 changes: 7 additions & 0 deletions distributed/distributed.yaml
Expand Up @@ -281,10 +281,17 @@ distributed:
- coiled
- cudf
- cuml
- pluggy # part of pytest
- prefect
- rechunker
- xarray
- xgboost
ignore-files:
- runpy\.py # `python -m pytest` shell command
- pytest # `pytest` shell command
- py\.test # `py.test` shell command
- _pytest # pytest implementation
- pycharm # Run pytest from PyCharm GUI
erred-tasks:
max-history: 100

Expand Down
11 changes: 6 additions & 5 deletions distributed/tests/test_client.py
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 cc133fb

Please sign in to comment.