Uh oh!
There was an error while loading. Please reload this page.
gh-120417: Add #noqa to used imports in the stdlib - #120421
Conversation
Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa". It avoids the temptation to remove an import which is used effectively.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
left a comment
There was a problem hiding this comment.
Thanks! Looks much better now, a couple of questions though:
Uh oh!
There was an error while loading. Please reload this page.
| try: | ||
| from _collections import _deque_iterator | ||
| from _collections import _deque_iterator # noqa | ||
| except ImportError: | ||
| pass |
There was a problem hiding this comment.
test_collections passes for me if I remove this import. Does it need to stay? It was added in 52f96d3
There was a problem hiding this comment.
@erlend-aasland@kumaraditya303: Do you know/recall why this symbol is exposed?
There was a problem hiding this comment.
Oh, test_deque fails if the import is removed. I expected deque to be tested as part of test_collections, but apparently it has its own test file. Could you add a comment that it is (apparently) required to expose it in the collections module in order for deque iterators to be pickled?
======================================================================
ERROR: test_iterator_pickle (test.test_deque.TestBasic.test_iterator_pickle)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/alexw/dev/cpython/Lib/test/test_deque.py", line 640, in test_iterator_pickle
dump = pickle.dumps((itorg, orig), proto)
_pickle.PicklingError: Can't pickle <class 'collections._deque_iterator'>: attribute lookup _deque_iterator on collections failed
----------------------------------------------------------------------| from _pyrepl.pager import (get_pager, pipe_pager, | ||
| plain_pager, tempfile_pager, tty_pager, | ||
| plain) # noqa |
There was a problem hiding this comment.
Which is the import being flagged as unused here? Why does it need to stay?
There was a problem hiding this comment.
pydoc.plain() is an alias to _pyrepl.pager.plain(). It has to stay since it's part of pydoc API, and test_pydoc fails if you remove it.
# noqa only applies to the current line, no?
There was a problem hiding this comment.
pydoc.plain()is an alias to_pyrepl.pager.plain(). It has to stay since it's part of pydoc API, and test_pydoc fails if you remove it.
I see, thanks. Maybe add a comment explaining that to the code?
# noqaonly applies to the current line, no?
It depends on the tool, the rule and the AST node. For example, in this:
fromcollectionsimport ( # noqa: F401deque,
OrderedDict,
ChainMap,
)the single noqa comment will cause Ruff to ignore the three unused imports deque, OrderedDict and ChainMap, because they constitute a single ImportFrom node in the AST and the noqa comment is applied on the starting line number of the AST node.
There was a problem hiding this comment.
I moved the import to a separated statement and added a comment.
gpshead
commented
Jun 12, 2024
Instead of blanket everything But at least we could fix those when ruff enters the modern era and names things to be understandable by code maintainers. |
vstinner
commented
Jun 12, 2024
On a line like
So you suggest using |
it's on our to-do list :-) Edit: and I see Zanie already said that on the Ruff issue tracker ;) |
gpshead
commented
Jun 12, 2024
yep, always list the specific error(s) being ignored in the comment. |
Uh oh!
There was an error while loading. Please reload this page.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
LGTM.
How did you know that all these imports were necessary? Or did you just flag all the names for which you can't confirm redundant imports?
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
Jun 13, 2024
I'm checking for unused imports for 5 years, and every year, I meet the same imports :-) Over time, I learnt which imports are done to populate an API, and which ones can be removed. You can try to remove them and see the test suite failing. |
serhiy-storchaka
commented
Jun 13, 2024
I agree with all this, I just wondering how did you find them, because the amount of the work is large, and many cases are not trivial. Removing imports unlit the code breaks is risky, not all can be covered by tests. |
vstinner
commented
Jun 13, 2024
I modified the PR to use |
vstinner
commented
Jun 13, 2024
I also rely on code review to validate my work :-) |
hugovk
commented
Jun 13, 2024
There's a rule (PGH004 cpython/Tools/clinic/.ruff.toml Line 9 in ca5108a Shall we add it to the other one, https://github.com/python/cpython/blob/main/Lib/test/.ruff.toml ? And maybe add another
If we added F401 |
vstinner
commented
Jun 13, 2024
I created a second PR for the 7 files which requires tons of |
vstinner
commented
Jun 13, 2024
Merged, thanks for reviews! @hugovk: I'm interested by a CI checking for unused imports, it's appealing :-) |
encukou
commented
Jun 17, 2024
For things that are re-exported for a public API, should add them to |
AlexWaygood
commented
Jun 17, 2024
usually IMO, yes |
Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa: F401". It avoids the temptation to remove an import which is used effectively.
Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa: F401". It avoids the temptation to remove an import which is used effectively.
Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa: F401". It avoids the temptation to remove an import which is used effectively.
Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa". It avoids the temptation to remove an import which is used effectively.