Skip to content

Files

Latest commit

 

History

History
34 lines (22 loc) · 751 Bytes

shallow-copy-environ.md

File metadata and controls

34 lines (22 loc) · 751 Bytes

Pattern: Use of copy.copy(os.environ)

Issue: -

Description

os.environ is not a dictionary but a proxy object, so shallow copy has still effects on original object. Use os.environ.copy() instead.

Example of incorrect code:

import copy
import os

wrong_env_copy = copy.copy(os.environ)  # will emit pylint warning
wrong_env_copy['ENV_VAR'] = 'new_value'  # changes os.environ
assert os.environ['ENV_VAR'] == 'new_value'

Example of correct code:

import copy
import os

good_env_copy = dict(os.environ)
good_env_copy['ENV_VAR'] = 'different_value'  # doesn't change os.environ
assert os.environ['ENV_VAR'] == 'new_value'

Further Reading