GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-120417: Add #noqa to used imports in the stdlib by vstinner · Pull Request #120421 · python/cpython Conversation
|
| try: | ||
| from _collections import _deque_iterator | ||
| from _collections import _deque_iterator # noqa | ||
| except ImportError: | ||
| pass |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
test_collections passes for me if I remove this import. Does it need to stay? It was added in 52f96d3
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@erlend-aasland @kumaraditya303: Do you know/recall why this symbol is exposed?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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 ----------------------------------------------------------------------
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sure, I added a comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| from _pyrepl.pager import (get_pager, pipe_pager, | ||
| plain_pager, tempfile_pager, tty_pager, | ||
| plain) # noqa |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Which is the import being flagged as unused here? Why does it need to stay?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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:
from collections import ( # noqa: F401 deque, 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I moved the import to a separated statement and added a comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
On a line like
So you suggest using |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
it's on our to-do list :-) Edit: and I see Zanie already said that on the Ruff issue tracker ;) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would also prefer that we ignore a specific linter error (F401) rather than all linter errors. But other than that, this LGTM now, thanks (though some tests seem to be failing)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
yep, always list the specific error(s) being ignored in the comment. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I modified the PR to use |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I also rely on code review to validate my work :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I created a second PR for the 7 files which requires tons of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Merged, thanks for reviews! @hugovk: I'm interested by a CI checking for unused imports, it's appealing :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
For things that are re-exported for a public API, should add them to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
usually IMO, yes |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.