Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in how PythonTA reports error messages when parsing config files #920

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Bug Fixes

- Fixed bug where running `python3 -m python_ta --generate-config` yields a `FileNotFoundError`.
- Fixed bug in how PythonTA reports error messages that occur when parsing configuration files.

### New checkers

Expand Down
5 changes: 5 additions & 0 deletions python_ta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ def _override_config(linter: PyLinter, config_location: AnyStr) -> None:

Snippets taken from pylint.config.config_initialization.
"""
linter.set_current_module(config_location)

# Read the configuration file.
config_file_parser = _ConfigurationFileParser(verbose=True, linter=linter)
try:
Expand All @@ -235,6 +237,9 @@ def _override_config(linter: PyLinter, config_location: AnyStr) -> None:
except _UnrecognizedOptionError as exc:
print(f"Unrecognized options: {', '.join(exc.options)}", file=sys.stderr)

# Everything has been set up already so emit any stashed messages.
linter._emit_stashed_messages()

linter.config_file = config_location


Expand Down
15 changes: 15 additions & 0 deletions python_ta/reporters/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os.path
import sys
from collections import defaultdict
from pathlib import Path
from typing import Dict, List, Optional, Tuple

from astroid import NodeNG
Expand Down Expand Up @@ -215,6 +216,20 @@ def _colourify(cls, colour_class: str, text: str) -> str:
# Event callbacks
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
"""Hook called when a module starts to be analysed."""
# First, check if `module` is the name of a config file and if so, make filepath the
# corresponding path to that config file.
possible_config_path = Path(os.path.expandvars(module)).expanduser()
config_extensions = [".toml", ".ini", ".pylintrc"]

if possible_config_path.exists():
module_basename = os.path.basename(module)
if (
sushimon marked this conversation as resolved.
Show resolved Hide resolved
any(possible_config_path.suffix == extension for extension in config_extensions)
or module_basename == ".pylintrc"
or module_basename == "pylintrc"
):
filepath = possible_config_path

# Skip if filepath is None
if filepath is None:
return
Expand Down
16 changes: 16 additions & 0 deletions tests/test_config/file_fixtures/test_with_errors.pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[REPORTS]

output-format = python_ta.reporters.HTMLReporter

[FORMAT]

max-line-length = 100

ignore-long-lines = ^\s*((# )?<?https?://\S+>?)|(>>>.*)$

[FORBIDDEN IMPORT]
extra-imports = math, tkinter

[MESSAGES CONTROL]
disable =
ooga
30 changes: 30 additions & 0 deletions tests/test_config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,33 @@ def test_default_pylint_checks_in_no_default(configure_linter_no_default) -> Non
assert all(
check not in disabled_checks for check in previously_disabled_checks if disabled_checks
)


def test_unknown_option_value() -> None:
"""Test that the configuration options gets overridden without error when there is an unknown
option value."""
curr_dir = os.path.dirname(__file__)
config = os.path.join(curr_dir, "file_fixtures", "test_with_errors.pylintrc")
reporter = python_ta.reset_linter(config=config).reporter

# Check if there are any messages with the msg_id `W0012` (the code corresponding to the error
# `unknown-option-value`.
message_ids = [msg.msg_id for message_lis in reporter.messages.values() for msg in message_lis]

assert "W0012" in message_ids


def test_unknown_option_value_no_default() -> None:
"""Test that the configuration options gets loaded without error when there is an unknown option
value.

The default options are not loaded from the PythonTA default config."""
curr_dir = os.path.dirname(__file__)
config = os.path.join(curr_dir, "file_fixtures", "test_with_errors.pylintrc")
reporter = python_ta.reset_linter(config=config, load_default_config=False).reporter

# Check if there are any messages with the msg_id `W0012` (the code corresponding to the error
# `unknown-option-value`.
message_ids = [msg.msg_id for message_lis in reporter.messages.values() for msg in message_lis]

assert "W0012" in message_ids