Skip to content

Commit

Permalink
Only apply author pattern checks when necessary
Browse files Browse the repository at this point in the history
If we're configured to allow all author patterns (`''` or `.*`), there's
no need to compile and apply our regular expression check.
  • Loading branch information
jparise committed Jan 12, 2017
1 parent 00582ff commit dc798e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changes
=======

1.1.2 (2017-01-12)
------------------

* Only apply the author regular expression pattern check when a pattern has
actually been configured.

1.1.1 (2016-11-06)
------------------

Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Configuration values are specified in the ``[flake8]`` section of your `config
file`_ or as command line arguments (e.g. ``--author-attribute=required``).

- ``author-attribute``: "optional", "required", "forbidden" (default: optional)
- ``author-pattern``: ``__author__`` validation pattern (default: ``.*``)
- ``author-pattern``: ``__author__`` validation `re`_ pattern (default: ``''``)

.. _config file: http://flake8.pycqa.org/en/latest/user/configuration.html
.. _re: https://docs.python.org/library/re.html
21 changes: 13 additions & 8 deletions flake8_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import re

__author__ = 'Jon Parise'
__version__ = '1.1.1'
__version__ = '1.1.2'


class Checker(object):
Expand Down Expand Up @@ -45,7 +45,7 @@ def add_options(cls, parser):
**extra_kwargs)
parser.add_option(
'--author-pattern',
default=r'.*',
default=r'',
help="__author__ attribute validation pattern (regex)",
**extra_kwargs)

Expand All @@ -59,11 +59,15 @@ def parse_options(cls, options):
options.author_attribute,
', '.join(cls.attribute_choices)))

try:
cls.options['pattern'] = re.compile(options.author_pattern)
except re.error as e:
raise ValueError("author-pattern: '{0}': {1}".format(
options.author_pattern, e))
# Only build a regular expression object when we've been configured
# with a pattern that doesn't match all strings. This naively just
# checks for the default empty pattern string as well as `.*`.
if options.author_pattern and options.author_pattern != '.*':
try:
cls.options['pattern'] = re.compile(options.author_pattern)
except re.error as e:
raise ValueError("author-pattern: '{0}': {1}".format(
options.author_pattern, e))

def find_author_node(self, tree):
for node in tree.body:
Expand All @@ -85,7 +89,8 @@ def run(self):
message = 'A401 __author__ attributes are not allowed'
yield node.lineno, node.col_offset, message, type(self)

elif node and not self.options['pattern'].match(node.value.s):
elif (node and 'pattern' in self.options and
not self.options['pattern'].match(node.value.s)):
message = ('A402 __author__ value "{0}" does not match "{1}"'
.format(node.value.s, self.options['pattern'].pattern))
yield node.lineno, node.col_offset, message, type(self)
1 change: 1 addition & 0 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_author_invalid_attribute(self):
def test_author_pattern(self):
author = 'Jon Parise <jon@example.com>'
self.assertIsNone(check(author))
self.assertIsNone(check(author, pattern=r''))
self.assertIsNone(check(author, pattern=r'.*'))

def test_author_pattern_not_matched(self):
Expand Down

0 comments on commit dc798e2

Please sign in to comment.