Flake8 Literal

1.5.0 · active · verified Wed Apr 15

flake8-literal is a Flake8 plugin designed to enforce consistent styling of string literals in Python code. It validates inline literals, multiline literals, and docstrings for quote usage (single or double quotes). Additionally, it includes checks for raw string usage, preventing unnecessary raw strings and enforcing their use when needed to avoid escaped backslashes. The current version is 1.5.0, and it is actively maintained.

Warnings

Install

Quickstart

Install flake8-literal and then run `flake8` on your Python code. The plugin's checks will automatically be included. Configuration options can be specified in a `flake8` configuration file (e.g., `setup.cfg`, `pyproject.toml`, or `.flake8`) under the `[flake8]` section, or via command-line arguments.

# Create a file named 'example.py'
# Then run flake8 example.py from your terminal

def check_quotes():
    # L100: Use double quotes for inline literals (if configured)
    inline_string = 'hello world'

    # L101: Use single quotes for multiline literals (if configured)
    multiline_string = """
    This is a multiline string.
    """

    # L102: Use double quotes for docstrings (if configured)
    """This is a docstring with incorrect quotes (if configured)."""

    # L103: Unnecessary raw string (if configured to avoid escapes)
    path_string = r"C:\users\name"

    import re
    # L104: Regex pattern should be a raw string (if configured strictly)
    regex_pattern = re.compile('[a-z]')

# To configure flake8-literal, create a setup.cfg or .flake8 file:
# For example, in setup.cfg:
# [flake8]
# literal-inline-quotes = double
# literal-multiline-quotes = single
# literal-docstring-quotes = double
# literal-avoid-escape = true
# literal-raw-regex-always-raw = true

view raw JSON →