Skip to content

Commit

Permalink
Raise meaningful exception for invalid reporter class being selected (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rogalski committed Apr 28, 2017
1 parent 3b3bfb1 commit cb6e452
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
7 changes: 6 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,12 @@ Pylint's ChangeLog
What's New in Pylint 1.8?
=========================

Release date: TBD
* Raise meaningful exception for invalid reporter class being selected

When unknown reporter class will be selected as Pylint reporter,
meaningful error message would be raised instead of bare ``ImportError``
or ``AttribueError`` related to module or reporter class being not found.
Close #1388


What's New in Pylint 1.7.1?
Expand Down
3 changes: 2 additions & 1 deletion doc/whatsnew/1.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 9,5 @@
Summary -- Release highlights
=============================

* None yet.
* Raise meaningful exception in case of invalid reporter class (output format)
being selected.
3 changes: 3 additions & 0 deletions pylint/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 13,6 @@ class UnknownMessageError(Exception):

class EmptyReportError(Exception):
"""raised when a report is empty and so should not be displayed"""

class InvalidReporterError(Exception):
"""raised when selected reporter is invalid (e.g. not found)"""
20 changes: 14 additions & 6 deletions pylint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 473,20 @@ def _load_reporter(self):
if name in self._reporters:
self.set_reporter(self._reporters[name]())
else:
qname = self._reporter_name
module = modutils.load_module_from_name(
modutils.get_module_part(qname))
class_name = qname.split('.')[-1]
reporter_class = getattr(module, class_name)
self.set_reporter(reporter_class())
try:
reporter_class = self._load_reporter_class()
except (ImportError, AttributeError):
raise exceptions.InvalidReporterError(name)
else:
self.set_reporter(reporter_class())

def _load_reporter_class(self):
qname = self._reporter_name
module = modutils.load_module_from_name(
modutils.get_module_part(qname))
class_name = qname.split('.')[-1]
reporter_class = getattr(module, class_name)
return reporter_class

def set_reporter(self, reporter):
"""set the reporter used to display messages and reports"""
Expand Down
7 changes: 7 additions & 0 deletions pylint/test/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 28,7 @@
from pylint.reporters import text
from pylint import checkers
from pylint.checkers.utils import check_messages
from pylint import exceptions
from pylint import interfaces
import pytest

Expand Down Expand Up @@ -390,6 391,12 @@ def test_report_output_format_aliased(linter):
assert linter.reporter.__class__.__name__ == 'TextReporter'


def test_set_unsupported_reporter(linter):
text.register(linter)
with pytest.raises(exceptions.InvalidReporterError):
linter.set_option('output-format', 'missing.module.Class')


def test_set_option_1(linter):
linter.set_option('disable', 'C0111,W0234')
assert not linter.is_message_enabled('C0111')
Expand Down

0 comments on commit cb6e452

Please sign in to comment.