This rule raises an issue when os.environ is shallow-copied with copy.copy().

Why is this an issue?

os.environ is not a plain dict. It is a proxy mapping that reads and writes the process environment.

copy.copy(os.environ) creates a shallow copy of that proxy. Mutations on the copy still update the real process environment, which is almost never what developers expect when they ask for a copy.

import copy
import os

env = copy.copy(os.environ)
env["MY_VAR"] = "value"
# os.environ["MY_VAR"] is also "value"

os.environ.copy() returns an independent dict snapshot. Changes to that dictionary do not affect the process environment.

What is the potential impact?

How to fix it

Replace copy.copy(os.environ) with os.environ.copy().

Code examples

Noncompliant code example

import copy
import os

env = copy.copy(os.environ)  # Noncompliant

Compliant solution

import os

env = os.environ.copy()

Resources

Documentation

External coding guidelines