This rule raises an issue when a file permission mode is passed as a non-octal integer literal to APIs such as os.chmod.

Why is this an issue?

Using non-octal values to set Unix file permissions is problematic because they are converted to octal internally, which can lead to unexpected file permissions and potential security issues.

Naming the value does not change this. A constant or an immutable local binding initialized with a decimal literal is converted the same way, and the missing 0o prefix is easier to overlook when the value is declared away from the call.

What is the potential impact?

How to fix it

Write permission modes as octal literals with the 0o prefix so the value matches the intended Unix mode.

The literal 0 is clear on its own and does not need an octal prefix.

Intentional decimal mode literals are rare. Treat them as won’t-fix only after review confirms the decimal value is deliberate rather than a mistaken shell-style octal.

Code examples

Noncompliant code example

import os

os.chmod("file.txt", 755)  # Noncompliant

Compliant solution

import os

os.chmod("file.txt", 0o755)

Noncompliant code example

from pathlib import Path

Path("config.ini").chmod(644)  # Noncompliant

Compliant solution

from pathlib import Path

Path("config.ini").chmod(0o644)

Resources

Documentation

External coding guidelines