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

Add warning on invalid percent markers; ValueError: unsupported format character #689

Closed
asottile opened this issue Apr 3, 2021 · 3 comments

Comments

@asottile
Copy link
Member

asottile commented Apr 3, 2021

In GitLab by @pjacock on Mar 4, 2019, 06:44

Please read this brief portion of documentation before going any further: http://flake8.pycqa.org/en/latest/internal/contributing.html#filing-a-bug

Please describe how you installed Flake8

$ pip install flake8

Please describe the problem or feature

Consider this example, call it str_fmt.py:

"""String formatting test case.

The percent j should be percent i for integer.
"""

a = 123
b = "Hello"
c = "World"

print("%j\t%s\t%s\n" % (a, b, c))

Currently flake8 does not report any problem with this file:

$ flake8 str_fmt.py ; echo "Return code $?"
Return code 0

However, on running it:

$ python str_fmt.py
Traceback (most recent call last):
  File "str_fmt.py", line 10, in <module>
    print("%j\t%s\t%s\n" % (a, b, c))
ValueError: unsupported format character 'j' (0x6a) at index 1

Please provide the exact, unmodified output of flake8 --bug-report

Not a bug, but for reference:

$ flake8 --version
3.6.0 (black: 0.0.1, flake8-blind-except: 0.1.1, flake8-bugbear: 18.8.0, flake8-docstrings: 1.3.0, pydocstyle: 3.0.0, mccabe: 0.6.1, pycodestyle: 2.4.0, pyflakes: 2.0.0, rst-docstrings: 0.0.8) CPython 3.7.2 on Linux

Please describe the problem or feature

Static code analysis could spot this kind of error and prevent ValueError: unsupported format character exceptions. In my experience this kind of error is particularly common in corner cases for error handling or logging code which is not exhaustively tested.

This is analogous to the flake8 warning akin to W605 invalid escape sequence (although that does not usually trigger an exception), but for invalid percent placeholders.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Mar 4, 2019, 06:47

Flake8 is merely a framework which other linters run under and does not implement any rules itself. I'd suggest either writing a plugin or reporting this to pyflakes / pycodestyle. I think there's something underway in pyflakes for this already : PyCQA/pyflakes#370

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Mar 4, 2019, 06:47

closed

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @pjacock on Mar 4, 2019, 06:56

Thank you - I used slightly different search terms so didn't find that. Also, I wasn't sure if it would fall under pyflakes or pycodestyle, so reported it here.

@asottile asottile closed this as completed Apr 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant