Programming Search⌘K

Python lints, page 5 of 8

The lint rules of Python, with what each one finds. This page has 1,420 cards.

Checks for pytest.warns calls without a match parameter. PT030

pytest.warns(Warning) will catch any Warning and may catch warnings that are unrelated to the code under test. To avoid this, pytest.warns should be called with a match parameter. The warning names that require a match parameter can be configured via the lint.flake8-pytest-style.warns-require-match-for and lint.flake8-pytest-style.warns-extend-require-match-for settings. Ruff takes this rule from flake8-pytest-style and made it stable in 0.12.0.

Code
PT030
Message
`pytest.warns({})` is too broad, set the `match` parameter or use a more specific warning

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.abspath. PTH100

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.resolve() can improve readability over the os.path module's counterparts (e.g., os.path.abspath()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH100
Message
`os.path.abspath()` should be replaced by `Path.resolve()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.chmod. PTH101

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.chmod() can improve readability over the os module's counterparts (e.g., os.chmod()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH101
Message
`os.chmod()` should be replaced by `Path.chmod()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.mkdir. PTH102

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.mkdir() can improve readability over the os module's counterparts (e.g., os.mkdir()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH102
Message
`os.mkdir()` should be replaced by `Path.mkdir()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.makedirs. PTH103

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.mkdir(parents=True) can improve readability over the os module's counterparts (e.g., os.makedirs(). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH103
Message
`os.makedirs()` should be replaced by `Path.mkdir(parents=True)`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.rename. PTH104

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.rename() can improve readability over the os module's counterparts (e.g., os.rename()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH104
Message
`os.rename()` should be replaced by `Path.rename()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.replace. PTH105

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.replace() can improve readability over the os module's counterparts (e.g., os.replace()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH105
Message
`os.replace()` should be replaced by `Path.replace()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.rmdir. PTH106

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.rmdir() can improve readability over the os module's counterparts (e.g., os.rmdir()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH106
Message
`os.rmdir()` should be replaced by `Path.rmdir()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.remove. PTH107

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.unlink() can improve readability over the os module's counterparts (e.g., os.remove()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH107
Message
`os.remove()` should be replaced by `Path.unlink()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.unlink. PTH108

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.unlink() can improve readability over the os module's counterparts (e.g., os.unlink()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH108
Message
`os.unlink()` should be replaced by `Path.unlink()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.getcwd and os.getcwdb. PTH109

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.cwd() can improve readability over the os module's counterparts (e.g., os.getcwd()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH109
Message
`os.getcwd()` should be replaced by `Path.cwd()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.exists. PTH110

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.exists() can improve readability over the os.path module's counterparts (e.g., os.path.exists()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH110
Message
`os.path.exists()` should be replaced by `Path.exists()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.expanduser. PTH111

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.expanduser() can improve readability over the os.path module's counterparts (e.g., as os.path.expanduser()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH111
Message
`os.path.expanduser()` should be replaced by `Path.expanduser()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.isdir. PTH112

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.is_dir() can improve readability over the os.path module's counterparts (e.g., os.path.isdir()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH112
Message
`os.path.isdir()` should be replaced by `Path.is_dir()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.isfile. PTH113

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.is_file() can improve readability over the os.path module's counterparts (e.g., os.path.isfile()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH113
Message
`os.path.isfile()` should be replaced by `Path.is_file()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.islink. PTH114

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.is_symlink() can improve readability over the os.path module's counterparts (e.g., os.path.islink()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH114
Message
`os.path.islink()` should be replaced by `Path.is_symlink()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.readlink. PTH115

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.readlink() can improve readability over the os module's counterparts (e.g., os.readlink()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH115
Message
`os.readlink()` should be replaced by `Path.readlink()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.stat. PTH116

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using Path object methods such as Path.stat() can improve readability over the os module's counterparts (e.g., os.path.stat()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH116
Message
`os.stat()` should be replaced by `Path.stat()`, `Path.owner()`, or `Path.group()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.isabs. PTH117

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.is_absolute() can improve readability over the os.path module's counterparts (e.g., as os.path.isabs()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH117
Message
`os.path.isabs()` should be replaced by `Path.is_absolute()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.join. PTH118

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.joinpath() or the / operator can improve readability over the os.path module's counterparts (e.g., os.path.join()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH118
Message
`os.{}.join()` should be replaced by `Path` with `/` operator
Message
`os.{}.join()` should be replaced by `Path.joinpath()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.basename. PTH119

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.name can improve readability over the os.path module's counterparts (e.g., os.path.basename()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH119
Message
`os.path.basename()` should be replaced by `Path.name`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.dirname. PTH120

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.parent can improve readability over the os.path module's counterparts (e.g., os.path.dirname()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH120
Message
`os.path.dirname()` should be replaced by `Path.parent`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.samefile. PTH121

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.samefile() can improve readability over the os.path module's counterparts (e.g., os.path.samefile()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH121
Message
`os.path.samefile()` should be replaced by `Path.samefile()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.path.splitext. PTH122

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.path. When possible, using Path object methods such as Path.suffix and Path.stem can improve readability over the os.path module's counterparts (e.g., os.path.splitext()). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH122
Message
`os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of the open() builtin. PTH123

