Skip to content

Commit

Permalink
finish all merges
Browse files Browse the repository at this point in the history
  • Loading branch information
vsahni3 committed Oct 13, 2023
1 parent d729ffd commit a8065a5
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 330 deletions.
15 changes: 9 additions & 6 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ python_ta.check_all(..., load_default_config=False)

## Custom Error Messages

PythonTA allows for pylint error messages to be overridden with more user friendly messages.
PythonTA allows for pylint error messages to be overridden with more user-friendly messages.
These messages are specified in `config/messages_config.toml` in the source code.
The user can provide their own messages configuration file by specifying `messages-config-path` in their `.pylintrc` file.
Note that the users' custom messages have priority over both pylint's and PythonTA's messages; see the `use-pyta-error-messages`
option below for more info on this.

## Reporters

Expand Down Expand Up @@ -94,19 +96,20 @@ python_ta.check_all(..., output='pyta_output.txt')
This options is compatible with all of PythonTA's reporter types, but we do not recommend its use with ColorReporter,
as this reporter uses terminal-specific characters to colourize text displayed on your screen.

## Overwrite Error Messages
## Use PythonTA's Error Messages

By default, PythonTA overwrites Pylint's error messages with its own to make them more reader-friendly. You can specify whether
you want Pylint's default error messages instead using the `overwrite-error-messages` option. For example, use the following configuration to
see Pylint's error messages:
By default, PythonTA overwrites some of pylint's error messages with its own to make them more beginner-friendly. You can
prevent this by setting the `use-pyta-error-messages` option to `False`.

```python
import python_ta
python_ta.check_all(config={
"overwrite-error-messages": False
"use-pyta-error-messages": False
})
```

Note that any custom messages set using the `messages-config-path` option will not be affected by this configuration.

## Forbidden Imports

By default, PythonTA has a list of modules that are allowed to be imported. You can specify any additional modules using the `extra-imports` configuration option, which you can set in a call to `python_ta.check_all` or in a configuration file.
Expand Down
15 changes: 7 additions & 8 deletions python_ta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,14 @@ def _check(
current_reporter.set_output(output)
messages_config_path = linter.config.messages_config_path
messages_config_default_path = linter._option_dicts["messages-config-path"]["default"]

messages_config = load_messages_config(messages_config_path, messages_config_default_path)
overwrite_error_messages = linter.config.overwrite_error_messages
use_pyta_error_messages = linter.config.use_pyta_error_messages
messages_config = load_messages_config(
messages_config_path, messages_config_default_path, use_pyta_error_messages
)

global PYLINT_PATCHED
if not PYLINT_PATCHED:
patch_all(
messages_config, overwrite_error_messages
) # Monkeypatch pylint (override certain methods)
patch_all(messages_config) # Monkeypatch pylint (override certain methods)
PYLINT_PATCHED = True

# Try to check file, issue error message for invalid files.
Expand Down Expand Up @@ -291,12 +290,12 @@ def reset_linter(
},
),
(
"overwrite-error-messages",
"use-pyta-error-messages",
{
"default": True,
"type": "yn",
"metavar": "<yn>",
"help": "Overwrite the default pylint error messages",
"help": "Overwrite the default pylint error messages with PythonTA's messages",
},
),
)
Expand Down
4 changes: 2 additions & 2 deletions python_ta/config/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pyta-server-address = http://127.0.0.1:5000
# Path to messages_config toml file
# messages-config-path = messages.toml

# Set whether the default error messages by pylint should be overwritten by python TA's custom messages
overwrite-error-messages = yes
# Set whether the default error messages by pylint should be overwritten by PythonTA's custom messages
use-pyta-error-messages = yes

[REPORTS]
# The type of reporter to use to display results. Available PyTA classes are
Expand Down
47 changes: 25 additions & 22 deletions python_ta/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,33 @@ def override_config(linter: PyLinter, config_location: AnyStr) -> None:
linter.config_file = config_location


def load_messages_config(path: str, default_path: str) -> dict:
def load_messages_config(path: str, default_path: str, use_pyta_error_messages: bool) -> dict:
"""Given path (potentially) specified by user and default default_path
of messages config file, merge the config files."""
of messages config file, merge the config files. We will only add the
PythonTA error messages if use_pyta_error_messages is True"""
merge_into = toml.load(default_path)

# assume the user is not going to provide a path which is the same as the default
if Path(default_path).resolve() == Path(path).resolve():
merge_from = {}
else:
try:
merge_from = toml.load(path)
except FileNotFoundError:
print(f"[WARNING] Could not find messages config file at {str(Path(path).resolve())}.")
merge_from = {}

if use_pyta_error_messages:
for category in merge_from:
if category not in merge_into:
merge_into[category] = {}
for checker in merge_from[category]:
if checker not in merge_into[category]:
merge_into[category][checker] = {}
for error_code in merge_from[category][checker]:
merge_into[category][checker][error_code] = merge_from[category][checker][
error_code
]
return merge_into

try:
merge_from = toml.load(path)
except FileNotFoundError:
print(
f"[WARNING] Could not find messages config file at {str(Path(path).resolve())}. Using default messages config file at {str(Path(default_path).resolve())}."
)
return merge_into

for category in merge_from:
if category not in merge_into:
merge_into[category] = {}
for checker in merge_from[category]:
if checker not in merge_into[category]:
merge_into[category][checker] = {}
for error_code in merge_from[category][checker]:
merge_into[category][checker][error_code] = merge_from[category][checker][
error_code
]
return merge_into
else:
return merge_from
5 changes: 2 additions & 3 deletions python_ta/patches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from .transforms import patch_ast_transforms


def patch_all(messages_config: dict, overwrite_error_messages: bool):
def patch_all(messages_config: dict):
"""Execute all patches defined in this module."""
patch_checkers()
patch_ast_transforms()
patch_messages()
if overwrite_error_messages:
patch_error_messages(messages_config)
patch_error_messages(messages_config)
1 change: 0 additions & 1 deletion tests/test_config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"pyta-number-of-messages": 10,
"max-nested-blocks": 5,
"max-line-length": 120,
"overwrite-error-messages": True,
}

# Non-fatal config errors. Fatal errors will be checked individually.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_messages_config/test.messages_config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
["pylint.checkers.base".BasicChecker]
E0111 = "This custom error message is modified."
W0101 = "This custom error message is modified."
131 changes: 0 additions & 131 deletions tests/test_messages_config/test.pylintrc

This file was deleted.

Loading

0 comments on commit a8065a5

Please sign in to comment.