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

Commit f1e883c

Browse files
committed
Starting to work on selecting checked error codes.
1 parent 286f4ff commit f1e883c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

pep257.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
"""
1414
from __future__ import with_statement
15+
from distutils.command.check import check
1516

1617
import os
1718
import sys
@@ -440,9 +441,20 @@ def get_option_parser():
440441
help='show explanation of each error')
441442
option('-s', '--source', action='store_true',
442443
help='show source for each error')
444+
option('--select', metavar='<codes>', default='',
445+
help='choose the basic list of checked errors by specifying which '
446+
'errors to check for (with a list of comma-separated error '
447+
'codes). for example: --select=D101,D202')
443448
option('--ignore', metavar='<codes>', default='',
444-
help='ignore a list comma-separated error codes, '
445-
'for example: --ignore=D101,D202')
449+
help='choose the basic list of checked errors by specifying which '
450+
'errors to ignore (with a list of comma-separated error '
451+
'codes). for example: --ignore=D101,D202')
452+
option('--add-select', metavar='<codes>', default='',
453+
help='amend the list of errors to check for by specifying more '
454+
'error codes to check.')
455+
option('--add-ignore', metavar='<codes>', default='',
456+
help='amend the list of errors to check for by specifying more '
457+
'error codes to ignore.')
446458
option('--match', metavar='<pattern>', default='(?!test_).*\.py',
447459
help="check only files that exactly match <pattern> regular "
448460
"expression; default is --match='(?!test_).*\.py' which "
@@ -484,14 +496,14 @@ def collect(names, match=lambda name: True, match_dir=lambda name: True):
484496
yield name
485497

486498

487-
def check(filenames, ignore=()):
499+
def check(filenames, checked_codes=()):
488500
"""Generate PEP 257 errors that exist in `filenames` iterable.
489501
490-
Skips errors with error-codes defined in `ignore` iterable.
502+
Only returns errors with error-codes defined in `checked_codes` iterable.
491503
492504
Example
493505
-------
494-
>>> check(['pep257.py'], ignore=['D100'])
506+
>>> check(['pep257.py'], checked_codes=['D100'])
495507
<generator object check at 0x...>
496508
497509
"""
@@ -502,7 +514,7 @@ def check(filenames, ignore=()):
502514
source = file.read()
503515
for error in PEP257Checker().check_source(source, filename):
504516
code = getattr(error, 'code', None)
505-
if code is not None and code not in ignore:
517+
if code in checked_codes:
506518
yield error
507519
except (EnvironmentError, AllError):
508520
yield sys.exc_info()[1]
@@ -567,6 +579,9 @@ def setup_stream_handler(options):
567579
log.addHandler(stream_handler)
568580

569581

582+
def get_checked_error_codes(options):
583+
return ['D100']
584+
570585
def run_pep257():
571586
log.setLevel(logging.DEBUG)
572587
opt_parser = get_option_parser()
@@ -589,7 +604,9 @@ def run_pep257():
589604
Error.explain = options.explain
590605
Error.source = options.source
591606
collected = list(collected)
592-
errors = check(collected, ignore=options.ignore.split(','))
607+
checked_codes = get_checked_error_codes(options)
608+
# options.ignore.split(',')
609+
errors = check(collected, checked_codes=checked_codes)
593610
code = 0
594611
count = 0
595612
for error in errors:
@@ -724,7 +741,7 @@ def check_blank_before_after_class(slef, class_, docstring):
724741
docstring.
725742
726743
"""
727-
# NOTE: this gives flase-positive in this case
744+
# NOTE: this gives false-positive in this case
728745
# class Foo:
729746
#
730747
# """Docstring."""

test_pep257.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,19 @@ def foo():
144144

145145
out, err = env.invoke_pep257(args='--count')
146146
assert '2' in out
147+
148+
def test_select_cli():
149+
"""Test choosing error codes with --select."""
150+
with Pep257Env() as env:
151+
with env.open('example.py', 'wt') as example:
152+
example.write(textwrap.dedent("""\
153+
def foo():
154+
pass
155+
"""))
156+
157+
_, err = env.invoke_pep257(args='--select=D100')
158+
assert 'D100' in err
159+
assert 'D103' not in err
160+
161+
def test_select_config():
162+
assert False

0 commit comments

Comments
 (0)