Skip to content

Every option in setup.cfg is loaded, even when the section is unknown to pylint #4371

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

Closed
SabaSCP opened this issue Apr 17, 2021 · 4 comments · Fixed by #6356
Closed

Every option in setup.cfg is loaded, even when the section is unknown to pylint #4371

SabaSCP opened this issue Apr 17, 2021 · 4 comments · Fixed by #6356
Labels
Bug 🪲 Configuration Related to configuration
Milestone

Comments

@SabaSCP
Copy link

SabaSCP commented Apr 17, 2021

This is a slightly different issue from what @mteiste has seen with #4272 . My issue is with all sections and options defined in setup.cfg are processed/loaded, even if those sections are for other tools and unknown to pylint.

This has the effect of setting pylint's ignore to D107,D400,D401.

Going through the code, it feels like the problem is somewhere in option_manager_mixin.py, either at read_config_line line 298:299 or at load_config_file line 312.

Apparently, Toml files are not affected because the logic is set to read from content["tool"]["pylint"]. But for setup.cfg there's no try / except block, iterating through every section:

if sect.startswith("pylint."):
	sect = sect[len("pylint.") :]
if not sect.isupper() and values:
	parser._sections[sect.upper()] = values

I think pylint should filtered / excluded any section that's not part of any pylint group -> self._mygroups.

Steps to reproduce

Given a config file setup.cfg

[pylint.master]
ignore = migrations

[flake8]
ignore = D107,D400,D401

Current behavior

Result of pylint --generate-rcfile | grep -A2 'ignore=':

ignore=D107,
       D400,
       D401

-->

Expected behavior

Result of pylint --generate-rcfile | grep -A2 'ignore=':

ignore=migrations

pylint --version output

Result of pylint --version output:

pylint 2.7.4
astroid 2.5.3
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]

Workaround

I had to monkey patch https://github.com/PyCQA/pylint/blob/master/pylint/config/option_manager_mixin.py#L308 with

        if section not in self._mygroups:
            continue

Resulting in:

def load_config_file(self):
    """Dispatch values previously read from a configuration file to each
    options provider)"""
    parser = self.cfgfile_parser
    for section in parser.sections():
        if section not in self._mygroups:
            continue
        for option, value in parser.items(section):
            try:
                self.global_set_option(option, value)
            except (KeyError, optparse.OptionError):
                continue
@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 Configuration Related to configuration labels Apr 17, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue. Do you want to open a merge request with the proposed changes ? :)

@SabaSCP
Copy link
Author

SabaSCP commented Apr 20, 2021

I wanted to get some insights / feedback first before jumping with the fix. I'm mostly concern on who's filling self._mygroups and if any external plugins are registered to self._mygroups.

@marsfan
Copy link

marsfan commented Aug 24, 2021

I can confirm that this is indeed an issue. I have a single setup.cfg file with both pylint and flake8 configuration options. I want to ignore a specific subdirectory with the ignore=example setting in [pylint.MASTER]

Elsewhere in my configuration file I have a [flake8] section with the option ignore=E303 setting. Pylint reads this and sets its ignore setting to E303 instead of example

It seems that pylint is not properly scanning only its own sections in the config file.

Interestingly enough, re-ordering the sections makes no difference. if the [flake8] section is above [pylint.MASTER] the results is the same as when [flake8] is below '[pylint.MASTER]'

@Pierre-Sassoulas
Copy link
Member

any external plugins are registered to self._mygroups.

Good point, yes, I think so. The way it's done right now each plugin has his own section in the configuration so it can have arbitrary name added by external code. Theorically a flake8 plugin could be added. We might have to make a breaking change to fix this, and it does not seem easy.

Thinking about two solutions on the top of my head:

  • Adding pylint as a suffix to current configuration for unknown plugin group. Ie. : limited list of known plugin in order to not force MASTER to be changed to pylint.MASTER so we don't break all the conf file ever done for pylint, but pylint.external_plugin would be required instead of external_plugin. And then we do not load everything obviously.
  • Breaking change for 3.0 where we only use pyproject.toml and provide a converter for old configurations, then we do not bother fixing 3 conf formats in 4 possible files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Configuration Related to configuration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants