Giter VIP home page Giter VIP logo

flake8-printf-formatting's Introduction

PyPI version Supported Python versions Tests Coverage

flake8-printf-formatting

flake8 plugin which forbids printf-style string formatting

Installation

pip install flake8-printf-formatting

Codes

Code Description
MOD001 do not use printf-style string formatting

Rationale

The official Python 3 documentation doesn't recommend printf-style string formatting:

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

Bad

print("Hello, %s!" % name)

Good

print("Hello, {name}!".format(name=name))

Even better

print(f"Hello, {name}!")

As a pre-commit hook

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.8
    hooks:
    -   id: flake8
        additional_dependencies: [flake8-printf-formatting]

Release process

  1. Bump version in setup.cfg.
  2. Add a commit "Release vX.Y.Z".
  3. Make sure checks still pass.
  4. Draft a new release with a tag name "X.Y.Z" and describe changes which involved in the release.
  5. Publish the release.

flake8-printf-formatting's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flake8-printf-formatting's Issues

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'.

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.)

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.