Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC/ENH: Clarify usage or configuration files and log about it #2552

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ These are both equivalent to running::

codespell --quiet-level 3 --count --skip "*.po,*.ts,./src/3rdParty,./src/Test"

If several config files are present, they are read in the following order:

#. ``pyproject.toml`` (only if the ``tomli`` library is available)
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved
#. ``setup.cfg``
#. ``.codespellrc``
#. any additional file supplied via ``--config``

If a codespell configuration is supplied in several of these files,
the configuration from the most recently read file overwrites previously
specified configurations.
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved

Any options specified in the command line will *override* options from the
config files.

Expand Down
21 changes: 18 additions & 3 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,17 @@ def parse_options(args):
with open(toml_file, 'rb') as f:
data = tomli.load(f).get('tool', {})
config.read_dict(data)
config.read(cfg_files)

# Collect which config files are going to be used
used_cfg_files = []
for cfg_file in cfg_files:
_cfg = configparser.ConfigParser()
_cfg.read(cfg_file)
if _cfg.has_section('codespell'):
used_cfg_files.append(cfg_file)

# Use config files
config.read(cfg_files)
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved
if config.has_section('codespell'):
# Build a "fake" argv list using option name and value.
cfg_args = []
Expand All @@ -445,7 +454,7 @@ def parse_options(args):
if not options.files:
options.files.append('.')

return options, parser
return options, parser, used_cfg_files


def parse_ignore_words_option(ignore_words_option):
Expand Down Expand Up @@ -776,7 +785,13 @@ def _script_main():

def main(*args):
"""Contains flow control"""
options, parser = parse_options(args)
options, parser, used_cfg_files = parse_options(args)

# Report used config files
if len(used_cfg_files) > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too late for this PR, but you could have used if used_cfg_files instead of if len(used_cfg_files) > 0.

Rationale from DeepSource.io:

Using the len function to check if a sequence is empty is not idiomatic and can be less performant than checking the truthiness of the object.

len doesn't know the context in which it is called, so if computing the length means traversing the entire sequence, it must; it doesn't know that the result is just being compared to 0. Computing the boolean value can stop after it sees the first element, regardless of how long the sequence actually is.

See #2559.

print('Used config files:')
for ifile, cfg_file in enumerate(used_cfg_files, start=1):
print(' %i: %s' % (ifile, cfg_file))

if options.regex and options.write_changes:
print("ERROR: --write-changes cannot be used together with "
Expand Down