Skip to content

Commit

Permalink
comprehensive overhaul of unused imports for mypy-1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Jan 12, 2024
1 parent ddde576 commit c04df80
Show file tree
Hide file tree
Showing 72 changed files with 220 additions and 147 deletions.
78 changes: 76 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ gtk-platform = [

mypy = [
"twisted[dev,all-non-platform]",
"mypy~=1.7",
"mypy-zope~=1.0.2",
"mypy~=1.8",
"mypy-zope~=1.0.3",
"types-setuptools",
"types-pyOpenSSL",
]
Expand Down Expand Up @@ -313,6 +313,80 @@ strict_equality = false
no_implicit_reexport = false
# Disable some checks until the effected modules fully adopt mypy

[[tool.mypy.overrides]]
ignore_missing_imports = true
follow_imports = "silent"
follow_imports_for_stubs = true
module = [

# pyobjc is not installed on linux
"CFNetwork",
"CoreFoundation",

# neither is pywin32
"pywintypes",
"win32api",
"win32con",
"win32com.*",
"win32event",
"win32file",
"win32pipe",
"win32process",
"win32security",
"win32console",

# pyserial is very optional
"serial",
"serial.*",

# GObject Introspection too
"gi",
"gi.repository.*",

# we need to clean this up, because it's ancient and probably unsupported,
# but as long as it's still imported, we don't want to type-check it
"gobject",
"pygtk",
"gtk",

# and wxPython
"wxPython.wx",
"wx",

# constantly doesn't have type stubs yet
"constantly",

# h2 doesn't have types yet and is optional besides
"h2.*",
"hpack.*",
"priority",

# subunit is untyped
"subunit",

# oh this is fun, not even totally sure what library this is, since it's not
# documented as an extra.
"utmp",

# automat is not typed, or typeable, at least until
# https://github.com/glyph/automat/pull/137 is landed
"automat",

# I think only used by `twisted.web.script.ResourceTemplate`
"quixote",

"cython_test_exception_raiser",

# various obscure database modules you shouldn't need installed in order to
# type-check
"pyPgSQL",
"psycopg",
"MySQLdb",
"kinterbasdb",

"SOAPpy",
]

[[tool.mypy.overrides]]
allow_untyped_defs = true
check_untyped_defs = false
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/application/internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from random import random as _goodEnoughRandom
from typing import List

from automat import MethodicalMachine # type: ignore[import]
from automat import MethodicalMachine

from twisted.application import service
from twisted.internet import task
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/application/runner/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Any, Callable, Mapping, TextIO

from attr import Factory, attrib, attrs
from constantly import NamedConstant # type: ignore[import]
from constantly import NamedConstant

from twisted.internet.interfaces import IReactorCore
from twisted.logger import (
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/conch/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from twisted.python.compat import nativeString

try:
import utmp # type: ignore[import]
import utmp
except ImportError:
utmp = None

Expand Down
16 changes: 8 additions & 8 deletions src/twisted/internet/_dumbwin32proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

from zope.interface import implementer

import pywintypes # type: ignore[import]
import pywintypes

# Win32 imports
import win32api # type: ignore[import]
import win32con # type: ignore[import]
import win32event # type: ignore[import]
import win32file # type: ignore[import]
import win32pipe # type: ignore[import]
import win32process # type: ignore[import]
import win32security # type: ignore[import]
import win32api
import win32con
import win32event
import win32file
import win32pipe
import win32process
import win32security

from twisted.internet import _pollingfile, error
from twisted.internet._baseprocess import BaseProcess
Expand Down
6 changes: 3 additions & 3 deletions src/twisted/internet/_glibbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from twisted.internet.interfaces import IReactorFDSet, IReadDescriptor, IWriteDescriptor
from twisted.python import log
from twisted.python.monkey import MonkeyPatcher
from ._signals import _UnixWaker
from ._signals import _IWaker, _UnixWaker


def ensureNotImported(moduleNames, errorMessage, preventImports=[]):
Expand Down Expand Up @@ -77,7 +77,7 @@ def _signalGlue():
reactor main loop which makes our signal handling work with glib's signal
handling.
"""
from gi import _ossighelper as signalGlue # type: ignore[import]
from gi import _ossighelper as signalGlue

patcher = MonkeyPatcher()
patcher.addPatch(signalGlue, "_wakeup_fd_is_active", True)
Expand Down Expand Up @@ -129,7 +129,7 @@ class GlibReactorBase(posixbase.PosixReactorBase, posixbase._PollLikeMixin):

# Install a waker that knows it needs to call C{_simulate} in order to run
# callbacks queued from a thread:
def _wakerFactory(self) -> GlibWaker:
def _wakerFactory(self) -> _IWaker:
return GlibWaker(self)

def __init__(self, glib_module: Any, gtk_module: Any, useGtk: bool = False) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/twisted/internet/_pollingfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ def _pollEvent(self):
# If we ever (let's hope not) need the above functionality on UNIX, this could
# be factored into a different module.

import pywintypes # type: ignore[import]
import win32api # type: ignore[import]
import win32file # type: ignore[import]
import win32pipe # type: ignore[import]
import pywintypes
import win32api
import win32file
import win32pipe


@implementer(IPushProducer)
Expand Down
3 changes: 1 addition & 2 deletions src/twisted/internet/_posixserialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

# dependent on pyserial ( http://pyserial.sf.net/ )
# only tested w/ 1.18 (5 Dec 2002)
from serial import PARITY_NONE # type: ignore[import]
from serial import EIGHTBITS, STOPBITS_ONE
from serial import EIGHTBITS, PARITY_NONE, STOPBITS_ONE

from twisted.internet import abstract, fdesc
from twisted.internet.serialport import BaseSerialPort
Expand Down
4 changes: 2 additions & 2 deletions src/twisted/internet/_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ class _IWaker(Interface):

disconnected = Attribute("")

def wakeUp():
def wakeUp() -> None:
"""
Called when the event should be wake up.
"""

def doRead():
def doRead() -> None:
"""
Read some data from my connection and discard it.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/_sslverify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from OpenSSL._util import lib as pyOpenSSLlib

import attr
from constantly import FlagConstant, Flags, NamedConstant, Names # type: ignore[import]
from constantly import FlagConstant, Flags, NamedConstant, Names
from incremental import Version

from twisted.internet.abstract import isIPAddress, isIPv6Address
Expand Down
9 changes: 4 additions & 5 deletions src/twisted/internet/_win32serialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
"""


import win32event # type: ignore[import]
import win32file # type: ignore[import]
import win32event
import win32file

# system imports
from serial import PARITY_NONE # type: ignore[import]
from serial import EIGHTBITS, STOPBITS_ONE
from serial.serialutil import to_bytes # type: ignore[import]
from serial import EIGHTBITS, PARITY_NONE, STOPBITS_ONE
from serial.serialutil import to_bytes

# twisted imports
from twisted.internet import abstract
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/_win32stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from zope.interface import implementer

import win32api # type: ignore[import]
import win32api

from twisted.internet import _pollingfile, main
from twisted.internet.interfaces import (
Expand Down
4 changes: 2 additions & 2 deletions src/twisted/internet/cfreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from zope.interface import implementer

from CFNetwork import ( # type: ignore[import]
from CFNetwork import (
CFSocketCreateRunLoopSource,
CFSocketCreateWithNative,
CFSocketDisableCallBacks,
Expand All @@ -30,7 +30,7 @@
kCFSocketReadCallBack,
kCFSocketWriteCallBack,
)
from CoreFoundation import ( # type: ignore[import]
from CoreFoundation import (

Check warning on line 33 in src/twisted/internet/cfreactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/cfreactor.py#L33

Added line #L33 was not covered by tests
CFAbsoluteTimeGetCurrent,
CFRunLoopAddSource,
CFRunLoopAddTimer,
Expand Down
13 changes: 6 additions & 7 deletions src/twisted/internet/defer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ def cancel(self) -> None:


def _parseDeferredListResult(
resultList: List[_DeferredListResultItemT[_T]], fireOnOneErrback: bool = False
resultList: List[_DeferredListResultItemT[_T]], fireOnOneErrback: bool = False, /
) -> List[_T]:
if __debug__:
for result in resultList:
Expand Down Expand Up @@ -1617,9 +1617,9 @@ def gatherResults(
is useful to prevent spurious 'Unhandled error in Deferred' messages
from being logged. This parameter is available since 11.1.0.
"""
d = DeferredList(deferredList, fireOnOneErrback=True, consumeErrors=consumeErrors)
d.addCallback(_parseDeferredListResult)
return cast(Deferred[List[_T]], d)
return DeferredList(
deferredList, fireOnOneErrback=True, consumeErrors=consumeErrors
).addCallback(_parseDeferredListResult)


class FailureGroup(Exception):
Expand Down Expand Up @@ -2116,14 +2116,13 @@ def _addCancelCallbackToDeferred(
@param status: a L{_CancellationStatus} tracking the current status of C{gen}
"""
it.callbacks, tmp = [], it.callbacks
it.addErrback(_handleCancelInlineCallbacks, status)
it = it.addErrback(_handleCancelInlineCallbacks, status)
it.callbacks.extend(tmp)
it.errback(_InternalInlineCallbacksCancelledError())


def _handleCancelInlineCallbacks(
result: Failure,
status: _CancellationStatus[_T],
result: Failure, status: _CancellationStatus[_T], /
) -> Deferred[_T]:
"""
Propagate the cancellation of an C{@}L{inlineCallbacks} to the
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from zope.interface import directlyProvides, implementer, provider

from constantly import NamedConstant, Names # type: ignore[import]
from constantly import NamedConstant, Names
from incremental import Version

from twisted.internet import defer, error, fdesc, interfaces, threads
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/gireactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from typing import Union

from gi.repository import GLib # type:ignore[import]
from gi.repository import GLib

from twisted.internet import _glibbase
from twisted.internet.error import ReactorAlreadyRunning
Expand Down
6 changes: 3 additions & 3 deletions src/twisted/internet/gtk2reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
try:
if not hasattr(sys, "frozen"):
# Don't want to check this for py2exe
import pygtk # type: ignore[import]
import pygtk

pygtk.require("2.0")
except (ImportError, AttributeError):
pass # maybe we're using pygtk before this hack existed.

import gobject # type: ignore[import]
import gobject

if not hasattr(gobject, "IO_HUP"):
# gi.repository's legacy compatibility helper raises an AttributeError with
Expand All @@ -74,7 +74,7 @@ class Gtk2Reactor(_glibbase.GlibReactorBase):
def __init__(self, useGtk=True):
_gtk = None
if useGtk is True:
import gtk as _gtk # type: ignore[import]
import gtk as _gtk

_glibbase.GlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)

Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/iocpreactor/iocpsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"send",
]

from twisted_iocpsupport.iocpsupport import ( # type: ignore[import]
from twisted_iocpsupport.iocpsupport import ( # type: ignore[import-not-found]
CompletionPort,
Event,
accept,
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/posixbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

if platform.isWindows():
try:
import win32process # type: ignore[import]
import win32process

processEnabled = True
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/pyuisupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

# System imports
import pyui # type: ignore[import]
import pyui # type: ignore[import-not-found]

Check warning on line 14 in src/twisted/internet/pyuisupport.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/pyuisupport.py#L14

Added line #L14 was not covered by tests


def _guiUpdate(reactor, delay):
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/serialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]

# all of them require pyserial at the moment, so check that first
import serial # type: ignore[import]
import serial
from serial import (
EIGHTBITS,
FIVEBITS,
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/test/process_gireactornocompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
sys.exit(0)

try:
import gobject # type: ignore[import]
import gobject # type: ignore[import-not-found]

Check warning on line 21 in src/twisted/internet/test/process_gireactornocompat.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/process_gireactornocompat.py#L21

Added line #L21 was not covered by tests
except ImportError:
sys.stdout.write("success")
else:
Expand Down

0 comments on commit c04df80

Please sign in to comment.