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

Commit cd1747f

Browse files
committed
Added error registry.
1 parent 1979ae1 commit cd1747f

File tree

1 file changed

+77
-44
lines changed

1 file changed

+77
-44
lines changed

pep257.py

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -442,50 +442,83 @@ def __lt__(self, other):
442442
return (self.filename, self.line) < (other.filename, other.line)
443443

444444

445-
errors = []
446-
447-
448-
def register_error(error_cls):
449-
errors.append(error_cls)
450-
451-
452-
def error(code, message):
453-
_code = code
454-
455-
class _Error(Error):
456-
code = _code
457-
_message = message
458-
459-
register_error(_Error)
460-
return _Error
461-
462-
463-
D100 = error('D100', 'Missing docstring in public module')
464-
D101 = error('D101', 'Missing docstring in public class')
465-
D102 = error('D102', 'Missing docstring in public method')
466-
D103 = error('D103', 'Missing docstring in public function')
467-
D200 = error('D200', 'One-line docstring should fit on one line with quotes, '
468-
'found %s')
469-
D201 = error('D201', 'No blank lines allowed before function docstring, '
470-
'found %s')
471-
D202 = error('D202', 'No blank lines allowed after function docstring, '
472-
'found %s')
473-
D203 = error('D203', '1 blank line required before class docstring, found %s')
474-
D204 = error('D204', '1 blank line required after class docstring, found %s')
475-
D205 = error('D205', '1 blank line required between summary line and '
476-
'description, found %s')
477-
D206 = error('D206', 'Docstring should be indented with spaces, not tabs')
478-
D207 = error('D207', 'Docstring is under-indented')
479-
D208 = error('D208', 'Docstring is over-indented')
480-
D209 = error('D209', 'Multi-line docstring closing quotes should be on a '
481-
'separate line')
482-
D210 = error('D210', 'No whitespaces allowed surrounding docstring text')
483-
D300 = error('D300', 'Use """triple double quotes""", found %s-quotes')
484-
D301 = error('D301', 'Use r""" if any backslashes in a docstring')
485-
D302 = error('D302', 'Use u""" for Unicode docstrings')
486-
D400 = error('D400', 'First line should end with a period, not %r')
487-
D401 = error('D401', 'First line should be in imperative mood: %r, not %r')
488-
D402 = error('D402', 'First line should not be the function\'s "signature"')
445+
class ErrorRegistry(object):
446+
groups = []
447+
448+
class ErrorGroup(object):
449+
errors = []
450+
451+
def __init__(self, prefix, name):
452+
self.prefix = prefix
453+
self.name = name
454+
455+
def create_error(self, error_code, error_message):
456+
# TODO: check prefix
457+
458+
class _Error(Error):
459+
code = error_code
460+
_message = error_message
461+
462+
self.errors.append(_Error)
463+
return _Error
464+
465+
@classmethod
466+
def create_group(cls, prefix, name):
467+
group = cls.ErrorGroup(prefix, name)
468+
cls.groups.append(group)
469+
return group
470+
471+
@classmethod
472+
def get_error_codes(cls):
473+
# TODO: implement
474+
pass
475+
476+
@classmethod
477+
def to_rst(cls):
478+
# TODO: implement
479+
pass
480+
481+
482+
D1xx = ErrorRegistry.create_group('D1', 'Missing Docstrings')
483+
D100 = D1xx.create_error('D100', 'Missing docstring in public module')
484+
D101 = D1xx.create_error('D101', 'Missing docstring in public class')
485+
D102 = D1xx.create_error('D102', 'Missing docstring in public method')
486+
D103 = D1xx.create_error('D103', 'Missing docstring in public function')
487+
488+
D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues')
489+
D200 = D2xx.create_error('D200', 'One-line docstring should fit on one line '
490+
'with quotes, found %s')
491+
D201 = D2xx.create_error('D201', 'No blank lines allowed before function '
492+
'docstring, found %s')
493+
D202 = D2xx.create_error('D202', 'No blank lines allowed after function '
494+
'docstring, found %s')
495+
D203 = D2xx.create_error('D203', '1 blank line required before class '
496+
'docstring, found %s')
497+
D204 = D2xx.create_error('D204', '1 blank line required after class '
498+
'docstring, found %s')
499+
D205 = D2xx.create_error('D205', '1 blank line required between summary line '
500+
'and description, found %s')
501+
D206 = D2xx.create_error('D206', 'Docstring should be indented with spaces, '
502+
'not tabs')
503+
D207 = D2xx.create_error('D207', 'Docstring is under-indented')
504+
D208 = D2xx.create_error('D208', 'Docstring is over-indented')
505+
D209 = D2xx.create_error('D209', 'Multi-line docstring closing quotes should '
506+
'be on a separate line')
507+
D210 = D2xx.create_error('D210', 'No whitespaces allowed surrounding '
508+
'docstring text')
509+
510+
D3xx = ErrorRegistry.create_group('D3', 'Quotes Issues')
511+
D300 = D3xx.create_error('D300', 'Use """triple double quotes""", found '
512+
'%s-quotes')
513+
D301 = D3xx.create_error('D301', 'Use r""" if any backslashes in a docstring')
514+
D302 = D3xx.create_error('D302', 'Use u""" for Unicode docstrings')
515+
516+
D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')
517+
D400 = D4xx.create_error('D400', 'First line should end with a period, not %r')
518+
D401 = D4xx.create_error('D401', 'First line should be in imperative mood: '
519+
'%r, not %r')
520+
D402 = D4xx.create_error('D402', 'First line should not be the function\'s '
521+
'"signature"')
489522

490523

491524
def get_option_parser():

0 commit comments

Comments
 (0)