From 0b8c348f2756c193d6bd2618cadbb90b2f218ccc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 8 Dec 2025 14:00:31 +0100 Subject: [PATCH] Fix pyflakes warnings: variable is assigned to but never used (#142294) Example of fixed warning: Lib/netrc.py:98:13: local variable 'toplevel' is assigned to but never used --- Lib/_py_warnings.py | 1 - Lib/_threading_local.py | 2 +- Lib/asyncio/base_events.py | 4 ++-- Lib/asyncio/proactor_events.py | 2 +- Lib/asyncio/tools.py | 2 +- Lib/asyncio/unix_events.py | 2 +- Lib/codeop.py | 4 ++-- Lib/compileall.py | 2 +- Lib/concurrent/interpreters/_queues.py | 6 +++--- Lib/dis.py | 1 - Lib/encodings/uu_codec.py | 2 +- Lib/ftplib.py | 8 ++++---- Lib/functools.py | 2 +- Lib/idlelib/pyshell.py | 1 - Lib/idlelib/textview.py | 4 ++-- Lib/imaplib.py | 6 +++--- Lib/inspect.py | 1 - Lib/modulefinder.py | 1 - Lib/multiprocessing/connection.py | 3 +-- Lib/netrc.py | 2 +- Lib/ntpath.py | 2 +- Lib/optparse.py | 2 +- Lib/pickle.py | 5 ++--- Lib/platform.py | 3 +-- Lib/pyclbr.py | 1 - Lib/re/_parser.py | 1 - Lib/subprocess.py | 2 +- Lib/tempfile.py | 2 +- Lib/tokenize.py | 2 +- Lib/turtle.py | 4 ++-- Lib/urllib/parse.py | 2 +- Lib/venv/__init__.py | 1 - 32 files changed, 36 insertions(+), 47 deletions(-) diff --git a/Lib/_py_warnings.py b/Lib/_py_warnings.py index 67c74fdd2d0b..d5a9cec86f36 100644 --- a/Lib/_py_warnings.py +++ b/Lib/_py_warnings.py @@ -563,7 +563,6 @@ def warn_explicit(message, category, filename, lineno, else: text = message message = category(message) - modules = None key = (text, category, lineno) with _wm._lock: if registry is None: diff --git a/Lib/_threading_local.py b/Lib/_threading_local.py index 0b9e5d3bbf6e..2af3885458b5 100644 --- a/Lib/_threading_local.py +++ b/Lib/_threading_local.py @@ -57,7 +57,7 @@ class _localimpl: # as soon as the OS-level thread ends instead. local = wrlocal() if local is not None: - dct = local.dicts.pop(idt) + local.dicts.pop(idt) wrlocal = ref(self, local_deleted) wrthread = ref(thread, thread_deleted) thread.__dict__[key] = wrlocal diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 8cbb71f70853..6619c87bcf5b 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -949,7 +949,7 @@ class BaseEventLoop(events.AbstractEventLoop): try: return await self._sock_sendfile_native(sock, file, offset, count) - except exceptions.SendfileNotAvailableError as exc: + except exceptions.SendfileNotAvailableError: if not fallback: raise return await self._sock_sendfile_fallback(sock, file, @@ -1270,7 +1270,7 @@ class BaseEventLoop(events.AbstractEventLoop): try: return await self._sendfile_native(transport, file, offset, count) - except exceptions.SendfileNotAvailableError as exc: + except exceptions.SendfileNotAvailableError: if not fallback: raise diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py index f404273c3ae5..3fa93b14a678 100644 --- a/Lib/asyncio/proactor_events.py +++ b/Lib/asyncio/proactor_events.py @@ -733,7 +733,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): async def _sock_sendfile_native(self, sock, file, offset, count): try: fileno = file.fileno() - except (AttributeError, io.UnsupportedOperation) as err: + except (AttributeError, io.UnsupportedOperation): raise exceptions.SendfileNotAvailableError("not a regular file") try: fsize = os.fstat(fileno).st_size diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index 1d463ea09ba5..f9b8a4ee56c5 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -244,7 +244,7 @@ def _get_awaited_by_tasks(pid: int) -> list: e = e.__context__ print(f"Error retrieving tasks: {e}") sys.exit(1) - except PermissionError as e: + except PermissionError: exit_with_permission_help_text() diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 1c1458127db5..49e8067ee7b4 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -359,7 +359,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): "os.sendfile() is not available") try: fileno = file.fileno() - except (AttributeError, io.UnsupportedOperation) as err: + except (AttributeError, io.UnsupportedOperation): raise exceptions.SendfileNotAvailableError("not a regular file") try: fsize = os.fstat(fileno).st_size diff --git a/Lib/codeop.py b/Lib/codeop.py index 8cac00442d99..40e88423119b 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -66,9 +66,9 @@ def _maybe_compile(compiler, source, filename, symbol, flags): try: compiler(source + "\n", filename, symbol, flags=flags) return None - except _IncompleteInputError as e: + except _IncompleteInputError: return None - except SyntaxError as e: + except SyntaxError: pass # fallthrough diff --git a/Lib/compileall.py b/Lib/compileall.py index 67fe370451e1..9519a5ac16f0 100644 --- a/Lib/compileall.py +++ b/Lib/compileall.py @@ -223,7 +223,7 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, cfile = importlib.util.cache_from_source(fullname) opt_cfiles[opt_level] = cfile - head, tail = name[:-3], name[-3:] + tail = name[-3:] if tail == '.py': if not force: try: diff --git a/Lib/concurrent/interpreters/_queues.py b/Lib/concurrent/interpreters/_queues.py index b5cc0b894494..ee159d7de638 100644 --- a/Lib/concurrent/interpreters/_queues.py +++ b/Lib/concurrent/interpreters/_queues.py @@ -223,7 +223,7 @@ class Queue: while True: try: _queues.put(self._id, obj, unboundop) - except QueueFull as exc: + except QueueFull: if timeout is not None and time.time() >= end: raise # re-raise time.sleep(_delay) @@ -258,7 +258,7 @@ class Queue: while True: try: obj, unboundop = _queues.get(self._id) - except QueueEmpty as exc: + except QueueEmpty: if timeout is not None and time.time() >= end: raise # re-raise time.sleep(_delay) @@ -277,7 +277,7 @@ class Queue: """ try: obj, unboundop = _queues.get(self._id) - except QueueEmpty as exc: + except QueueEmpty: raise # re-raise if unboundop is not None: assert obj is None, repr(obj) diff --git a/Lib/dis.py b/Lib/dis.py index d6d2c1386dd7..8c257d118fb2 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -530,7 +530,6 @@ class Formatter: fields.append(instr.opname.ljust(_OPNAME_WIDTH)) # Column: Opcode argument if instr.arg is not None: - arg = repr(instr.arg) # If opname is longer than _OPNAME_WIDTH, we allow it to overflow into # the space reserved for oparg. This results in fewer misaligned opargs # in the disassembly output. diff --git a/Lib/encodings/uu_codec.py b/Lib/encodings/uu_codec.py index 4e58c62fe9ef..4f8704016e21 100644 --- a/Lib/encodings/uu_codec.py +++ b/Lib/encodings/uu_codec.py @@ -56,7 +56,7 @@ def uu_decode(input, errors='strict'): break try: data = binascii.a2b_uu(s) - except binascii.Error as v: + except binascii.Error: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((s[0]-32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 50771e8c17c2..640acc64f620 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -314,9 +314,9 @@ class FTP: port = sock.getsockname()[1] # Get proper port host = self.sock.getsockname()[0] # Get proper host if self.af == socket.AF_INET: - resp = self.sendport(host, port) + self.sendport(host, port) else: - resp = self.sendeprt(host, port) + self.sendeprt(host, port) if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(self.timeout) return sock @@ -455,7 +455,7 @@ class FTP: """ if callback is None: callback = print_line - resp = self.sendcmd('TYPE A') + self.sendcmd('TYPE A') with self.transfercmd(cmd) as conn, \ conn.makefile('r', encoding=self.encoding) as fp: while 1: @@ -951,7 +951,7 @@ def test(): elif file[:2] == '-d': cmd = 'CWD' if file[2:]: cmd = cmd + ' ' + file[2:] - resp = ftp.sendcmd(cmd) + ftp.sendcmd(cmd) elif file == '-p': ftp.set_pasv(not ftp.passiveserver) else: diff --git a/Lib/functools.py b/Lib/functools.py index 8063eb5ffc33..836eb680ccd4 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -687,7 +687,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): # still adjusting the links. root = oldroot[NEXT] oldkey = root[KEY] - oldresult = root[RESULT] + oldresult = root[RESULT] # noqa: F841 root[KEY] = root[RESULT] = None # Now update the cache dictionary. diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 1b7c2af1a923..b80c8e56c928 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -498,7 +498,6 @@ class ModifiedInterpreter(InteractiveInterpreter): self.rpcclt.close() self.terminate_subprocess() console = self.tkconsole - was_executing = console.executing console.executing = False self.spawn_subprocess() try: diff --git a/Lib/idlelib/textview.py b/Lib/idlelib/textview.py index 23f0f4cb5027..0f719a06883a 100644 --- a/Lib/idlelib/textview.py +++ b/Lib/idlelib/textview.py @@ -129,8 +129,8 @@ class ViewWindow(Toplevel): self.title(title) self.viewframe = ViewFrame(self, contents, wrap=wrap) self.protocol("WM_DELETE_WINDOW", self.ok) - self.button_ok = button_ok = Button(self, text='Close', - command=self.ok, takefocus=False) + self.button_ok = Button(self, text='Close', + command=self.ok, takefocus=False) self.viewframe.pack(side='top', expand=True, fill='both') self.is_modal = modal diff --git a/Lib/imaplib.py b/Lib/imaplib.py index c17673654818..22a0afcd9815 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -808,7 +808,7 @@ class IMAP4: """ name = 'PROXYAUTH' - return self._simple_command('PROXYAUTH', user) + return self._simple_command(name, user) def rename(self, oldmailbox, newmailbox): @@ -1310,7 +1310,7 @@ class IMAP4: try: self._get_response() - except self.abort as val: + except self.abort: if __debug__: if self.debug >= 1: self.print_log() @@ -1867,7 +1867,7 @@ if __name__ == '__main__': try: optlist, args = getopt.getopt(sys.argv[1:], 'd:s:') - except getopt.error as val: + except getopt.error: optlist, args = (), () stream_command = None diff --git a/Lib/inspect.py b/Lib/inspect.py index 8e7511b3af01..ff462750888c 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2140,7 +2140,6 @@ def _signature_strip_non_python_syntax(signature): current_parameter = 0 OP = token.OP - ERRORTOKEN = token.ERRORTOKEN # token stream always starts with ENCODING token, skip it t = next(token_stream) diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index b115d99ab30f..7fb19a5c5d18 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -400,7 +400,6 @@ class ModuleFinder: yield "relative_import", (level, fromlist, name) def scan_code(self, co, m): - code = co.co_code scanner = self.scan_opcodes for what, args in scanner(co): if what == "store": diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index fc00d2861260..64ec53884aeb 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -709,8 +709,7 @@ if sys.platform == 'win32': # written data and then disconnected -- see Issue 14725. else: try: - res = _winapi.WaitForMultipleObjects( - [ov.event], False, INFINITE) + _winapi.WaitForMultipleObjects([ov.event], False, INFINITE) except: ov.cancel() _winapi.CloseHandle(handle) diff --git a/Lib/netrc.py b/Lib/netrc.py index 2f502c1d5336..750b5071e3c6 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -95,7 +95,7 @@ class netrc: while 1: # Look for a machine, default, or macdef top-level keyword saved_lineno = lexer.lineno - toplevel = tt = lexer.get_token() + tt = lexer.get_token() if not tt: break elif tt[0] == '#': diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 77d2bf86a5f0..7d637325240f 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -726,7 +726,7 @@ else: try: if _getfinalpathname(spath) == path: path = spath - except ValueError as ex: + except ValueError: # Unexpected, as an invalid path should not have gained a prefix # at any point, but we ignore this error just in case. pass diff --git a/Lib/optparse.py b/Lib/optparse.py index 02ff7140882e..5ff7f74754f9 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -1372,7 +1372,7 @@ class OptionParser (OptionContainer): self.values = values try: - stop = self._process_args(largs, rargs, values) + self._process_args(largs, rargs, values) except (BadOptionError, OptionValueError) as err: self.error(str(err)) diff --git a/Lib/pickle.py b/Lib/pickle.py index f3025776623d..71c12c50f7f0 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1169,7 +1169,6 @@ class _Pickler: def save_global(self, obj, name=None): write = self.write - memo = self.memo if name is None: name = getattr(obj, '__qualname__', None) @@ -1756,7 +1755,7 @@ class _Unpickler: i = self.read(1)[0] try: self.append(self.memo[i]) - except KeyError as exc: + except KeyError: msg = f'Memo value not found at index {i}' raise UnpicklingError(msg) from None dispatch[BINGET[0]] = load_binget @@ -1765,7 +1764,7 @@ class _Unpickler: i, = unpack('