Skip to content

Pylint fix for invalid TOML config #4720

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

Merged
merged 7 commits into from
Nov 13, 2021

Conversation

tanvimoharir
Copy link
Contributor

@tanvimoharir tanvimoharir commented Jul 19, 2021

Steps

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Description

This is a fix for handling bad or invalid toml configuration in pyproject.toml

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Related Issue

Closes #4580

@coveralls
Copy link

coveralls commented Jul 19, 2021

Pull Request Test Coverage Report for Build 1456420246

  • 8 of 11 (72.73%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 93.386%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/config/option_manager_mixin.py 8 11 72.73%
Files with Coverage Reduction New Missed Lines %
pylint/extensions/docparams.py 3 97.74%
Totals Coverage Status
Change from base Build 1454801709: -0.02%
Covered Lines: 13879
Relevant Lines: 14862

💛 - Coveralls

@tanvimoharir tanvimoharir changed the title Adding check for dict Pylint fix for invalid TOML config Jul 19, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Crash 💥 A bug that makes pylint crash label Jul 19, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.4 milestone Jul 19, 2021
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test with the failing toml, please ? :)

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.9.4, 2.9.5 Jul 19, 2021
@tanvimoharir
Copy link
Contributor Author

Could you add a test with the failing toml, please ? :)

Yes I will do that, I also need to accommodate the other failed case which was AttributeError: 'DynamicInlineTableDict' object has no attribute 'find'

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.9.5, 2.9.6 Jul 21, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Configuration Related to configuration label Jul 21, 2021
@tanvimoharir
Copy link
Contributor Author

@Pierre-Sassoulas I added three tests, one for empty list and two more for the other two cases mentioned in #4580
However for the case for AttributeError: 'DynamicInlineTableDict' object has no attribute 'find' its still crashing.

Comment on lines 112 to 136
def test_toml_with_invalid_data_for_imports(tmp_path):
# This would test config with invalid data for imports section
# refer #4580
config_file = tmp_path / "pyproject.toml"
config_file.write_text(
"""
[tool.pylint."imports"]
preferred-modules = { "a"="b" }
"""
)
with pytest.raises(AttributeError):
check_configuration_file_reader(config_file)


