Skip to content
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

bad-string-format-type: only works on old-style string formatting and can't infer values #6163

Open
DudeNr33 opened this issue Apr 3, 2022 · 3 comments
Assignees
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@DudeNr33
Copy link
Collaborator

DudeNr33 commented Apr 3, 2022

Bug description

Two issues with bad-string-format-type:

  1. Just like bad-format-character the bad-string-format-type message is currently only raised for old-style string formatting.
  2. If only a single value needs to be formatted, the check does not work if the value to format is passed in as a variable. Using variables in tuples however is fine.

Given a file a.py:

# pylint: disable=consider-using-f-string,missing-module-docstring
WORD = "abc"
print("%d" % "abc")  # works
print("%d %d" % (WORD, 1))  # works
print("%d", WORD)  # doesn't work
print("{:d}".format("abc"))  # doesn't work
print(f"{'abc':d}")  #  doesn't work

Configuration

``pylintrc`` from ``pylint`` git repo.

Command used

pylint a.py

Pylint output

************* Module a
.notes/a.py:3:6: E1307: Argument 'builtins.str' does not match format type 'd' (bad-string-format-type)
.notes/a.py:4:6: E1307: Argument 'builtins.str' does not match format type 'd' (bad-string-format-type)

Expected behavior

All of the examples should raise the message.

Pylint version

pylint 2.14.0-dev0
astroid 2.11.2
Python 3.10.2 (main, Apr  3 2022, 14:46:07) [Clang 12.0.5 (clang-1205.0.22.9)]

OS / Environment

macOS BigSur 11.6

Additional dependencies

No response

@DudeNr33 DudeNr33 added Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code labels Apr 3, 2022
@DudeNr33
Copy link
Collaborator Author

DudeNr33 commented Apr 3, 2022

When this is resolved, the doc/data/messages/b/bad-string-format-type/details.rst must be updated.

Two hints so far:

            elif isinstance(args, nodes.Name):
                args_elts = [args]
                num_args = 1

@Pierre-Sassoulas Pierre-Sassoulas added Help wanted 🙏 Outside help would be appreciated, good for new contributors Good first issue Friendly and approachable by new contributors labels Apr 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jun 24, 2022
@clavedeluna clavedeluna self-assigned this Nov 30, 2022
@clavedeluna
Copy link
Contributor

clavedeluna commented Nov 30, 2022

@DudeNr33

print("%d", WORD) # doesn't work

is that third example which you also mention

third example print("%d" % WORD), we end up in this else block

supposed to be

print("%d" % WORD) # doesn't work ?

Because print("%d", WORD) doesn't emit a Python error though it does look like a mistake %d abc

Also

When using a variable as format value (third example print("%d" % WORD), we end up in this else block

has already been fixed.

So now the tests to correctly get to emit the msgs are these

WORD = "abc"
print("{:d}".format("abc"))  # [bad-string-format-type]
print(f"{'abc':d}")  # [bad-string-format-type]

@DudeNr33
Copy link
Collaborator Author

DudeNr33 commented Dec 1, 2022

@clavedeluna thanks for spotting this. You are correct, that's a typo and it should have been print("%d" % WORD).

If some of the problems are already fixed, even better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
3 participants