Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Config file warning #280

Merged
merged 8 commits into from Sep 3, 2017
2 changes: 2 additions & 0 deletions docs/release_notes.rst
Expand Up @@ -25,6 +25,8 @@ Bug Fixes
* Fixed an issue where if a first word in a docstring had Unicode characters
and the docstring was not a unicode string, an exception would be raised
(#258, #264).
* Configuration files that were specified by CLI and don't contain a valid
section name will now issue a warning to ``stderr`` (#276, #280).


2.0.0 - April 18th, 2017
Expand Down
9 changes: 8 additions & 1 deletion src/pydocstyle/config.py
Expand Up @@ -256,10 +256,17 @@ def _get_config(self, node):
raise IllegalConfiguration('Configuration file {!r} specified '
'via --config was not found.'
.format(self._run_conf.config))

if None in self._cache:
return self._cache[None]
options, _ = self._read_configuration_file(self._run_conf.config)
config = self._create_check_config(options)

if options is None:
log.warning('Configuration file does not contain a '
'pydocstyle section. Using default configuration.')
config = self._create_check_config(self._options)
Copy link
Member

Choose a reason for hiding this comment

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

This function is called for every directory in a project, so a lack of a pydocstyle section should generally be treated by just skipping it and checking the parent directory. If you use the default config, you might screw up the configuration (this might happen if, for example, there's a setup.cfg file for a different tool).

This logic should only be applied if self._run_config.config is not None, i.e., if a specific config file is specified.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I thought this block was in the top level of the method, but in fact it's inside the self._run_config.config check. But add a test for this anyway (a config file in the hierarchy that doesn't have a correct section name).

Copy link
Member Author

Choose a reason for hiding this comment

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

😸

else:
config = self._create_check_config(options)

# Make the CLI always win
final_config = {}
Expand Down
26 changes: 26 additions & 0 deletions src/tests/test_integration.py
Expand Up @@ -251,6 +251,32 @@ def foo():
assert 'D103' not in err


def test_sectionless_config_file(env):
"""Test that config files without a valid section name issue a warning."""
with env.open('config.ini', 'wt') as conf:
conf.write('[pdcstl]')
config_path = conf.name

_, err, code = env.invoke('--config={}'.format(config_path))
assert code == 0
assert 'Configuration file does not contain a pydocstyle section' in err

with env.open('example.py', 'wt') as example:
example.write(textwrap.dedent("""\
def foo():
pass
"""))

with env.open('tox.ini', 'wt') as conf:
conf.write('[pdcstl]\n')
conf.write('ignore = D100')

out, err, code = env.invoke()
assert code == 1
assert 'D100' in out
assert 'file does not contain a pydocstyle section' not in err


def test_config_path(env):
"""Test that options are correctly loaded from a specific config file.

Expand Down