Skip to content

gh-120417: Add #noqa to used imports in the stdlib #120421

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 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_setmode = None

import io
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END) # noqa

valid_seek_flags = {0, 1, 2} # Hardwired values
if hasattr(os, 'SEEK_HOLE') :
Expand Down
2 changes: 1 addition & 1 deletion Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa
console.raw_input = readfunc
else:
try:
import readline
import readline # noqa
except ImportError:
pass
console.interact(banner, exitmsg)
Expand Down
2 changes: 1 addition & 1 deletion Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,4 @@ def make_encoding_map(decoding_map):
# package
_false = 0
if _false:
import encodings
import encodings # noqa
2 changes: 1 addition & 1 deletion Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
_collections_abc.MutableSequence.register(deque)

try:
from _collections import _deque_iterator
from _collections import _deque_iterator # noqa
except ImportError:
pass
Copy link
Member

Choose a reason for 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

Copy link
Member Author

Choose a reason for 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?

Copy link
Member

@AlexWaygood AlexWaygood Jun 12, 2024

Choose a reason for 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

----------------------------------------------------------------------

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I added a comment.


Expand Down
2 changes: 1 addition & 1 deletion Lib/concurrent/futures/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def _check_system_limits():
raise NotImplementedError(_system_limited)
_system_limits_checked = True
try:
import multiprocessing.synchronize
import multiprocessing.synchronize # noqa
except ImportError:
_system_limited = (
"This Python build lacks multiprocessing.synchronize, usually due "
Expand Down
2 changes: 1 addition & 1 deletion Lib/curses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def start_color():
try:
has_key
except NameError:
from .has_key import has_key
from .has_key import has_key # noqa

# Wrapper for the entire curses-based application. Runs a function which
# should be the rest of your curses-based application. If the application
Expand Down
4 changes: 2 additions & 2 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
try:
from _datetime import *
from _datetime import __doc__
from _datetime import __doc__ # noqa
except ImportError:
from _pydatetime import *
from _pydatetime import __doc__
from _pydatetime import __doc__ # noqa

__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
"MINYEAR", "MAXYEAR", "UTC")
8 changes: 4 additions & 4 deletions Lib/decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@

try:
from _decimal import *
from _decimal import __version__
from _decimal import __libmpdec_version__
from _decimal import __version__ # noqa
from _decimal import __libmpdec_version__ # noqa
except ImportError:
from _pydecimal import *
from _pydecimal import __version__
from _pydecimal import __libmpdec_version__
from _pydecimal import __version__ # noqa
from _pydecimal import __libmpdec_version__ # noqa
2 changes: 1 addition & 1 deletion Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __hash_new(name, data=b'', **kwargs):

try:
# OpenSSL's scrypt requires OpenSSL 1.1+
from _hashlib import scrypt
from _hashlib import scrypt # noqa
except ImportError:
pass

Expand Down
2 changes: 1 addition & 1 deletion Lib/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io
import os
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
from _lzma import _encode_filter_properties, _decode_filter_properties # noqa
import _compression


Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def allow_connection_pickling(self):
'''
# This is undocumented. In previous versions of multiprocessing
# its only effect was to make socket objects inheritable on Windows.
from . import connection
from . import connection # noqa

def set_executable(self, executable):
'''Sets the path to a python.exe or pythonw.exe binary used to run
Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import atexit
import threading # we want threading to install it's
# cleanup function before multiprocessing does
from subprocess import _args_from_interpreter_flags
from subprocess import _args_from_interpreter_flags # noqa

from . import process

Expand Down
4 changes: 2 additions & 2 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import _opcode
from _opcode import stack_effect

from _opcode_metadata import (_specializations, _specialized_opmap, opmap,
HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE)
from _opcode_metadata import (_specializations, _specialized_opmap, opmap, # noqa
HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE) # noqa
EXTENDED_ARG = opmap['EXTENDED_ARG']

opname = ['<%r>' % (op,) for op in range(max(opmap.values()) + 1)]
Expand Down
2 changes: 1 addition & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def ixor(a, b):
except ImportError:
pass
else:
from _operator import __doc__
from _operator import __doc__ # noqa

# All of these "__func__ = func" assignments have to happen after importing
# from _operator to make sure they're set to the right function
Expand Down
2 changes: 1 addition & 1 deletion Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')):
warnings._deprecated('java_ver', remove=(3, 15))
# Import the needed APIs
try:
import java.lang
import java.lang # noqa
except ImportError:
return release, vendor, vminfo, osinfo

Expand Down
2 changes: 1 addition & 1 deletion Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def f8(x):
if __name__ == '__main__':
import cmd
try:
import readline
import readline # noqa
except ImportError:
pass

Expand Down
5 changes: 3 additions & 2 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class or function within a module or module in a package. If the
from reprlib import Repr
from traceback import format_exception_only

from _pyrepl.pager import (get_pager, plain, pipe_pager,
plain_pager, tempfile_pager, tty_pager)
from _pyrepl.pager import (get_pager, pipe_pager,
plain_pager, tempfile_pager, tty_pager,
plain) # noqa
Copy link
Member

Choose a reason for 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?

Copy link
Member Author

Choose a reason for 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?

Copy link
Member

@AlexWaygood AlexWaygood Jun 12, 2024

Choose a reason for 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?

# noqa only 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.

Copy link
Member Author

Choose a reason for 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.



# --------------------------------------------------------- old names
Expand Down
2 changes: 1 addition & 1 deletion Lib/re/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

MAGIC = 20230612

from _sre import MAXREPEAT, MAXGROUPS
from _sre import MAXREPEAT, MAXGROUPS # noqa

# SRE standard exception (access as sre.error)
# should this really be here?
Expand Down
2 changes: 1 addition & 1 deletion Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def main(*args):
# No SQL provided; start the REPL.
console = SqliteInteractiveConsole(con)
try:
import readline
import readline # noqa
except ImportError:
pass
console.interact(banner, exitmsg="")
Expand Down
4 changes: 2 additions & 2 deletions Lib/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
]

from _struct import *
from _struct import _clearcache
from _struct import __doc__
from _struct import _clearcache # noqa
from _struct import __doc__ # noqa
6 changes: 3 additions & 3 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import _symtable
from _symtable import (
USE,
DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL,
DEF_PARAM, DEF_TYPE_PARAM,
DEF_FREE_CLASS,
DEF_GLOBAL, # noqa
DEF_NONLOCAL, DEF_LOCAL,
DEF_PARAM, DEF_TYPE_PARAM, DEF_FREE_CLASS,
DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
DEF_COMP_ITER, DEF_COMP_CELL,
SCOPE_OFF, SCOPE_MASK,
Expand Down
4 changes: 2 additions & 2 deletions Lib/unittest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def testMultiply(self):
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
skipIf, skipUnless, expectedFailure, doModuleCleanups,
enterModuleContext)
from .suite import BaseTestSuite, TestSuite
from .suite import BaseTestSuite, TestSuite # noqa
from .loader import TestLoader, defaultTestLoader
from .main import TestProgram, main
from .main import TestProgram, main # noqa
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
# IsolatedAsyncioTestCase will be imported lazily.
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

# check for SSL
try:
import ssl
import ssl # noqa
except ImportError:
_have_ssl = False
else:
Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/dom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ class UserDataHandler:
EMPTY_NAMESPACE = None
EMPTY_PREFIX = None

from .domreg import getDOMImplementation, registerDOMImplementation
from .domreg import getDOMImplementation, registerDOMImplementation # noqa
Loading