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

Fix a false positive for consider-using-dict-items #9594

Conversation

mbyrnepr2
Copy link
Member

Type of Changes

Type
βœ“ πŸ› Bug fix
✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Fix a false positive for consider-using-dict-items when iterating os.environ using the os.environ.keys() operation and then deleting an item using the key as a lookup.

Closes #9554

… ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes pylint-dev#9554
@mbyrnepr2 mbyrnepr2 added the False Positive 🦟 A message is emitted but nothing is wrong with the code label May 3, 2024
Copy link

codecov bot commented May 3, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.81%. Comparing base (3c8be8e) to head (1f2a0da).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #9594   +/-   ##
=======================================
  Coverage   95.81%   95.81%           
=======================================
  Files         173      173           
  Lines       18825    18827    +2     
=======================================
+ Hits        18038    18040    +2     
  Misses        787      787           
Files Coverage Ξ”
...int/checkers/refactoring/recommendation_checker.py 96.56% <100.00%> (+0.03%) ⬆️

This comment has been minimized.

@mbyrnepr2 mbyrnepr2 marked this pull request as ready for review May 4, 2024 05:04
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.1.1 milestone May 4, 2024
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

LGTM, some nits :)

tests/functional/c/consider/consider_using_dict_items.py Outdated Show resolved Hide resolved
tests/functional/c/consider/consider_using_dict_items.py Outdated Show resolved Hide resolved
@@ -0,0 +1,3 @@
Fix a false positive for ``consider-using-dict-items`` when iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Fix a false positive for ``consider-using-dict-items`` when iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.
Fix a false positive for ``consider-using-dict-items`` when iterating using ``keys()`` and then deleting an item using the key as a lookup.

The false positive is more generic than just os.environ, right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

That is indeed true and I made the distinction here in the public-facing doc (compared to the commit message) because I thought it might sound odd since this situation suggests a modified iterating dict error would occur (which doesn’t happen in the case of os.environ because it is dict-like but also a bit special.
Having said that I’m not against using the word dict here instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I just checked and the commit message is the same πŸ˜…

Copy link
Member

Choose a reason for hiding this comment

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

If it's only a FP for os.environ because it's special then my suggestion is wrong πŸ˜„

Copy link
Member Author

Choose a reason for hiding this comment

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

Well it would manifest for any dict but for a standard dict you would also have the modify iterating dict error; but I could be overthinking that.

Copy link
Member Author

@mbyrnepr2 mbyrnepr2 May 4, 2024

Choose a reason for hiding this comment

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

I just meant that you don’t run into modify iterating dict error for os.environ which is why I say it’s a bit special.

This comment has been minimized.

This comment has been minimized.

val = any(True for k8 in Foo.c_dict.keys() if Foo.c_dict[k8]) # [consider-iterating-dictionary,consider-using-dict-items]
val = any(
True for k8 in Foo.c_dict.keys() if Foo.c_dict[k8]
) # [consider-iterating-dictionary,consider-using-dict-items]
Copy link
Member Author

Choose a reason for hiding this comment

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

This formatting change by black is problematic:

E       AssertionError: Wrong message(s) raised for "consider_using_dict_items.py":
E       
E       Expected in testdata:
E         97: consider-iterating-dictionary
E         97: consider-using-dict-items
E       
E       Unexpected in testdata:
E         96: consider-iterating-dictionary
E         96: consider-using-dict-items

Copy link
Member Author

Choose a reason for hiding this comment

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

(so I've manually set it back to the way it was before the black formatting).

Copy link
Contributor

github-actions bot commented May 4, 2024

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 1f2a0da

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

πŸ‘

@Pierre-Sassoulas Pierre-Sassoulas merged commit c864cd4 into pylint-dev:main May 4, 2024
44 checks passed
github-actions bot pushed a commit that referenced this pull request May 4, 2024
When iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes #9554

Co-authored-by: Pierre Sassoulas <[email protected]>>
(cherry picked from commit c864cd4)
Pierre-Sassoulas pushed a commit that referenced this pull request May 4, 2024
When iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes #9554

Co-authored-by: Pierre Sassoulas <[email protected]>>
(cherry picked from commit c864cd4)

Co-authored-by: Mark Byrne <[email protected]>
@mbyrnepr2 mbyrnepr2 deleted the 9554_false_positive_consider_using_dict_items branch May 5, 2024 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive "consider-using-dict-items" when iteration over dict.keys()
3 participants