diff options
| author | Eric N. Vander Weele <ericvw@gmail.com> | 2019-07-28 10:39:27 -0400 |
|---|---|---|
| committer | Eric N. Vander Weele <ericvw@gmail.com> | 2019-07-28 10:43:06 -0400 |
| commit | f01011a403fd806571b8b0fa172ec39f54bb9e83 (patch) | |
| tree | 477435b62d25ab2104eedf84a39d19f96d55ff0d /src/flake8/options | |
| parent | 1ba56b9056fd60dbb0f991fe5a4ac0bb6ad85cb9 (diff) | |
| download | flake8-f01011a403fd806571b8b0fa172ec39f54bb9e83.tar.gz | |
Normalize option values additionally by type
Now that the contract has narrowed for `utils.normalize_paths()`
and `utils.parse_comma_separated_list()`, `Option.normalize()` must
handle when the option value is either a singular value or a sequence
(i.e., `list`) of values.
The paths where `Option.normalize()` is called are:
1. options/config.py: `MergedConfigParser.parse_*_config()`
There are 3 paths wehre eventually `_normalize_value()` is called.
2. options/manager.py: `OptionManager.parse_args()`
For (1), values are coming from the `configparser` module. For (2),
values are coming from `optparse.OptionParser`.
Rudimentary investigation seems to implicate that
`optparse.OptionParser.parse_args()` always returns values in a `list`
because it accumulates values. However, for `configparser`, the values
are a string due to the key-value nature of the INI format.
Given that `Option.normalize()` is the convergence point where
normalization of an option occurs, it is acceptable for the method to
also handle the parsing comma-separated values and path normalization by
the option value's type.
Diffstat (limited to 'src/flake8/options')
| -rw-r--r-- | src/flake8/options/manager.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 5d21127..cf94927 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -2,7 +2,7 @@ import collections import logging import optparse # pylint: disable=deprecated-module -from typing import Any, Callable, Dict, List, Optional, Set +from typing import Dict, List, Optional, Set from flake8 import utils @@ -142,14 +142,17 @@ class Option(object): def normalize(self, value, *normalize_args): """Normalize the value based on the option configuration.""" + if self.comma_separated_list and isinstance( + value, utils.string_types + ): + value = utils.parse_comma_separated_list(value) + if self.normalize_paths: - # Decide whether to parse a list of paths or a single path - normalize = utils.normalize_path # type: Callable[..., Any] - if self.comma_separated_list: - normalize = utils.normalize_paths - return normalize(value, *normalize_args) - elif self.comma_separated_list: - return utils.parse_comma_separated_list(value) + if isinstance(value, list): + value = utils.normalize_paths(value, *normalize_args) + else: + value = utils.normalize_path(value, *normalize_args) + return value def normalize_from_setuptools(self, value): |
