Skip to content

Fix crash caused by invalid format strings in .format context #10300

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 4 commits into from
Mar 24, 2025

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Mar 23, 2025

Type of Changes

Type
βœ“ πŸ› Bug fix
✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

This PR addresses a crash in the consider-using-f-string check that occurs when .format is used with keyword arguments and the format string is malformed. The utils.parse_format_method_string raises an uncaught IncompleteFormatString exception, leading to the unexpected crash.

Another observation is consider-using-f-string is raised when .format is used with positional arguments:

print("{".format(1))  # [consider-using-f-string], [bad-format-string]

In such cases, it seems more appropriate to suppress consider-using-f-string, since the bad-format-string message already covers the issue.

Closes #10282

@zenlyj zenlyj added the Crash πŸ’₯ A bug that makes pylint crash label Mar 23, 2025
Copy link

codecov bot commented Mar 23, 2025

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.86%. Comparing base (a3e5bef) to head (2b16fcf).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10300   +/-   ##
=======================================
  Coverage   95.86%   95.86%           
=======================================
  Files         175      175           
  Lines       19074    19074           
=======================================
  Hits        18286    18286           
  Misses        788      788           
Files with missing lines Coverage Ξ”
...int/checkers/refactoring/recommendation_checker.py 96.56% <100.00%> (ΓΈ)
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment has been minimized.

Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 2b16fcf

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.

Great !

Comment on lines -415 to +425
if isinstance(keyword, nodes.Dict):
if len(keyword.items) > 1 and len(keyword_args) > 1:
return
if (
isinstance(keyword, nodes.Dict)
and len(keyword.items) > 1
and len(keyword_args) > 1
):
return
Copy link
Member

Choose a reason for hiding this comment

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

Nice !

@Pierre-Sassoulas
Copy link
Member

I don't know what's wrong with the failing job, seems like a cache issue where pylint 4.0.0dev0 doesn't refer to the same commit ?

@zenlyj zenlyj changed the title Fix crash caused by invalid format strings under .format context Fix crash caused by invalid format strings in .format context Mar 23, 2025
@Pierre-Sassoulas Pierre-Sassoulas merged commit e6cbd41 into pylint-dev:main Mar 24, 2025
44 of 47 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 24, 2025
Pierre-Sassoulas pushed a commit that referenced this pull request Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AstroidError when checking against sample bad-format-string code
2 participants