4nds/pylint-quotes

`AttributeError` (`StringQuoteChecker.config`) with release of `pylint==3.0.0`

Opened this issue · 1 comments

Bug description

When parsing the following utils.py:

"""Utilities for the example project."""


def do_something():
    '''Does something.'''
    return 'something'


def do_something_else():
    '''Does something else.

    Returns:
        str: A string of some sort.
    '''
    return "string1 " + 'string2'

The following error occurs with newer Pylint (>=3.0.0):

Traceback (most recent call last):
  File "d:\Python\Python311\Lib\site-packages\pylint\lint\pylinter.py", line 788, in _lint_file
    check_astroid_module(module)
  File "d:\Python\Python311\Lib\site-packages\pylint\lint\pylinter.py", line 1017, in check_astroid_module
    retval = self._check_astroid_module(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python311\Lib\site-packages\pylint\lint\pylinter.py", line 1067, in _check_astroid_module
    token_checker.process_tokens(tokens)
  File "d:\Python\Python311\Lib\site-packages\pylint_quotes\checker.py", line 263, in process_tokens
    self._process_string_token(token, start_row, start_col)
  File "d:\Python\Python311\Lib\site-packages\pylint_quotes\checker.py", line 289, in _process_string_token
    preferred_quote = SMART_QUOTE_OPTS.get(self.config.string_quote)
                                           ^^^^^^^^^^^
AttributeError: 'StringQuoteChecker' object has no attribute 'config'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "d:\Python\Python311\Lib\site-packages\pylint\lint\pylinter.py", line 752, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "d:\Python\Python311\Lib\site-packages\pylint\lint\pylinter.py", line 790, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Pylint version

pylint 3.0.3
astroid 3.0.3
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]

OS / Environment

win32 (Windows)

Script used

I used the following script to produce the error for debugging purposes, but the same error occurs when running Pylint in command line or using Pylint extension in Visual Studio Code:

"""File for testing pylint."""

import pylint.lint


def check_file(file_path: str) -> int:
    """Function checking file with pylint."""
    if not file_path.endswith('.py'):
        return 0

    pylint_opts = [
        "--load-plugins=pylint_quotes",
        "--string-quote=single-avoid-escape",
        "--triple-quote=single",
        "--docstring-quote=double",
    ]
    runner = pylint.lint.Run(pylint_opts + [file_path], exit=False)
    return runner.linter.msg_status


check_file(r'pylint-quotes/example/foo/utils.py')

The error happens because class StringQuoteChecker, that is subclass of BaseTokenChecker,

class StringQuoteChecker(BaseTokenChecker):
"""Pylint checker for the consistent use of characters in strings.

class BaseTokenChecker inherits from BaseChecker,

pylint/pylint/checkers/base_checker.py

class BaseTokenChecker(BaseChecker):
    """Base class for checkers that want to have access to the token stream."""

class BaseChecker inherits from _ArgumentsProvider,

pylint/pylint/checkers/base_checker.py

class BaseChecker(_ArgumentsProvider):
    # checker name (you may reuse an existing one)

and class _ArgumentsProvider since Pylint version 3.0.0 no longer contains config property which was depreciated since Pylint 2.14.0,

The config attribute of BaseChecker has been deprecated. You can use checker.linter.config to access the global configuration object instead of a checker-specific object.

and later removed in Pylint 3.0.0,

pylint/pylint/config/arguments_provider.py

    @property
    def config(self) -> argparse.Namespace:
        # TODO: 3.0: Remove deprecated attribute
        warnings.warn(
            "The checker-specific config attribute has been deprecated. Please use "
            "'linter.config' to access the global configuration object.",
            DeprecationWarning,
            stacklevel=2,
        )
        return self._arguments_manager.config

Like the deprecation warning says solution is to use linter.config instead of config property. So instead of self.config.string_quote in

preferred_quote = SMART_QUOTE_OPTS.get(self.config.string_quote)

we need to use self.linter.config.string_quote.