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 unnecessary-comprehension (C416) #4453

Merged
merged 4 commits into from
Oct 5, 2024

Conversation

echoix
Copy link
Member

@echoix echoix commented Oct 5, 2024

@echoix echoix added this to the 8.5.0 milestone Oct 5, 2024
@echoix echoix requested a review from ninsbl October 5, 2024 17:54
@github-actions github-actions bot added GUI wxGUI related Python Related code is in Python libraries module imagery tests Related to Test Suite labels Oct 5, 2024
ninsbl
ninsbl previously approved these changes Oct 5, 2024
Copy link
Member

@ninsbl ninsbl left a comment

Choose a reason for hiding this comment

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

I have not build and tested, but the changes are according to the ruff rule. In some cases I am not sure if the list() is even necessary...

@echoix
Copy link
Member Author

echoix commented Oct 5, 2024

I have not build and tested, but the changes are according to the ruff rule. In some cases I am not sure if the list() is even necessary...

I also researched a bit on the behavior as I thought it helped with copies and the likes. It seems it doesn't help, and is no more different as a shallow copy either. So the list constructor provides the same as the comprehension, only that the list comprehensions are not the fastest.
See these SO questions/answers for example:

It was harder to find real definitive answers that weren't related to Python 2 era.

So, in order to at least have the same behavior, in case there were some edge cases when there wasn't a list (ie, we wouldn't want to have a string not as a list element, as iterating it would go character-per-character instead), using the list constructor seems the best approach.

@echoix
Copy link
Member Author

echoix commented Oct 5, 2024

There was a recursion problem here since __len__ used len(self.mapsets()) and mapsets() was changed too. So I'm adding a # noqa exclusion here. We'll see if it's enough. It isn't obvious why the list constructor used the len() instead of using up the iterable.

@echoix echoix enabled auto-merge (squash) October 5, 2024 20:50
@echoix echoix merged commit cd9e0a4 into OSGeo:main Oct 5, 2024
26 checks passed
@echoix echoix deleted the fix-C416-unnecessary-comprehension branch October 5, 2024 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GUI wxGUI related imagery libraries module Python Related code is in Python tests Related to Test Suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants