Registry / testing / flake8-literal

flake8-literal

JSON →
library1.5.0pypypiunverified

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.

pip install flake8-literal
INSTALL
IMPORT
SIG · FLAKE8-LITERAL
F
flake8-literal
testingpythonv1.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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
Debug
Known issues
gotchaflake8-literal is a plugin and does not expose symbols for direct import into Python code. Its functionality is integrated by running the `flake8` command-line tool. All configurations are done through `flake8`'s configuration files (e.g., `setup.cfg`, `.flake8`) or command-line options, not within Python code.
fix
Configure `flake8-literal` options (e.g., `literal-inline-quotes`) in your `setup.cfg` or `.flake8` file under the `[flake8]` section, or pass them as `--literal-inline-quotes=double` arguments to the `flake8` command.
affects: All versions
gotchaThe raw string validation feature for regular expression patterns (e.g., `L104`) has a specific limitation: it only works when string literals are directly passed as arguments to `re` module functions (e.g., `re.compile(r'...')`) and these functions are called directly (not aliased or stored in variables first). Indirect usages, like defining a pattern string in a variable and then passing it to `re.compile(pattern)`, will not be checked.
fix
Ensure `re` functions are called directly with raw string literals for `flake8-literal` to apply its raw string regex checks, or be aware that indirect assignments will bypass this specific check.
affects: All versions
gotchaAs a `flake8` plugin, `flake8-literal`'s effectiveness can depend on the version of `flake8` being used. While `flake8-literal` requires Python >=3.12, ensure that your installed `flake8` version is compatible with this plugin and with your Python environment. Major updates to `flake8` can sometimes introduce changes in how plugins are discovered or configured.
fix
Always install `flake8` and its plugins within the same Python environment. If encountering unexpected behavior, check the `flake8` and `flake8-literal` documentation for any specific version compatibility notes. Use `pip install --upgrade flake8 flake8-literal` to ensure you have recent, compatible versions.
affects: All versions
Errors
Common errors & fixes
LIT001 Use single quotes for string
An inline string literal is using double quotes, but flake8-literal is configured to enforce single quotes for inline strings.
fix
Change the string literal to use single quotes (e.g., `x = 'hello'`) or configure `flake8-literal` to allow or prefer double quotes by setting `literal-inline-quotes = double` in your flake8 configuration file (e.g., `.flake8`, `setup.cfg`).
LIT101 Remove raw prefix when not using escapes
A string literal is prefixed with `r` (making it a raw string), but it does not contain any backslashes or characters that would necessitate the raw prefix.
fix
Remove the `r` prefix from the string literal (e.g., `x = 'path'` instead of `x = r'path'` if 'path' contains no backslashes).
LIT102 Use raw prefix to avoid escaped slash
A regular string literal contains escaped backslashes (e.g., `\`), which can be simplified and made more readable by using a raw string (`r"..."`).
fix
Add an `r` prefix to the string literal and remove the redundant backslash escapes (e.g., `re.compile(r'\d+')` instead of `re.compile('\\d+')`).
flake8-literal not working
The `flake8-literal` plugin is not installed, not correctly enabled by Flake8, or its specific error codes are being ignored in your Flake8 configuration.
fix
First, ensure `flake8-literal` is installed via `pip install flake8-literal`. Then, check your Flake8 configuration file (e.g., `.flake8`, `setup.cfg`, `pyproject.toml`) to ensure `LIT` error codes are not ignored (e.g., remove `LIT` from `ignore = ...`) or explicitly select them (e.g., `select = E,F,W,LIT`).
Upgrade
Version history
1.5.0latest on PyPI · released Jun 28, 2025
Audit
Dependencies
flake8requiredThis is a plugin for Flake8 and requires Flake8 to function.
Agent activity
2 hits · last 30 days
node
2
Resources
flake8-literal — pip install flake8-literal · libregistry