Skip to content

unsorted-dunder-all (RUF022)

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for __all__ definitions that are not ordered according to an "isort-style" sort.

An isort-style sort orders items first according to their casing: SCREAMING_SNAKE_CASE names (conventionally used for global constants) come first, followed by CamelCase names (conventionally used for classes), followed by anything else. Within each category, a natural sort is used to order the elements.

Why is this bad?

Consistency is good. Use a common convention for __all__ to make your code more readable and idiomatic.

Example

import sys

__all__ = [
    "b",
    "c",
    "a",
]

if sys.platform == "win32":
    __all__ += ["z", "y"]

Use instead:

import sys

__all__ = [
    "a",
    "b",
    "c",
]

if sys.platform == "win32":
    __all__ += ["y", "z"]

Fix safety

This rule's fix is marked as always being safe, in that it should very rarely alter the semantics of any Python code. However, note that (although it's rare) the value of __all__ could be read by code elsewhere that depends on the exact iteration order of the items in __all__, in which case this rule's fix could theoretically cause breakage.

Note also that for multiline __all__ definitions that include comments on their own line, it can be hard to tell where the comments should be moved to when sorting the contents of __all__. While this rule's fix will never delete a comment, it might sometimes move a comment to an unexpected location.