def test_toml_with_invalid_data_for_basic(tmp_path):
# This would test config with invalid data for basic section
# refer #4580
config_file = tmp_path / "pyproject.toml"
config_file.write_text(
"""
[tool.pylint."basic"]
name-group = { "a"="b" }
"""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per my other comment these two cases are still crashing.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests looks nice, do you need help to fix the use case that is failing ?

@tanvimoharir
Copy link
Contributor Author

The tests looks nice, do you need help to fix the use case that is failing ?

I have attempted a fix.

@tanvimoharir
Copy link
Contributor Author

The tests looks nice, do you need help to fix the use case that is failing ?

Yes, it would be great if you help me with this. I have tried few things and they dont seem to be working although the tests pass when I executed locally.

@Pierre-Sassoulas
Copy link
Member

I think we need to define better what is expected in the pyproject.toml for pylint. The current crash might be due to disable = "logging-not-lazy,logging-format-interpolation" not being inside "MESSAGES CONTROL". But we'd need to have proper error message when something is misplaced, because right now it's not very convenient and it's just crashing when the configuration is not correct.

I guess the expected configuration for the failing test should be:

[tool.pylint]
    [tool.pylint."messages control"]
        disable = "logging-not-lazy,logging-format-interpolation"
    [tool.pylint."master"]
        load-plugins = []

(Correct me if I'm wrong, I'm no toml expert. But when using the current conf instead of crashing we should have an error message like:

Disable is not a valid section for pylint, did you mean '[tool.pylint."messages control"]  disable = ...' ?

@tanvimoharir
Copy link
Contributor Author

I think we need to define better what is expected in the pyproject.toml for pylint. The current crash might be due to disable = "logging-not-lazy,logging-format-interpolation" not being inside "MESSAGES CONTROL". But we'd need to have proper error message when something is misplaced, because right now it's not very convenient and it's just crashing when the configuration is not correct.

I guess the expected configuration for the failing test should be:

[tool.pylint]
    [tool.pylint."messages control"]
        disable = "logging-not-lazy,logging-format-interpolation"
    [tool.pylint."master"]
        load-plugins = []

(Correct me if I'm wrong, I'm no toml expert. But when using the current conf instead of crashing we should have an error message like:

Disable is not a valid section for pylint, did you mean '[tool.pylint."messages control"]  disable = ...' ?

Thanks @Pierre-Sassoulas I have updated with the configuration you have specified here. I will look more into TOML types since I'm not that familiar with it.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.9.6, 2.10.1 Jul 25, 2021
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.10.2, 2.10.x Aug 21, 2021
@tanvimoharir
Copy link
Contributor Author

@Pierre-Sassoulas Could you help me proceed with this ? I'm not able to understand it clearly.

@Pierre-Sassoulas
Copy link
Member

Hey sorry for the time it took to answer this. It look like the configuration from the toml is not taken into account like before as some integration tests are failing. If I were you I would add unit test on the TOML configuration in order to understand what is going wrong in the integrations tests first. Do not hesitate to ask if there is something specific that is blocking you.

@tanvimoharir
Copy link
Contributor Author

Hey sorry for the time it took to answer this. It look like the configuration from the toml is not taken into account like before as some integration tests are failing. If I were you I would add unit test on the TOML configuration in order to understand what is going wrong in the integrations tests first. Do not hesitate to ask if there is something specific that is blocking you.

The current failures which i see through via pipeline details are because of the tests which I added.
Do you think I should remove those?
Let me know if I'm missing something.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for review following #5276 and #5277.

@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as ready for review November 8, 2021 09:18
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change (like previously) looks good! I like how we are now handling most errors while still continuing.

Mostly some comments about the tests.

@@ -0,0 +1,7 @@
# Both disable and load-plugins do not belong in the imports section
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how we do not detect anything here, but we should.

tanvimoharir and others added 2 commits November 12, 2021 22:29
Add test for current pyproject.toml issues

Add a 'bad-configuration-option-value' message for bad toml configuration

We do not catch all the problem in toml because we don't know what
is expected so we can't recommend. See pylint-dev#5259

We can detect bad top level option when reading the toml
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following #5287 this became a more manageable change, it's ready for review @DanielNoord :)

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand how the new checker works, or at least: how its description fits the single case where it can be emitted in option_manager_mixin.

if not isinstance(values, dict):
# This class is a mixin the add_message come from the pylinter
self.add_message( # type: ignore
"bad-configuration-option-value", line=0, args=(section, values)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably overlooking something really obvious, but how does this check whether load-plugins = [] should be in the tools.pylint section (as one of the tests does)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing directly in the tools.pylint except header (master, message controls, etc), so at top level, if it's not a dict it can't be right. We know nothing about what to expect in the configuration except that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps bad-configuration-section would be better then? For me option-value implies there is something wrong with the type of the option value (but that might be just me).

I think such a check (option value types) would be helpful anyway, if we don't already check it, so reserving bad-configuration-option-value for that checker would be better.
Putting stuff in the wrong section and giving stuff the incorrect type are different mistakes and require different messages. For the the latter we could (for example) also include the expected type in the message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'm going to change the name and upgrade the tests.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what the message is checking now. I think renaming and rewording the description might help future confusion. Rest looks good to go!

if not isinstance(values, dict):
# This class is a mixin the add_message come from the pylinter
self.add_message( # type: ignore
"bad-configuration-option-value", line=0, args=(section, values)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps bad-configuration-section would be better then? For me option-value implies there is something wrong with the type of the option value (but that might be just me).

I think such a check (option value types) would be helpful anyway, if we don't already check it, so reserving bad-configuration-option-value for that checker would be better.
Putting stuff in the wrong section and giving stuff the incorrect type are different mistakes and require different messages. For the the latter we could (for example) also include the expected type in the message.

@Pierre-Sassoulas Pierre-Sassoulas merged commit 1a20bc5 into pylint-dev:main Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Configuration Related to configuration Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pylint crashes for pyproject.toml configuration mixed with another tool Pylint Crash on invalid TOML config
4 participants