rspec/rules/S5806/python/rule.adoc

41 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Shadowing a builtin makes your code more difficult to read and maintain. It may also be a source of bugs as you can reference the builtin by mistake.
It is sometimes ok to shadow a builtin to improve the readability of a public API or to support multiple versions of a library. In these cases the value is higher than the maintainability cost. Just be careful when you do it.
It is not ok to shadow builtins with variables which are local to a function or method. These variables are not public and can be easily renamed, thus reducing the confusion and making the code less error-prone.
This rule raises an issue when the name of a local variable matches the name of a builtin.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
def a_function():
int = 42 # Noncompliant; int is a builtin
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
def a_function():
value = 42
----
2021-04-28 16:49:39 +02:00
== See
* https://docs.python.org/3.8/library/stdtypes.html[Python documentation - Built-in Types]
* https://docs.python.org/3/library/functions.html[Python documentation - Built-in Functions]
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]