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

Commit a44566f

Browse files
committed
Improved convention flag as per #128
1 parent 64a276f commit a44566f

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
@@ -626,8 +626,14 @@ def to_rst(cls):
626626
'"signature"')
627627

628628

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

632638

633639
def get_option_parser():
@@ -651,7 +657,8 @@ def get_option_parser():
651657
'codes). for example: --ignore=D101,D202')
652658
option('--convention', metavar='<name>', default='',
653659
help='choose the basic list of checked errors by specifying an '
654-
'existing convention. for example: --convention=pep257')
660+
'existing convention. Possible conventions: {0}'
661+
.format(', '.join(conventions)))
655662
option('--add-select', metavar='<codes>', default='',
656663
help='amend the list of errors to check for by specifying more '
657664
'error codes to check.')
@@ -716,7 +723,7 @@ def check(filenames, select=None, ignore=None):
716723
checked_codes = (select or
717724
set(ErrorRegistry.get_error_codes()) - set(ignore))
718725
else:
719-
checked_codes = Conventions.pep257
726+
checked_codes = conventions.pep257
720727

721728
for filename in filenames:
722729
log.info('Checking file %s.', filename)
@@ -811,9 +818,9 @@ def get_checked_error_codes(options):
811818
elif options.select:
812819
checked_codes = set(options.select.split(','))
813820
elif options.convention:
814-
checked_codes = getattr(Conventions, options.convention)
821+
checked_codes = getattr(conventions, options.convention)
815822
else:
816-
checked_codes = Conventions.pep257
823+
checked_codes = conventions.pep257
817824
checked_codes -= set(options.add_ignore.split(','))
818825
checked_codes |= set(options.add_select.split(','))
819826
return checked_codes - set('')
@@ -826,7 +833,9 @@ def validate_options(options):
826833
log.error('Cannot pass both {0} and {1}. They are '
827834
'mutually exclusive.'.format(opt1, opt2))
828835
return False
829-
if options.convention and not hasattr(Conventions, options.convention):
836+
if options.convention and options.convention not in conventions:
837+
log.error("Illegal convention '{0}'. Possible conventions: {1}"
838+
.format(options.convention, ', '.join(conventions.keys())))
830839
return False
831840
return True
832841

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)