2023-10-17 09:45:51 +02:00
|
|
|
:identifier_capital_plural: Local variables and function parameters
|
|
|
|
:identifier: local variable and function parameter
|
|
|
|
:identifier_plural: local variables and function parameters
|
|
|
|
:identifier_or: local variable or function parameter
|
|
|
|
:regex: ^[_a-z][a-z0-9_]*$
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
include::../introduction.adoc[]
|
|
|
|
|
|
|
|
include::../why-is-this-an-issue.adoc[]
|
|
|
|
|
|
|
|
include::../what-is-the-potential-impact.adoc[]
|
2020-06-30 10:16:44 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2020-06-30 10:16:44 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
Loop counters of one letter are ignored by this rule.
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
[source,python]
|
2020-06-30 10:16:44 +02:00
|
|
|
----
|
|
|
|
for i in range(limit): # Compliant
|
|
|
|
print(i)
|
|
|
|
----
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
Local variables matching regular expression ``++^[_A-Z][A-Z0-9_]*$++`` are considered as constant and ignored by this rule. +
|
|
|
|
Function parameters are not concerned by this exception.
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
def print_something(important_param):
|
|
|
|
LOCAL_VARIABLE = "" # Compliant
|
|
|
|
print(important_param + LOCAL_VARIABLE)
|
|
|
|
----
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
include::../how-to-fix-it.adoc[]
|
|
|
|
|
|
|
|
=== Code examples
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
With the default regular expression ``{regex}``:
|
|
|
|
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
def print_something(IMPORTANT_PARAM): # Noncompliant
|
|
|
|
localVariable = "" # Noncompliant
|
|
|
|
print(IMPORTANT_PARAM + localVariable)
|
|
|
|
----
|
2023-05-25 14:18:12 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
==== Compliant solution
|
2023-05-25 14:18:12 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
2023-05-25 14:18:12 +02:00
|
|
|
----
|
2023-10-17 09:45:51 +02:00
|
|
|
def print_something(important_param):
|
|
|
|
local_variable = ""
|
|
|
|
print(important_param + local_variable)
|
2023-05-25 14:18:12 +02:00
|
|
|
----
|
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
== Resources
|
|
|
|
|
|
|
|
=== Documentation
|
|
|
|
|
|
|
|
* Python Enhancement Proposals - https://peps.python.org/pep-0008/#naming-conventions[PEP8 - Naming Conventions]
|
|
|
|
* Wikipedia - https://en.wikipedia.org/wiki/Naming_convention_(programming)[Naming Convention (programming)]
|
|
|
|
|
|
|
|
=== Related rules
|
|
|
|
|
|
|
|
* S100 - Method names should comply with a naming convention
|
|
|
|
* S101 - Class names should comply with a naming convention
|
|
|
|
* S116 - Field names should comply with a naming convention
|
|
|
|
* S1542 - Function names should comply with a naming convention
|
|
|
|
* S1578 - Module names should comply with a naming convention
|
|
|
|
* S2710 - The first argument to class methods should follow the naming convention
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
2023-05-25 14:18:12 +02:00
|
|
|
|
2023-10-17 09:45:51 +02:00
|
|
|
include::../parameters.adoc[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|