Skip to content

Commit

Permalink
Add explicit stacklevel for warnings.warn
Browse files Browse the repository at this point in the history
The stacklevel is 1 by default, this PR makes it explicit so flake8
can enforce that we are conscious about the stacklevel. stacklevel=1 is
correct when the line-number where warn() is called is the relevant context
for the warning recipient.
  • Loading branch information
berland committed Aug 25, 2023
1 parent 6daddc2 commit fcfcd18
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 6 deletions.
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ignore =
B009
B014
B019
B028
max-line-length = 88
[mutmut]
paths_to_mutate=src/ert/config/parsing/
Expand Down
1 change: 1 addition & 0 deletions src/ert/cli/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def _setup_ensemble_experiment(ert, storage, args, experiment_id):
f"MIN_REALIZATIONS {min_realizations_count}, MIN_REALIZATIONS has been "
f"set to match active_realizations.",
category=ConfigWarning,
stacklevel=1,
)

simulations_argument = {
Expand Down
1 change: 1 addition & 0 deletions src/ert/config/ensemble_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__( # noqa: 501 pylint: disable=too-many-arguments, too-many-branches
"DISABLE_PARAMETERS key instead.\n",
gen_kw[0],
),
stacklevel=1,
)

options = option_dict(gen_kw, 4)
Expand Down
12 changes: 10 additions & 2 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ def _workflows_from_dict( # pylint: disable=too-many-branches
f"Loading workflow job {workflow_job[0]!r}"
f" failed with '{err}'. It will not be loaded.",
workflow_job[0],
)
),
stacklevel=1,
)
except ConfigValidationError as err:
errors.append(
Expand All @@ -549,6 +550,7 @@ def _workflows_from_dict( # pylint: disable=too-many-branches
ConfigWarning.with_context(
f"Unable to open job directory {job_path}", job_path
),
stacklevel=1,
)
continue

Expand All @@ -565,6 +567,7 @@ def _workflows_from_dict( # pylint: disable=too-many-branches
f" failed with '{err}'. It will not be loaded.",
file_name,
),
stacklevel=1,
)
except ConfigValidationError as err:
errors.append(
Expand All @@ -589,7 +592,8 @@ def _workflows_from_dict( # pylint: disable=too-many-branches
warnings.warn(
ConfigWarning.with_context(
f"Workflow {filename!r} was added twice", work[0]
)
),
stacklevel=1,
)
except ConfigValidationError as err:
warnings.warn(
Expand All @@ -599,6 +603,7 @@ def _workflows_from_dict( # pylint: disable=too-many-branches
+ err.cli_message(),
work[0],
),
stacklevel=1,
)

errors = []
Expand Down Expand Up @@ -642,6 +647,7 @@ def _installed_jobs_from_dict(cls, config_dict):
f"{job_config_file!r} over {jobs[name].executable!r}",
name,
),
stacklevel=1,
)
jobs[name] = new_job

Expand All @@ -665,6 +671,7 @@ def _installed_jobs_from_dict(cls, config_dict):
ConfigWarning.with_context(
f"No files found in job directory {job_path}", job_path
),
stacklevel=1,
)
continue

Expand All @@ -685,6 +692,7 @@ def _installed_jobs_from_dict(cls, config_dict):
f"choosing {full_path!r} over {jobs[name].executable!r}",
name,
),
stacklevel=1
)
jobs[name] = new_job

Expand Down
1 change: 1 addition & 0 deletions src/ert/config/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def from_config_list(
f"this has no effect and can be removed",
config_list,
),
stacklevel=1,
)

errors = []
Expand Down
9 changes: 9 additions & 0 deletions src/ert/config/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def _handle_history_observation(
" Truncating start of segment to 0.",
segment_name,
),
stacklevel=1,
)
start = 0
if stop >= time_len:
Expand All @@ -167,6 +168,7 @@ def _handle_history_observation(
f" end of segment to {time_len - 1}.",
segment_name,
),
stacklevel=1,
)
stop = time_len - 1
if start > stop:
Expand All @@ -176,6 +178,7 @@ def _handle_history_observation(
f" end of segment to {start}.",
segment_name,
),
stacklevel=1,
)
stop = start
if np.size(std_dev[start:stop]) == 0:
Expand All @@ -187,6 +190,7 @@ def _handle_history_observation(
"time map.",
segment_name,
),
stacklevel=1,
)
std_dev[start:stop] = cls._handle_error_mode(
values[start:stop],
Expand All @@ -207,6 +211,7 @@ def _handle_history_observation(
f" {summary_key}:{i} - ignored",
summary_key,
),
stacklevel=1,
)
continue
data[dates[i - 1]] = SummaryObservation(
Expand Down Expand Up @@ -237,6 +242,7 @@ def _get_time(date_dict: DateDict, start_time: datetime) -> Tuple[datetime, str]
" Please use ISO date format YYYY-MM-DD",
date_str,
),
stacklevel=1,
)
return date, f"DATE={date_str}"
except ValueError as err:
Expand Down Expand Up @@ -427,6 +433,7 @@ def _handle_general_observation(
f" - ignoring observation {obs_key}",
state_kw,
),
stacklevel=1,
)
return {}
config_node = ensemble_config.getNode(state_kw)
Expand Down Expand Up @@ -454,6 +461,7 @@ def _handle_general_observation(
"The observation will be ignored",
obs_key,
),
stacklevel=1,
)
return {}
response_report_steps = (
Expand All @@ -469,6 +477,7 @@ def _handle_general_observation(
" - The observation will be ignored",
state_kw,
),
stacklevel=1,
)
return {}
restart = 0 if restart is None else restart
Expand Down
3 changes: 2 additions & 1 deletion src/ert/config/parsing/lark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def _substitute_token(
"Probably this causes a loop.",
token,
),
stacklevel=1,
)

return current
Expand All @@ -185,7 +186,7 @@ def _tree_to_dict(
kw, *args = node # type: ignore
if kw not in schema:
warnings.warn(
ConfigWarning.with_context(f"Unknown keyword {kw!r}", kw),
ConfigWarning.with_context(f"Unknown keyword {kw!r}", kw), stacklevel=1
)
continue

Expand Down
3 changes: 2 additions & 1 deletion src/ert/config/parsing/schema_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def push_deprecation(info: DeprecationInfo, line: List[ContextString]):
filename=filename,
message=deprecation.resolve_message(line),
).set_context_keyword(line)
)
),
stacklevel=1,
)

@abc.abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _check_locale():
using one of the environment variables 'LANG', LC_ALL', or 'LC_NUMERIC' to
either the 'C' locale or alternatively a locale which uses '.' as
decimalpoint.\n""" # noqa
warnings.warn(msg, category=ConfigWarning)
warnings.warn(msg, category=ConfigWarning, stacklevel=1)


def _clicked_help_button(menu_label: str, link: str):
Expand Down

0 comments on commit fcfcd18

Please sign in to comment.