Skip to content

Commit

Permalink
flake8-bugbear: opt-in to a couple of opinionated warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie committed Aug 20, 2024
1 parent 4b58a6a commit d81220b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cylc/flow/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2463,7 +2463,7 @@ def get_taskdef(
'Duplicate task message in'
f' "[runtime][{name}][outputs]'
f'{output} = {message}" - messages must be unique'
)
) from None
valid, msg = TaskOutputValidator.validate(output)
if not valid:
raise WorkflowConfigError(
Expand Down
50 changes: 29 additions & 21 deletions cylc/flow/scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,53 +51,62 @@
"""
import functools
import pkgutil
from pathlib import Path
import re
import sys
import shutil
import sys
from collections import Counter
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Callable,
Counter as CounterType,
Dict,
Iterator,
List,
Optional,
Union,
)

try:
# BACK COMPAT: tomli
# Support for Python versions before tomllib was added to the
# standard library.
# FROM: Python 3.7
# TO: Python: 3.10
from tomli import (
loads as toml_loads,
TOMLDecodeError,
)
from tomli import TOMLDecodeError, loads as toml_loads
except ImportError:
from tomllib import ( # type: ignore[no-redef]
loads as toml_loads,
TOMLDecodeError,
)
from typing import (
TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Union
)

from ansimarkup import parse as cparse

from cylc.flow import LOG
from cylc.flow.exceptions import CylcError
import cylc.flow.flags
from cylc.flow import job_runner_handlers
from cylc.flow import LOG, job_runner_handlers
from cylc.flow.cfgspec.workflow import SPEC, upg
from cylc.flow.exceptions import CylcError
from cylc.flow.id_cli import parse_id
from cylc.flow.job_runner_mgr import JobRunnerManager
from cylc.flow.loggingutil import set_timestamps
from cylc.flow.option_parsers import (
WORKFLOW_ID_OR_PATH_ARG_DOC,
CylcOptionParser as COP,
WORKFLOW_ID_OR_PATH_ARG_DOC
)
from cylc.flow.cfgspec.workflow import upg, SPEC
from cylc.flow.id_cli import parse_id
from cylc.flow.parsec.config import ParsecConfig
from cylc.flow.scripts.cylc import DEAD_ENDS
from cylc.flow.terminal import cli_function


if TYPE_CHECKING:
from optparse import Values

# BACK COMPAT: typing_extensions.Literal
# FROM: Python 3.7
# TO: Python 3.8
from typing_extensions import Literal
from optparse import Values


LINT_TABLE = ['tool', 'cylc', 'lint']
LINT_SECTION = '.'.join(LINT_TABLE)
Expand Down Expand Up @@ -1114,7 +1123,7 @@ def check_cylc_file(
file: Path,
file_rel: Path,
checks: Dict[str, dict],
counter: Dict[str, int],
counter: CounterType[str],
modify: bool = False,
):
"""Check A Cylc File for Cylc 7 Config"""
Expand Down Expand Up @@ -1172,7 +1181,7 @@ def lint(
file_rel: Path,
lines: Iterator[str],
checks: Dict[str, dict],
counter: Dict[str, int],
counter: CounterType[str],
modify: bool = False,
write: Callable = print
) -> Iterator[str]:
Expand All @@ -1186,7 +1195,7 @@ def lint(
Iterator which produces one line of text at a time
e.g. open(file) or iter(['foo\n', 'bar\n', 'baz\n'].
counter:
Dictionary for counting lint hits per category.
Counter for counting lint hits per category.
modify:
If True, this generator will yield the file one line at a time
with comments inserted to help users fix their lint.
Expand Down Expand Up @@ -1238,7 +1247,6 @@ def lint(
msg = check_meta['short'].format(**check)
else:
msg = check_meta['short']
counter.setdefault(check_meta['purpose'], 0)
counter[check_meta['purpose']] += 1
if modify:
# insert a command to help the user
Expand Down Expand Up @@ -1473,7 +1481,7 @@ def main(parser: COP, options: 'Values', target=None) -> None:
)

# Check each file matching a pattern:
counter: Dict[str, int] = {}
counter: CounterType[str] = Counter()
for file in get_cylc_files(target, mergedopts[EXCLUDE]):
LOG.debug(f'Checking {file}')
check_cylc_file(
Expand Down
6 changes: 3 additions & 3 deletions cylc/flow/scripts/report_timings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@
the database query to obtain the timing information may take some time.
"""

import collections
import contextlib
import io as StringIO
import sys
from collections import Counter
from typing import TYPE_CHECKING


from cylc.flow import LOG
from cylc.flow.exceptions import CylcError
from cylc.flow.id_cli import parse_id
Expand All @@ -66,6 +65,7 @@
from cylc.flow.rundb import CylcWorkflowDAO
from cylc.flow.terminal import cli_function


if TYPE_CHECKING:
from optparse import Values

Expand Down Expand Up @@ -278,7 +278,7 @@ def _reshape_timings(timings):
# there are duplicate entries in the index (see #2509). The
# best way around this seems to be to add an intermediate index
# level (in this case a retry counter) to de-duplicate indices.
counts = collections.defaultdict(int)
counts = Counter()
retry = []
for t in timings.index:
counts[t] += 1
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[flake8]
extend-select=
; Use `raise from` inside `except` for error handling
; https://docs.python.org/3/tutorial/errors.html#exception-chaining
B904
; Use Counter() instead of defaultdict(int) to avoid excessive memory use:
B910
ignore=
; module level import not at top of file
E402,
Expand Down

0 comments on commit d81220b

Please sign in to comment.