summaryrefslogtreecommitdiff
path: root/pylint/checkers
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-03-29 14:44:27 +0200
committerGitHub <noreply@github.com>2022-03-29 14:44:27 +0200
commit0bc45e9037416203bd11f83b84a906fcec1a47f0 (patch)
treeaed06a43f53926fdb2b4045b381e31df9f2247ac /pylint/checkers
parent700752a03604e4fe1b73e5f4200649c157e6eaaa (diff)
downloadpylint-git-0bc45e9037416203bd11f83b84a906fcec1a47f0.tar.gz
Create an `Argument` class and allow convertion of optdict into them (#5584)
* Use config initialization of ``_ArgumentsManager`` * Allow ``BaseChecker`` to register on a ``_ArgumentsManager`` * Use the ``argparse`` config handler in ``logging.py`` and add tests
Diffstat (limited to 'pylint/checkers')
-rw-r--r--pylint/checkers/base_checker.py19
-rw-r--r--pylint/checkers/logging.py7
2 files changed, 23 insertions, 3 deletions
diff --git a/pylint/checkers/base_checker.py b/pylint/checkers/base_checker.py
index 1cdc014fc..21c63364a 100644
--- a/pylint/checkers/base_checker.py
+++ b/pylint/checkers/base_checker.py
@@ -3,18 +3,25 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
import functools
+import sys
from inspect import cleandoc
from typing import Any, Optional
from astroid import nodes
from pylint.config import OptionsProviderMixIn
+from pylint.config.exceptions import MissingArgumentManager
from pylint.constants import _MSG_ORDER, WarningScope
from pylint.exceptions import InvalidMessageError
from pylint.interfaces import Confidence, IRawChecker, ITokenChecker, implements
from pylint.message.message_definition import MessageDefinition
from pylint.utils import get_rst_section, get_rst_title
+if sys.version_info >= (3, 8):
+ from typing import Literal
+else:
+ from typing_extensions import Literal
+
@functools.total_ordering
class BaseChecker(OptionsProviderMixIn):
@@ -32,16 +39,26 @@ class BaseChecker(OptionsProviderMixIn):
# mark this checker as enabled or not.
enabled: bool = True
- def __init__(self, linter=None):
+ def __init__(
+ self, linter=None, *, future_option_parsing: Literal[None, True] = None
+ ):
"""Checker instances should have the linter as argument.
:param ILinter linter: is an object implementing ILinter.
+ :raises MissingArgumentManager: If no linter object is passed.
"""
if self.name is not None:
self.name = self.name.lower()
super().__init__()
self.linter = linter
+ if future_option_parsing:
+ # We need a PyLinter object that subclasses _ArgumentsManager to register options
+ if not self.linter:
+ raise MissingArgumentManager
+
+ self.linter._register_options_provider(self)
+
def __gt__(self, other):
"""Permit to sort a list of Checker by name."""
return f"{self.name}{self.msgs}".__gt__(f"{other.name}{other.msgs}")
diff --git a/pylint/checkers/logging.py b/pylint/checkers/logging.py
index 743f40db8..d9d73d3c1 100644
--- a/pylint/checkers/logging.py
+++ b/pylint/checkers/logging.py
@@ -144,15 +144,18 @@ class LoggingChecker(checkers.BaseChecker):
),
)
+ def __init__(self, linter: "PyLinter") -> None:
+ super().__init__(linter=linter, future_option_parsing=True)
+
def visit_module(self, _: nodes.Module) -> None:
"""Clears any state left in this checker from last module checked."""
# The code being checked can just as easily "import logging as foo",
# so it is necessary to process the imports and store in this field
# what name the logging module is actually given.
self._logging_names: Set[str] = set()
- logging_mods = self.config.logging_modules
+ logging_mods = self.linter.namespace.logging_modules
- self._format_style = self.config.logging_format_style
+ self._format_style = self.linter.namespace.logging_format_style
self._logging_modules = set(logging_mods)
self._from_imports = {}