pathlib offers a high-level API for path manipulation. When possible, using Path object methods such as Path.open() can improve readability over the open builtin. Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH123
Message
`open()` should be replaced by `Path.open()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of the py.path library. PTH124

The py.path library is in maintenance mode. Instead, prefer the standard library's pathlib module, or third-party modules like path (formerly py.path). Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.231.

Code
PTH124
Message
`py.path` is in maintenance mode, use `pathlib` instead

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of .split(os.sep). PTH206

The pathlib module in the standard library should be used for path manipulation. It provides a high-level API with the functionality needed for common operations on Path objects. Ruff takes this rule from flake8-use-pathlib and made it stable in v0.0.281.

Code
PTH206
Message
Replace `.split(os.sep)` with `Path.parts`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.listdir. PTH208

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os. When possible, using pathlib's Path.iterdir() can improve readability over os.listdir(). Ruff takes this rule from flake8-use-pathlib and made it stable in 0.10.0.

Code
PTH208
Message
Use `pathlib.Path.iterdir()` instead.

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for pathlib.Path.with_suffix() calls where the given suffix does not have a leading dot or the given suffix is a single dot "." and the Python version is less than 3.14. PTH210

Path.with_suffix() will raise an error at runtime if the given suffix is not prefixed with a dot or, in versions prior to Python 3.14, if it is a single dot ".". Ruff takes this rule from flake8-use-pathlib and made it stable in 0.10.0.

Code
PTH210
Message
Invalid suffix passed to `.with_suffix()`
Message
Dotless suffix passed to `.with_suffix()`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of os.symlink. PTH211

pathlib offers a high-level API for path manipulation, as compared to the lower-level API offered by os.symlink. Ruff takes this rule from flake8-use-pathlib and made it stable in 0.13.0.

Code
PTH211
Message
`os.symlink` should be replaced by `Path.symlink_to`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for if statements with complex conditionals in stubs. PYI002

Type checkers understand simple conditionals to express variations between different Python versions and platforms. However, complex tests may not be understood by a type checker, leading to incorrect inferences when they analyze your code. Ruff takes this rule from flake8-pyi and made it stable in v0.0.276.

Code
PYI002
Message
`if` test must be a simple comparison against `sys.platform` or `sys.version_info`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for problematic sys.version_info-related conditions in stubs. PYI003

Stub files support simple conditionals to test for differences in Python versions using sys.version_info. However, there are a number of common mistakes involving sys.version_info comparisons that should be avoided. For example, comparing against a string can lead to unexpected behavior. Ruff takes this rule from flake8-pyi and made it stable in v0.0.276.

Code
PYI003
Message
Unrecognized `sys.version_info` check

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for Python version comparisons in stubs that compare against patch versions (e.g., Python 3.8.3) instead of major and minor versions (e.g., Python 3.8). PYI004

Stub files support simple conditionals to test for differences in Python versions and platforms. However, type checkers only understand a limited subset of these conditionals. In particular, type checkers don't support patch versions (e.g., Python 3.8.3), only major and minor versions (e.g., Python 3.8). Therefore, version checks in stubs should only use the major and minor versions. Ruff takes this rule from flake8-pyi and made it stable in v0.0.276.

Code
PYI004
Message
Version comparison must use only major and minor version

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for Python version comparisons that compare against a tuple of the wrong length. PYI005

Stub files support simple conditionals to test for differences in Python versions and platforms. When comparing against sys.version_info, avoid comparing against tuples of the wrong length, which can lead to unexpected behavior. Ruff takes this rule from flake8-pyi and made it stable in v0.0.276.

Code
PYI005
Message
Version comparison must be against a length-{} tuple

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Check for unrecognized sys.platform checks. PYI007

Platform checks should be simple string comparisons. Some sys.platform checks are too complex for type checkers to understand, and thus result in incorrect inferences by these tools. sys.platform checks should be simple string comparisons, like if sys.platform == "linux". Ruff takes this rule from flake8-pyi and made it stable in v0.0.246.

Code
PYI007
Message
Unrecognized `sys.platform` check

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for typed function arguments in stubs with complex default values. PYI011

Stub (.pyi) files exist as "data files" for static analysis tools, and are not evaluated at runtime. While simple default values may be useful for some tools that consume stubs, such as IDEs, they are ignored by type checkers. Ruff takes this rule from flake8-pyi and made it stable in v0.0.253.

Code
PYI011
Message
Only simple default values allowed for typed arguments

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for untyped function arguments in stubs with default values that are not "simple" /// (i.e., int, float, complex, bytes, str, bool, None, ..., or simple container literals). PYI014

Stub (.pyi) files exist to define type hints, and are not evaluated at runtime. As such, function arguments in stub files should not have default values, as they are ignored by type checkers. Ruff takes this rule from flake8-pyi and made it stable in v0.0.253.

Code
PYI014
Message
Only simple default values allowed for arguments

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for assignments in stubs with default values that are not "simple" (i.e., int, float, complex, bytes, str, bool, None, ..., or simple container literals). PYI015

Stub (.pyi) files exist to define type hints, and are not evaluated at runtime. As such, assignments in stub files should not include values, as they are ignored by type checkers. Ruff takes this rule from flake8-pyi and made it stable in v0.0.260.

Code
PYI015
Message
Only simple default values allowed for assignments

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for assignments with multiple or non-name targets in stub files. PYI017

In general, stub files should be thought of as "data files" for a type checker, and are not intended to be executed. As such, it's useful to enforce that only a subset of Python syntax is allowed in a stub file, to ensure that everything in the stub is unambiguous for the type checker. Ruff takes this rule from flake8-pyi and made it stable in v0.0.279.

Code
PYI017
Message
Stubs should not contain assignments to attributes or multiple targets

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the presence of unused private TypeVar, ParamSpec or TypeVarTuple declarations. PYI018

A private TypeVar that is defined but not used is likely a mistake. It should either be used, made public, or removed to avoid confusion. A type variable is considered "private" if its name starts with an underscore. Ruff takes this rule from flake8-pyi and made it stable in v0.0.281.

Code
PYI018
Message
Private {} `{}` is never used

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for methods that use custom TypeVars in their annotations when they could use Self instead. PYI019

While the semantics are often identical, using Self is more intuitive and succinct (per PEP 673) than a custom TypeVar. For example, the use of Self will typically allow for the omission of type parameters on the self and cls arguments. Ruff takes this rule from flake8-pyi and made it stable in v0.0.283.

Code
PYI019
Message
Use `Self` instead of custom TypeVar `{}`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for quoted type annotations in stub (.pyi) files, which should be avoided. PYI020

Stub files natively support forward references in all contexts, as stubs are never executed at runtime. (They should be thought of as "data files" for type checkers and IDEs.) As such, quotes are never required for type annotations in stub files, and should be omitted. Ruff takes this rule from flake8-pyi and made it stable in v0.0.265.

Code
PYI020
Message
Quoted annotations should not be included in stubs

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for from collections.abc import Set imports that do not alias Set to AbstractSet. PYI025

The Set type in collections.abc is an abstract base class for set-like types. It is easily confused with, and not equivalent to, the set builtin. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI025
Message
Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for __eq__ and __ne__ implementations that use typing.Any as the type annotation for their second parameter. PYI032

The Python documentation recommends the use of object to "indicate that a value could be any type in a typesafe manner". Any, on the other hand, should be seen as an "escape hatch when you need to mix dynamically and statically typed code". Since using Any allows you to write highly unsafe code, you should generally only use Any when the semantics of your code would otherwise be inexpressible to the type checker. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI032
Message
Prefer `object` to `Any` for the second parameter to `{}`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the use of type comments (e.g., x = 1 # type: int). PYI033

Type comments are a soft-deprecated form of type annotation. They are unsupported by some modern type checkers, including ty and Pyrefly, and are only necessary when supporting end-of-life Python versions such as 3.5 or older. They are never necessary in stub files, which are not executed at runtime. Ruff takes this rule from flake8-pyi and made it stable in v0.0.254.

Code
PYI033
Message
Don't use type comments

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for methods that are annotated with a fixed return type which should instead be returning Self. PYI034

If methods that generally return self at runtime are annotated with a fixed return type, and the class is subclassed, type checkers will not be able to infer the correct return type. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI034
Message
`__new__` methods usually return `self` at runtime
Message
`{}` methods in classes like `{}` usually return `self` at runtime

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks that __all__, __match_args__, and __slots__ variables are assigned to values when defined in stub files. PYI035

Special variables like __all__ have the same semantics in stub files as they do in Python modules, and so should be consistent with their runtime counterparts. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI035
Message
`{}` in a stub file must have a value, as it has the same semantics as `{}` at runtime

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for incorrect function signatures on __exit__ and __aexit__ methods. PYI036

Improperly annotated __exit__ and __aexit__ methods can cause unexpected behavior when interacting with type checkers. Ruff takes this rule from flake8-pyi and made it stable in v0.0.279.

Code
PYI036
Message
Star-args in `{}` should be annotated with `object`
Message
If there are no star-args, `{}` should have at least 3 non-keyword-only args (excluding `self`)
Message
All arguments after the first four in `{}` must have a default value
Message
All keyword-only arguments in `{}` must have a default value
Message
The first argument in `{}` should be annotated with `object` or `type[BaseException] | None`
Message
The second argument in `{}` should be annotated with `object` or `BaseException | None`
Message
The third argument in `{}` should be annotated with `object` or `types.TracebackType | None`
Message
Annotations for a three-argument `{}` overload (excluding `self`) should either be `None, None, None` or `type[BaseException], BaseException, types.TracebackType`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the presence of the from __future__ import annotations import statement in stub files. PYI044

Stub files natively support forward references in all contexts, as stubs are never executed at runtime. (They should be thought of as "data files" for type checkers.) As such, the from __future__ import annotations import statement has no effect and should be omitted. Ruff takes this rule from flake8-pyi and made it stable in v0.0.273.

Code
PYI044
Message
`from __future__ import annotations` has no effect in stub files, since type checkers automatically treat stubs as having those semantics

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for functions in stub (.pyi) files that contain multiple statements. PYI048

Stub files are never executed, and are only intended to define type hints. As such, functions in stub files should not contain functional code, and should instead contain only a single statement (e.g., ...). Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI048
Message
Function body must contain exactly one statement

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of typing.NoReturn (and typing_extensions.NoReturn) for parameter annotations. PYI050

Prefer Never over NoReturn for parameter annotations. Never has a clearer name in these contexts, since it makes little sense to talk about a parameter annotation "not returning". Ruff takes this rule from flake8-pyi and made it stable in v0.0.272.

Code
PYI050
Message
Prefer `{}.Never` over `NoReturn` for argument annotations

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for redundant unions between a Literal and a builtin supertype of that Literal. PYI051

Using a Literal type in a union with its builtin supertype is redundant, as the supertype will be strictly more general than the Literal type. For example, Literal"A" | str is equivalent to str, and Literal1 | int is equivalent to int, as str and int are the supertypes of "A" and 1 respectively. Ruff takes this rule from flake8-pyi and made it stable in v0.0.283.

Code
PYI051
Message
`Literal[{}]` is redundant in a union with `{}`
Message
`Literal` is redundant in a union with `{}`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the use of string and bytes literals longer than 50 characters in stub (.pyi) files. PYI053

If a function or variable has a default value where the string or bytes representation is greater than 50 characters long, it is likely to be an implementation detail or a constant that varies depending on the system you're running on. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI053
Message
String and bytes literals longer than 50 characters are not permitted

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for numeric literals with a string representation longer than ten characters. PYI054

If a function has a default value where the literal representation is greater than 10 characters, the value is likely to be an implementation detail or a constant that varies depending on the system you're running on. Ruff takes this rule from flake8-pyi and made it stable in v0.0.271.

Code
PYI054
Message
Numeric literals with a string representation longer than ten characters are not permitted

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks that append, extend and remove methods are not called on __all__. PYI056

Different type checkers have varying levels of support for calling these methods on __all__. Instead, use the += operator to add items to __all__, which is known to be supported by all major type checkers. Ruff takes this rule from flake8-pyi and made it stable in v0.0.281.

Code
PYI056
Message
Calling `.{}()` on `__all__` may not be supported by all type checkers (use `+=` instead)

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for uses of typing.ByteString or collections.abc.ByteString. PYI057

ByteString has been deprecated since Python 3.9 and will be removed in Python 3.17. The Python documentation recommends using either collections.abc.Buffer (or the typing_extensions backport on Python <3.12) or a union like bytes | bytearray | memoryview instead. Ruff takes this rule from flake8-pyi and made it stable in 0.6.0.

Code
PYI057
Message
Do not use `{}.ByteString`, which has unclear semantics and is deprecated

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for simple __iter__ methods that return Generator, and for simple __aiter__ methods that return AsyncGenerator. PYI058

Using (Async)Iterator for these methods is simpler and more elegant. More importantly, it also reflects the fact that the precise kind of iterator returned from an __iter__ method is usually an implementation detail that could change at any time. Type annotations help define a contract for a function; implementation details should not leak into that contract. Ruff takes this rule from flake8-pyi and made it stable in v0.2.0.

Code
PYI058
Message
Use `{}` as the return value for simple `{}` methods

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for redundant LiteralNone annotations. PYI061

While LiteralNone is a valid type annotation, it is semantically equivalent to None. Prefer None over LiteralNone for both consistency and readability. Ruff takes this rule from flake8-pyi and made it stable in 0.13.0.

Code
PYI061
Message
Use `None` rather than `Literal[None]`
Message
Use `Optional[Literal[...]]` rather than `Literal[None, ...]`
Message
Use `Literal[...] | None` rather than `Literal[None, ...]`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the presence of PEP 484-style positional-only parameters. PYI063

Historically, PEP 484 recommended prefixing parameter names with double underscores (__) to indicate to a type checker that they were positional-only. However, PEP 570 (introduced in Python 3.8) introduced dedicated syntax for positional-only arguments. If a forward slash (/) is present in a function signature on Python 3.8+, all parameters prior to the slash are interpreted as positional-only. Ruff takes this rule from flake8-pyi and made it stable in 0.8.0.

Code
PYI063
Message
Use PEP 570 syntax for positional-only parameters

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for redundant FinalLiteral[...] annotations. PYI064

All constant variables annotated as Final are understood as implicitly having Literal types by a type checker. As such, a FinalLiteral[...] annotation can often be replaced with a bare Final, annotation, which will have the same meaning to the type checker while being more concise and more readable. Ruff takes this rule from flake8-pyi and made it stable in 0.8.0.

Code
PYI064
Message
`Final[Literal[{}]]` can be replaced with a bare `Final`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for code that branches on sys.version_info comparisons where branches corresponding to older Python versions come before branches corresponding to newer Python versions. PYI066

As a convention, branches that correspond to newer Python versions should come first. This makes it easier to understand the desired behavior, which typically corresponds to the latest Python versions. Ruff takes this rule from flake8-pyi and made it stable in 0.8.0.

Code
PYI066
Message
Put branches for newer Python versions first when branching on `sys.version_info` comparisons

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for inline strings that use single quotes or double quotes, depending on the value of the lint.flake8-quotes.inline-quotes option. Q000

Consistency is good. Use either single or double quotes for inline strings, but be consistent. Ruff takes this rule from flake8-quotes and made it stable in v0.0.88.

Code
Q000
Message
Single quotes found but double quotes preferred
Message
Double quotes found but single quotes preferred

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for multiline strings that use single quotes or double quotes, depending on the value of the lint.flake8-quotes.multiline-quotes setting. Q001

Consistency is good. Use either single or double quotes for multiline strings, but be consistent. Ruff takes this rule from flake8-quotes and made it stable in v0.0.88.

Code
Q001
Message
Single quote multiline found but double quotes preferred
Message
Double quote multiline found but single quotes preferred

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for docstrings that use single quotes or double quotes, depending on the value of the lint.flake8-quotes.docstring-quotes setting. Q002

Consistency is good. Use either single or double quotes for docstring strings, but be consistent. Ruff takes this rule from flake8-quotes and made it stable in v0.0.88.

Code
Q002
Message
Single quote docstring found but double quotes preferred
Message
Double quote docstring found but single quotes preferred

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Comparison between constants: "… … …" has a constant value. R0133

When two literals are compared with each other the result is a constant. Using the constant directly is both easier to read and more performant. Initializing 'True' and 'False' this way is not required since Python 2.3. pylint calls it comparison-of-constants.

Code
R0133
Severity
refactor
Message
Comparison between constants: "{} {} {}" has a constant value

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Redefining argument with the local name …. R1704

Used when a local name is redefining an argument, which might suggest a potential error. This is taken in account only for a handful of name binding operations, such as for iteration, with statement assignment and exception handler assignment. pylint calls it redefined-argument-from-local.

Code
R1704
Severity
refactor
Message
Redefining argument with the local name {}

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Disallow trailing comma tuple. R1707

In Python, a tuple is actually created by the comma symbol, not by the parentheses. Unfortunately, one can actually create a tuple by misplacing a trailing comma, which can lead to potential weird bugs in your code. You should always use parentheses explicitly for creating a tuple. pylint calls it trailing-comma-tuple.

Code
R1707
Severity
refactor
Message
Disallow trailing comma tuple

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Do not raise StopIteration in generator, use return statement instead. R1708

According to PEP479, the raise of StopIteration to end the loop of a generator may lead to hard to find bugs. This PEP specify that raise StopIteration has to be replaced by a simple return statement. pylint calls it stop-iteration-return.

Code
R1708
Severity
refactor
Message
Do not raise StopIteration in generator, use return statement instead

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Either all return statements in a function should return an expression, or none of them should. R1710

According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable). pylint calls it inconsistent-return-statements.

Code
R1710
Severity
refactor
Message
Either all return statements in a function should return an expression, or none of them should.

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Consider merging these comparisons with 'in' by using '… …in (…)'. Use a set instead if elements are hashable. R1714

To check if a variable is equal to one of many values, combine the values into a set or tuple and check if the variable is contained "in" it instead of checking for equality against each of the values. This is faster and less verbose. pylint calls it consider-using-in.

Code
R1714
Severity
refactor
Message
Consider merging these comparisons with 'in' by using '{} {}in ({})'. Use a set instead if elements are hashable.

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Consider using dict.get for getting values from a dict if a key is present or a default if not. R1715

Using the builtin dict.get for getting a value from a dictionary if a key is present or a default if not, is simpler and considered more idiomatic, although sometimes a bit slower. pylint calls it consider-using-get.

Code
R1715
Severity
refactor
Message
Consider using dict.get for getting values from a dict if a key is present or a default if not

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Consider using a dictionary comprehension. R1717

Emitted when we detect the creation of a dictionary using the dict() callable and a transient list. Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a dict comprehension. Also it is faster since you don't need to create another transient list. pylint calls it consider-using-dict-comprehension.

Code
R1717
Severity
refactor
Message
Consider using a dictionary comprehension

Source: github.com, pylint.readthedocs.io. JSON record. This entry is a card. Help write this page.

Check that methods do not have redundant Self annotations. redundant-self

If a method uses the Self type in the return type or the type of a non-self argument, there is no need to annotate the self argument explicitly. Such annotations are allowed by 673 but are redundant. mypy does not enable this error code by default. Use --enable-error-code redundant-self to enable it.

Code
redundant-self
Severity
error

Source: github.com, mypy.readthedocs.io. JSON record. This entry is a card. Help write this page.

Checks for the presence of a return None statement when None is the only possible return value. RET501

Python implicitly assumes return None if an explicit return value is omitted. Therefore, explicitly returning None is redundant and should be avoided when it is the only possible return value across all code paths in a given function. Ruff takes this rule from flake8-return and made it stable in v0.0.154.

Code
RET501
Message
Do not explicitly `return None` in function if it is the only possible return value

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for the presence of a return statement with no explicit value, for functions that return non-None values elsewhere. RET502

Including a return statement with no explicit value can cause confusion when other return statements in the function return non-None values. Python implicitly assumes return None if no other return value is present. Adding an explicit return None can make the code more readable by clarifying intent. Ruff takes this rule from flake8-return and made it stable in v0.0.154.

Code
RET502
Message
Do not implicitly `return None` in function able to return non-`None` value

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for missing explicit return statements at the end of functions that can return non-None values. RET503

The lack of an explicit return statement at the end of a function that can return non-None values can cause confusion. Python implicitly returns None if no other return value is present. Adding an explicit return None can make the code more readable by clarifying intent. Ruff takes this rule from flake8-return and made it stable in v0.0.154.

Code
RET503
Message
Missing explicit `return` at the end of function able to return non-`None` value

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for ambiguous Unicode characters in strings. RUF001

Some Unicode characters are visually similar to ASCII characters, but have different code points. For example, GREEK CAPITAL LETTER ALPHA (U+0391) is visually similar, but not identical, to the ASCII character A. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.102.

Code
RUF001
Message
String contains ambiguous {}. Did you mean {}?

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for ambiguous Unicode characters in docstrings. RUF002

Some Unicode characters are visually similar to ASCII characters, but have different code points. For example, GREEK CAPITAL LETTER ALPHA (U+0391) is visually similar, but not identical, to the ASCII character A. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.102.

Code
RUF002
Message
Docstring contains ambiguous {}. Did you mean {}?

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for ambiguous Unicode characters in comments. RUF003

Some Unicode characters are visually similar to ASCII characters, but have different code points. For example, GREEK CAPITAL LETTER ALPHA (U+0391) is visually similar, but not identical, to the ASCII character A. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.108.

Code
RUF003
Message
Comment contains ambiguous {}. Did you mean {}?

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for asyncio.create_task and asyncio.ensure_future calls that do not store a reference to the returned result. RUF006

Per the asyncio documentation, the event loop only retains a weak reference to tasks. If the task returned by asyncio.create_task and asyncio.ensure_future is not stored in a variable, or a collection, or otherwise referenced, it may be garbage collected at any time. This can lead to unexpected and inconsistent behavior, as your tasks may or may not run to completion. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.247.

Code
RUF006
Message
Store a reference to the return value of `{}.{}`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for mutable default values in dataclass attributes. RUF008

Mutable default values share state across all instances of the dataclass. This can lead to bugs when the attributes are changed in one instance, as those changes will unexpectedly affect all other instances. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.262.

Code
RUF008
Message
Do not use mutable default values for dataclass attributes

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for function calls in dataclass attribute defaults. RUF009

Function calls are only performed once, at definition time. The returned value is then reused by all instances of the dataclass. This can lead to unexpected behavior when the function call returns a mutable object, as changes to the object will be shared across all instances. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.262.

Code
RUF009
Message
Do not perform function call `{}` in dataclass defaults
Message
Do not perform function call in dataclass defaults

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for dictionary comprehensions that use a static key, like a string literal or a variable defined outside the comprehension. RUF011

Using a static key (like a string literal) in a dictionary comprehension is usually a mistake, as it will result in a dictionary with only one key, despite the comprehension iterating over multiple values. Ruff takes this rule from Ruff-specific rules, and removed it.

Code
RUF011
Message
Dictionary comprehension uses static key

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for mutable default values in class attributes. RUF012

Mutable default values share state across all instances of the class, while not being obvious. This can lead to bugs when the attributes are changed in one instance, as those changes will unexpectedly affect all other instances. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.273.

Code
RUF012
Message
Mutable default value for class attribute

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks the following constructs, all of which can be replaced by next(iter(...)):. RUF015

Calling e.g. list(...) will create a new list of the entire collection, which can be very expensive for large collections. If you only need the first element of the collection, you can use next(...) or next(iter(...) to lazily fetch the first element. The same is true for the other constructs. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.278.

Code
RUF015
Message
Prefer `next({})` over single element slice

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for indexed access to lists, strings, tuples, bytes, and comprehensions using a type other than an integer or slice. RUF016

Only integers or slices can be used as indices to these types. Using other types will result in a TypeError at runtime and a SyntaxWarning at import time. Ruff takes this rule from Ruff-specific rules and made it stable in v0.0.278.

Code
RUF016
Message
Slice in indexed access to type `{}` uses type `{}` instead of an integer
Message
Indexed access to type `{}` uses type `{}` instead of an integer or slice

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for chained operators where adding parentheses could improve the clarity of the code. RUF021

and always binds more tightly than or when chaining the two together, but this can be hard to remember (and sometimes surprising). Adding parentheses in these situations can greatly improve code readability, with no change to semantics or performance. Ruff takes this rule from Ruff-specific rules and made it stable in 0.8.0.

Code
RUF021
Message
Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for mutable objects passed as a value argument to dict.fromkeys. RUF024

All values in the dictionary created by the dict.fromkeys method refer to the same instance of the provided object. If that object is modified, all values are modified, which can lead to unexpected behavior. For example, if the empty list ([]) is provided as the default value, all values in the dictionary will use the same list; as such, appending to any one entry will append to all entries. Ruff takes this rule from Ruff-specific rules and made it stable in 0.5.0.

Code
RUF024
Message
Do not pass mutable objects as values to `dict.fromkeys`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for incorrect usages of default_factory as a keyword argument when initializing a defaultdict. RUF026

The defaultdict constructor accepts a callable as its first argument. For example, it's common to initialize a defaultdict with int or list via defaultdict(int) or defaultdict(list), to create a dictionary that returns 0 or [] respectively when a key is missing. Ruff takes this rule from Ruff-specific rules and made it stable in 0.5.0.

Code
RUF026
Message
`default_factory` is a positional-only argument to `defaultdict`

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.

Checks for functions declared async that do not await or otherwise use features requiring the function to be declared async. RUF029

Declaring a function async when it's not is usually a mistake, and will artificially limit the contexts where that function may be called. In some cases, labeling a function async is semantically meaningful. For example, an async test or callback may need to run in an async execution context, even if it only uses a ContextVar. Ruff takes this rule from Ruff-specific rules. It is in preview.

Code
RUF029
Message
Function `{}` is declared `async`, but doesn't `await` or use `async` features.

Source: docs.astral.sh. JSON record. This entry is a card. Help write this page.