Skip to content

Prefer iterating dict instead of calling dict.keys() #138

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

Merged
merged 1 commit into from
Sep 20, 2019
Merged

Prefer iterating dict instead of calling dict.keys() #138

merged 1 commit into from
Sep 20, 2019

Conversation

jdufresne
Copy link
Member

Calling dict.keys() is unnecessary. iter(dict) is equivalent to dict.keys(). Inspired by Lennart Regebro's talk "Prehistoric Patterns in Python" from PyCon 2017.

https://www.youtube.com/watch?v=V5-JH23Vk0I

@codecov
Copy link

codecov bot commented Dec 18, 2017

Codecov Report

Merging #138 into master will not change coverage.
The diff coverage is 85.71%.

@@           Coverage Diff           @@
##           master     #138   +/-   ##
=======================================
  Coverage   70.93%   70.93%           
=======================================
  Files          49       49           
  Lines        4816     4816           
  Branches      812      812           
=======================================
  Hits         3416     3416           
  Misses       1066     1066           
  Partials      334      334
Impacted Files Coverage Δ
Lib/ldap/modlist.py 84.61% <100%> (ø) ⬆️
Lib/ldap/schema/subentry.py 68.79% <83.33%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 576d5bf...9bfdc6d. Read the comment docs.

tiran
tiran previously requested changes Dec 18, 2017
Copy link
Member

@tiran tiran left a comment

Choose a reason for hiding this comment

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

In general it's a good idea. However dict.keys() to dict iteration can cause dict size change during iteration bugs. I'd prefer to delay this postpone this change for 3.1 release.

First we should bring the cidict module to 100% test coverage (including edge cases). Then we should remove all functions that are provided by UserDict.IterableUserDict or collections.Mapping. Then we can modernize the code.

@jdufresne
Copy link
Member Author

However dict.keys() to dict iteration can cause dict size change during iteration bugs. I'd prefer to delay this postpone this change for 3.1 release.

This would already be true on Python 3. On Python 3, you can't both iterate over dict.keys() and modify the dict as dict.keys() is just a view and not a list copy of the keys.

@encukou
Copy link
Member

encukou commented Mar 15, 2018

@jdufresne, can I rebase this? (It's not a problem for me, but I don't want to cause conflicts if you're working on it.)

@jdufresne
Copy link
Member Author

Sure, go ahead, thanks. I normally just grep for \.keys( and analyze the results.

@jdufresne
Copy link
Member Author

Rebased on the latest master branch. Thanks.

@encukou
Copy link
Member

encukou commented Mar 27, 2018

Sorry for the delay in reviewing.
If you'd like, I can rebase this, address my comments and let you review it. It would make the back-and-forth faster, but I don't want to mess with your pull request without warning.

@jdufresne
Copy link
Member Author

If you'd like, I can rebase this, address my comments and let you review it. It would make the back-and-forth faster, but I don't want to mess with your pull request without warning.

That is fine with me. Please feel free to make additional changes should a review catch anything else.

Calling dict.keys() is unnecessary. iter(dict) is equivalent to
dict.keys(). Inspired by Lennart Regebro's talk "Prehistoric Patterns in
Python" from PyCon 2017.

https://www.youtube.com/watch?v=V5-JH23Vk0I
@encukou encukou dismissed tiran’s stale review May 24, 2019 11:36

cidict is no longer affected

@encukou encukou merged commit f4059c4 into python-ldap:master Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy