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

style: Fix if-else-block-instead-of-if-exp (SIM108) (part 3) #4570

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

echoix
Copy link
Member

@echoix echoix commented Oct 21, 2024

Last PR of this series. It finishes by enabling checking the rule with ruff.

PRs in this series: #4562, #4561

Ruff rule: https://docs.astral.sh/ruff/rules/if-else-block-instead-of-if-exp

@echoix echoix requested a review from ninsbl October 21, 2024 21:19
@github-actions github-actions bot added GUI wxGUI related Python Related code is in Python libraries labels Oct 21, 2024
@echoix
Copy link
Member Author

echoix commented Oct 21, 2024

On my fork I think it's bandit that will complain about the use of eval, that is only moved around

"" if keywords is None or keywords.text is None else keywords.text
)
shortcut = shortcut.text if shortcut is not None else ""
wxId = eval("wx." + wxId.text) if wxId is not None else wx.ID_ANY

Check warning

Code scanning / Bandit

Use of possibly insecure function - consider using safer ast.literal_eval. Warning

Use of possibly insecure function - consider using safer ast.literal_eval.
idx = i - 1
else:
idx = i
idx = i - 1 if missingKey is True else i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idx = i - 1 if missingKey is True else i
idx = i - 1 if missingKey else i

@@ -382,10 +382,7 @@ def OnEraseBackground(self, event):

def OnSize(self, event):
size = self.GetClientSize()
if CheckWxVersion(version=[2, 9]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may as well just remove the check, it's too old version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without docs, I'm not sure what does the check return true for (to choose what branch). If you don't know I'll read later

@@ -294,15 +294,12 @@ def __init__(self, label=None, data=None):
def label(self):
return self._label

def match(self, key, value, case_sensitive=False):
def match(self, key, value, case_sensitive=False) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need from __future__ import annotations here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the future is used when we want to use | instead of Optional[] (that needs to be imported too), plus more other syntax that was supposed to be mandatory in 3.10, but still postponed. (That future exists since 3.7).

https://docs.python.org/3/library/__future__.html#module-contents

It also uses the new behavior of considering the types as strings without evaluating for better performance, instead of evaluating the types. Type checking doesn't isn't enforced at runtime anyways, so post-poneing makes sense.

In this file, bool doesn't need extra workarounds to work correctly.

Type annotations isn't new, it was there in Python 3.5, but supported syntax evolved to be more useful

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained in #4561, in order to have more precise validations on what simplifications could be done in regards to the types. I analyzed these and greatly helped, as it allowed type checking to understand that some variables are strings (coming from the options), and that the values from the flags are bools, not any type. So some of the conditions could assume the bool type and just use "or" instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GUI wxGUI related libraries Python Related code is in Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants