Skip to content

Commit 054f233

Browse files
authored
Extend documentation for redefined-builtin (#10167)
1 parent 0d5439b commit 054f233

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The :ref:`allowed-redefined-builtins <variables-options>` option lets you specify names that are permitted to shadow built-ins.
2+
3+
However, this option is not effective for redefinitions at the module level or for global variables. For example:
4+
5+
Module-Level Redefinitions::
6+
7+
# module_level_redefine.py
8+
id = 1 # Shadows the built-in `id`
9+
10+
Global Variable Redefinitions::
11+
12+
# global_variable_redefine.py
13+
def my_func():
14+
global len
15+
len = 1 # Shadows the built-in `len`
16+
17+
Rationale:
18+
19+
Shadowing built-ins at the global scope is discouraged because it obscures their behavior
20+
throughout the entire module, increasing the risk of subtle bugs when the built-in is needed elsewhere.
21+
In contrast, local redefinitions are acceptable as their impact is confined to a specific scope,
22+
reducing unintended side effects and simplifying debugging.

tests/functional/r/redefined/redefined_builtin_allowed.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ def function():
77
print(dir, dict)
88

99
list = "not in globals" # [redefined-builtin]
10+
11+
def global_variable_redefine():
12+
"""Shadow `len` using the `global` keyword."""
13+
global len
14+
len = 1 # [redefined-builtin]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[messages control]
2-
disable = invalid-name
2+
disable = invalid-name, global-variable-undefined
33
[variables]
4-
allowed-redefined-builtins = dir, list
4+
allowed-redefined-builtins = dir, list, len
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
redefined-builtin:6:4:6:8:function:Redefining built-in 'dict':UNDEFINED
22
redefined-builtin:9:0:9:4::Redefining built-in 'list':UNDEFINED
3+
redefined-builtin:14:4:14:7:global_variable_redefine:Redefining built-in 'len':UNDEFINED

0 commit comments

Comments
 (0)