Skip to main content

Python docstring reStructuredText (RST) validator

Project description

Released on the Python Package Index (PyPI) Released on Conda Testing with TravisCI PyPI downloads Code style: black

Introduction

This is an MIT licensed flake8 plugin for enforcing a Python string formatting preference. It is available to install from the Python Package Index (PyPI):

For historical reasons, the Python programming language has accumlated multiple ways to do string formatting. The three main ones are:

  • Percent operator, as in this example:

    >>> name = "Peter"
    >>> price = 1.2
    >>> print("Hello %s, do you have $%0.2f?" % (name, price))
    Hello Peter, do you have $1.20?
    
  • Format method, available since Python 2.6,

    >>> name = "Peter"
    >>> price = 1.2
    >>> print("Hello {}, do you have ${:0.2f}?".format(name, price))
    Hello Peter, do you have $1.20?
    
  • f-strings, available since Python 3.6,

    >>> name = "Peter"
    >>> price = 1.2
    >>> print(f"Hello {name}, do you have ${price:0.2f}?")
    

We are spoilt for choice, but quoting the Zen of Python, There should be one - and preferably only one - obvious way to do it. This flake8 plugin exists to let you define which of these styles your project allows.

For example, if you accept that f-strings are best, you could run a tool like flynt to automatically convert all your code - and then use this flake8 plugin to enforce the style.

Alternatively, you might have a large legacy codebase with lots of the percent formatting - yet want to gradually move to f-strings. Here you coudl use this plugin to enforce a rule allowing those but rejecting the string format method.

By default this plugin complains about all three styles - we expect you to make an explicit choice and configure which codes to ignore.

Flake8 Validation codes

Early versions of flake8 assumed a single character prefix for the validation codes, which became problematic with collisions in the plugin ecosystem. Since v3.0, flake8 has supported longer prefixes therefore this plugin uses SFS as its prefix (short for String Format Style).

Code

Description

SFS100

Bytes literal formatting using percent operator.

SFS101

String literal formatting using percent operator.

SFS102

String literal formatting using .format method.

SFS103

String literal formatting using f-string.

Installation and usage

Python 3.6 or later is required (as we use need to parse the Python syntax which may include f-strings).

We recommend installing the plugin using pip, which handles the dependencies:

$ pip install flake8-sfs

Alternatively, if you are using the Anaconda packaging system, the following command will install the plugin with its dependencies:

$ conda install -c conda-forge flake8-sfs

The new validator should be automatically included when using flake8 which may now report additional validation codes starting with SFS (as defined above). For example:

$ flake8 example.py

You can request only the SFS codes be shown using:

$ flake8 --select SFS example.py

Similarly you might add particular SFS validation codes to your flake8 configuration file’s select or ignore list.

Configuration

We assume you are familiar with flake8 configuration.

Unless your code performs no string formatting at all (which would be unusual), you should tell flake8 to ignore at least one of this plugin’s violation codes. For example:

[flake8]
extend-ignore =
    # Ignore f-strings, we like them:
    SFS103,

Note that flake8 allows splitting comma separated lists over multiple lines, and allows including of hash comment lines.

Version History

Version

Released

Changes

v0.0.1

2020-01-11

  • Initial public release.

Developers

This plugin is on GitHub at https://github.com/peterjc/flake8-sfs

To make a new release once tested locally and on TravisCI:

$ git tag vX.Y.Z
$ python setup.py sdist --formats=gztar
$ twine upload dist/flake8-sfs-X.Y.Z.tar.gz
$ git push origin master --tags

The PyPI upload should trigger an automated pull request updating the flake8-sfs conda-forge recipe.

TODO

  • Can/should we spot byte formatting?

  • Can we spot pointless f-strings? See flake8-pie’s PIE782 code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-sfs-0.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

flake8_sfs-0.0.1-py2.py3-none-any.whl (5.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8-sfs-0.0.1.tar.gz.

File metadata

  • Download URL: flake8-sfs-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191201 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3

File hashes

Hashes for flake8-sfs-0.0.1.tar.gz
Algorithm Hash digest
SHA256 444b839ef76eaaa17c56a362b13f587959836032b57d1f19bacc716d84b78e01
MD5 a95dd91633cd5f340aaf0d8946f6f025
BLAKE2b-256 ca2db1860b76c0b0d7d83e98688fe5d1b57aaf7907828c6945256c32f499aca4

See more details on using hashes here.

File details

Details for the file flake8_sfs-0.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: flake8_sfs-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for flake8_sfs-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 25ecde04de8f8f49caaf422488683cd8c1255103cdfeb6d504e63a0ff107e286
MD5 769e94558319568c3563ace8debb2944
BLAKE2b-256 e1bd5898db9e6e85ae378771f08fd335394fd9eb726a7f3d23954547f48522f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page