diff options
| author | Ruairidh MacLeod <5160559+rkm@users.noreply.github.com> | 2020-05-12 14:23:26 +0100 |
|---|---|---|
| committer | Anthony Sottile <asottile@umich.edu> | 2020-05-13 13:25:51 -0700 |
| commit | 45573570cf706f8490cd631ee6f6a58bc41f8130 (patch) | |
| tree | b4f42b855dfd87a0d4a0f4e6f6ab306da6ccc805 /src | |
| parent | 666be736e0a8632c217997201ef1117babf47c75 (diff) | |
| download | flake8-45573570cf706f8490cd631ee6f6a58bc41f8130.tar.gz | |
Parse --jobs as a custom argparse type. Fixes #567
Diffstat (limited to 'src')
| -rw-r--r-- | src/flake8/checker.py | 11 | ||||
| -rw-r--r-- | src/flake8/main/options.py | 22 |
2 files changed, 24 insertions, 9 deletions
diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 36a4735..d993cb9 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -137,19 +137,12 @@ class Manager(object): return 0 jobs = self.options.jobs - if jobs != "auto" and not jobs.isdigit(): - LOG.warning( - '"%s" is not a valid parameter to --jobs. Must be one ' - 'of "auto" or a numerical value, e.g., 4.', - jobs, - ) - return 0 # If the value is "auto", we want to let the multiprocessing library # decide the number based on the number of CPUs. However, if that # function is not implemented for this particular value of Python we # default to 1 - if jobs == "auto": + if jobs.is_auto: try: return multiprocessing.cpu_count() except NotImplementedError: @@ -157,7 +150,7 @@ class Manager(object): # Otherwise, we know jobs should be an integer and we can just convert # it to an integer - return int(jobs) + return jobs.n_jobs def _handle_results(self, filename, results): style_guide = self.style_guide diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index ba1f1c2..f4588e8 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -62,6 +62,27 @@ def register_preliminary_options(parser): ) +class JobsArgument: + """Type callback for the --jobs argument.""" + + def __init__(self, arg): # type: (str) -> None + """Parse and validate the --jobs argument. + + :param str arg: + The argument passed by argparse for validation + """ + self.is_auto = False + self.n_jobs = -1 + if arg == "auto": + self.is_auto = True + elif arg.isdigit(): + self.n_jobs = int(arg) + else: + raise argparse.ArgumentTypeError( + "{!r} must be 'auto' or an integer.".format(arg), + ) + + def register_default_options(option_manager): """Register the default options on our OptionManager. @@ -293,6 +314,7 @@ def register_default_options(option_manager): "--jobs", default="auto", parse_from_config=True, + type=JobsArgument, help="Number of subprocesses to use to run checks in parallel. " 'This is ignored on Windows. The default, "auto", will ' "auto-detect the number of processors available to use." |
