Coder Social home page Coder Social logo

flake8-printf-formatting's Issues

Should not trigger on byte strings

(Partially copied from MichaelKim0407/flake8-use-fstring#22, as this plugin also suffers from the same issue.)

The following code triggers a MOD001 error:

a = 42
b"y=%d" % a

However, using an f-string with a byte string is a syntax error:

>>> bf"y={a}"
  File "<stdin>", line 1
    bf"y={a}"
      ^^^^^^^
SyntaxError: invalid syntax

And .format() is not an option either:

>>> b"y={}".format(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'format'

The only other option is .join() or string concatenation, which are less elegant IMO.

(I could also do "y={}".format(a).encode("utf-8"), but that doesn't always work right if there are byte literals in the string.)

Add option to allow printf formatting if string contains braces

Generally I prefer using f-strings or .format(), but when the string contains { and/or } I find it cleaner to just use %-formatting instead:

"bar_format": "{l_bar}{bar:%d}{r_bar}" % (columns - 42),

The alternatives are much harder to read:

"bar_format": f"{{l_bar}}{{bar:{columns - 42}}}{{r_bar}}"
"bar_format": "{{l_bar}}{{bar:{:d}}}{{r_bar}}".format(columns - 42)

It would be nice to have an option to automatically allow %-formatting in such cases without having to add # noqa.

ModuleNotFoundError: No module named 'importlib_metadata'

In Python 3.8, the importlib_metadata package was implemented in the standard library as importlib.metadata (Python docs)

You may want to add this snippet to flake8_printf_formatting.py and update the install requires in setup.cfg

try:
    import importlib.metadata as importlib_metadata
except ImportError:
    import importlib_metadata  # python_version<'3.8'

I can also submit a PR if that would be of interest


For searchability, this is the error from flake8: flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "MOD" due to No module named 'importlib_metadata'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.