Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule EC66 does not apply to the Python language #198

Closed
AghilesAzzoug opened this issue Jun 2, 2023 · 3 comments
Closed

Rule EC66 does not apply to the Python language #198

AghilesAzzoug opened this issue Jun 2, 2023 · 3 comments
Assignees
Labels
🏗️ refactoring refactoring for best practices 🗃️ rule rule improvment or rule development or bug python 💉 bug Something isn't working

Comments

@AghilesAzzoug
Copy link
Contributor

Rule EC66 does not apply to the Python language.

Explanation

Variable substitution is not possible in pure string literals as opposed to formatted string literals (aka. f-strings), unlike in PHP.
Therefore, we can use single and double quotes string literals interchangeably without worrying for any useless CPU cycles.

Disassembling the two examples given in the HTML description of rule EC66 give exactly the same bytecode.

Compliant bytecode

 15           0 LOAD_CONST               1 ('Andrea')
              2 STORE_FAST               0 (firstname)

 18           4 LOAD_CONST               2 (<code object my_function at 0x000001D7D40D1D40
              6 LOAD_CONST               3 ('compliant.<locals>.my_function')
              8 MAKE_FUNCTION            0
             10 STORE_FAST               1 (my_function)

 21          12 LOAD_FAST                1 (my_function)
             14 LOAD_CONST               4 ('Robert')
             16 LOAD_CONST               5 (12)
             18 CALL_FUNCTION            2
             20 POP_TOP
             22 LOAD_CONST               0 (None)
             24 RETURN_VALUE

Disassembly of <code object my_function at 0x000001D7D40D1D40
 19           0 LOAD_GLOBAL              0 (print)
              2 LOAD_FAST                0 (name)
              4 LOAD_CONST               1 ('is')
              6 BINARY_ADD
              8 LOAD_FAST                1 (age)
             10 BINARY_ADD
             12 LOAD_CONST               2 (' yo.')
             14 BINARY_ADD
             16 CALL_FUNCTION            1
             18 POP_TOP
             20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

Non compliant bytecode

  5           0 LOAD_CONST               1 ('Andrea')
              2 STORE_FAST               0 (firstname)

  8           4 LOAD_CONST               2 (<code object my_function at 0x000001D7D40D1BE0
              6 LOAD_CONST               3 ('non_compliant.<locals>.my_function')
              8 MAKE_FUNCTION            0
             10 STORE_FAST               1 (my_function)

 11          12 LOAD_FAST                1 (my_function)
             14 LOAD_CONST               4 ('Robert')
             16 LOAD_CONST               5 (12)
             18 CALL_FUNCTION            2
             20 POP_TOP
             22 LOAD_CONST               0 (None)
             24 RETURN_VALUE

Disassembly of <code object my_function at 0x000001D7D40D1BE0
  9           0 LOAD_GLOBAL              0 (print)
              2 LOAD_FAST                0 (name)
              4 LOAD_CONST               1 ('is')
              6 BINARY_ADD
              8 LOAD_FAST                1 (age)
             10 BINARY_ADD
             12 LOAD_CONST               2 (' yo.')
             14 BINARY_ADD
             16 CALL_FUNCTION            1
             18 POP_TOP
             20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

It also seems that Python parser treats single and double quotes string literals the same way, see here (they only check if the last quote char matches the first one).

@dedece35 dedece35 added 🗃️ rule rule improvment or rule development or bug 🏗️ refactoring refactoring for best practices python labels Jun 2, 2023
@dedece35 dedece35 added the 💉 bug Something isn't working label Jun 2, 2023
@AghilesAzzoug
Copy link
Contributor Author

After some research and experimentation, it appears that using an f-string without variable placeholders is a bit slower than a literal, as it generates more instructions for no reason. It also seems that this issue is already natively handled by SonarQube, see here.

In any case, what is certain is that single quotes and double quotes are strictly equivalent in Python.

@dedece35
Copy link
Member

dedece35 commented Jun 6, 2023

Hi @AghilesAzzoug
thank you for investigation ... I understand your analyse.
We will discuss with core-team during weekly meeting and give you feedback.
(plop @glalloue @mdubois81)

@dedece35
Copy link
Member

issue closed because of migration to new python repository : green-code-initiative/ecoCode-python#4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏗️ refactoring refactoring for best practices 🗃️ rule rule improvment or rule development or bug python 💉 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants