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

Commit 23cf188

Browse files
committed
Merge pull request #131 from Nurdok/improved-convention-flag
Improve --convention flag
2 parents 0cb5c2e + a44566f commit 23cf188

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

docs/snippets/cli.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Usage
1818
which errors to ignore (with a list of comma-separated
1919
error codes). for example: --ignore=D101,D202
2020
--convention=<name> choose the basic list of checked errors by specifying
21-
an existing convention. for example:
22-
--convention=pep257
21+
an existing convention. Possible conventions: pep257
2322
--add-select=<codes> amend the list of errors to check for by specifying
2423
more error codes to check.
2524
--add-ignore=<codes> amend the list of errors to check for by specifying
@@ -36,6 +35,7 @@ Usage
3635
-v, --verbose print status information
3736
--count print total number of errors to stdout
3837
38+
3939
Return Code
4040
^^^^^^^^^^^
4141

pep257.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,14 @@ def to_rst(cls):
627627
'"signature"')
628628

629629

630-
class Conventions(object):
631-
pep257 = set(ErrorRegistry.get_error_codes())
630+
class AttrDict(dict):
631+
def __getattr__(self, item):
632+
return self[item]
633+
634+
635+
conventions = AttrDict({
636+
'pep257': set(ErrorRegistry.get_error_codes()),
637+
})
632638

633639

634640
def get_option_parser():
@@ -652,7 +658,8 @@ def get_option_parser():
652658
'codes). for example: --ignore=D101,D202')
653659
option('--convention', metavar='<name>', default='',
654660
help='choose the basic list of checked errors by specifying an '
655-
'existing convention. for example: --convention=pep257')
661+
'existing convention. Possible conventions: {0}'
662+
.format(', '.join(conventions)))
656663
option('--add-select', metavar='<codes>', default='',
657664
help='amend the list of errors to check for by specifying more '
658665
'error codes to check.')
@@ -717,7 +724,7 @@ def check(filenames, select=None, ignore=None):
717724
checked_codes = (select or
718725
set(ErrorRegistry.get_error_codes()) - set(ignore))
719726
else:
720-
checked_codes = Conventions.pep257
727+
checked_codes = conventions.pep257
721728

722729
for filename in filenames:
723730
log.info('Checking file %s.', filename)
@@ -812,9 +819,9 @@ def get_checked_error_codes(options):
812819
elif options.select:
813820
checked_codes = set(options.select.split(','))
814821
elif options.convention:
815-
checked_codes = getattr(Conventions, options.convention)
822+
checked_codes = getattr(conventions, options.convention)
816823
else:
817-
checked_codes = Conventions.pep257
824+
checked_codes = conventions.pep257
818825
checked_codes -= set(options.add_ignore.split(','))
819826
checked_codes |= set(options.add_select.split(','))
820827
return checked_codes - set('')
@@ -827,7 +834,9 @@ def validate_options(options):
827834
log.error('Cannot pass both {0} and {1}. They are '
828835
'mutually exclusive.'.format(opt1, opt2))
829836
return False
830-
if options.convention and not hasattr(Conventions, options.convention):
837+
if options.convention and options.convention not in conventions:
838+
log.error("Illegal convention '{0}'. Possible conventions: {1}"
839+
.format(options.convention, ', '.join(conventions.keys())))
831840
return False
832841
return True
833842

test_pep257.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,11 @@ def test_missing_docstring_in_package():
289289
assert code == 1
290290
assert 'D100' not in err # shouldn't be treated as a module
291291
assert 'D104' in err # missing docstring in package
292+
293+
294+
def test_illegal_convention():
295+
with Pep257Env() as env:
296+
out, err, code = env.invoke_pep257('--convention=illegal_conv')
297+
assert code == 2
298+
assert "Illegal convention 'illegal_conv'." in err
299+
assert 'Possible conventions: pep257' in err

0 commit comments

Comments
 (0)