Skip to content

Commit

Permalink
twisted#11922 disallow incomplete defs in mypy config (twisted#11923)
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Aug 28, 2023
2 parents 21f77b3 + dc0f060 commit e7bd373
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 51 deletions.
26 changes: 5 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ module = [
'twisted.trial.*',
'twisted.web.*',
'twisted.words.*',
'twisted.test.test_plugin',
'twisted.internet.testing',
'twisted.internet.test.test_base',
'twisted.internet.protocol',
'twisted.internet.posixbase',
]

[[tool.mypy.overrides]]
Expand Down Expand Up @@ -633,27 +638,6 @@ module = [
'twisted.internet.tcp',
]

[[tool.mypy.overrides]]
allow_untyped_defs = true
check_untyped_defs = false
allow_incomplete_defs = true
module = [
'twisted.internet.posixbase',
'twisted.internet.protocol',
'twisted.internet.test.test_base',
'twisted.internet.testing',
'twisted.test.test_plugin',
]

[[tool.mypy.overrides]]
allow_incomplete_defs = true
module = [
'twisted.mail.relaymanager',
'twisted.web.test.test_util',
'twisted.web.util',
'twisted.words.im.basesupport',
]

[[tool.mypy.overrides]]
allow_untyped_defs = false
module = [
Expand Down
30 changes: 15 additions & 15 deletions src/twisted/internet/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


import random
from typing import Callable, Optional
from typing import Any, Callable, Optional

from zope.interface import implementer

Expand Down Expand Up @@ -553,7 +553,7 @@ def logPrefix(self):
"""
return self.__class__.__name__

def dataReceived(self, data: bytes):
def dataReceived(self, data: bytes) -> None:
"""
Called whenever data is received.
Expand All @@ -568,7 +568,7 @@ def dataReceived(self, data: bytes):
differing chunk sizes, down to one byte at a time.
"""

def connectionLost(self, reason: failure.Failure = connectionDone):
def connectionLost(self, reason: failure.Failure = connectionDone) -> None:
"""
Called when the connection is shut down.
Expand All @@ -581,7 +581,7 @@ def connectionLost(self, reason: failure.Failure = connectionDone):

@implementer(interfaces.IConsumer)
class ProtocolToConsumerAdapter(components.Adapter):
def write(self, data: bytes):
def write(self, data: bytes) -> None:
self.original.dataReceived(data)

def registerProducer(self, producer, streaming):
Expand All @@ -598,10 +598,10 @@ def unregisterProducer(self):

@implementer(interfaces.IProtocol)
class ConsumerToProtocolAdapter(components.Adapter):
def dataReceived(self, data: bytes):
def dataReceived(self, data: bytes) -> None:
self.original.write(data)

def connectionLost(self, reason: failure.Failure):
def connectionLost(self, reason: failure.Failure) -> None:
pass

def makeConnection(self, transport):
Expand All @@ -625,23 +625,23 @@ class ProcessProtocol(BaseProtocol):

transport: Optional[interfaces.IProcessTransport] = None

def childDataReceived(self, childFD: int, data: bytes):
def childDataReceived(self, childFD: int, data: bytes) -> None:
if childFD == 1:
self.outReceived(data)
elif childFD == 2:
self.errReceived(data)

def outReceived(self, data: bytes):
def outReceived(self, data: bytes) -> None:
"""
Some data was received from stdout.
"""

def errReceived(self, data: bytes):
def errReceived(self, data: bytes) -> None:
"""
Some data was received from stderr.
"""

def childConnectionLost(self, childFD: int):
def childConnectionLost(self, childFD: int) -> None:
if childFD == 0:
self.inConnectionLost()
elif childFD == 1:
Expand All @@ -664,14 +664,14 @@ def errConnectionLost(self):
This will be called when stderr is closed.
"""

def processExited(self, reason: failure.Failure):
def processExited(self, reason: failure.Failure) -> None:
"""
This will be called when the subprocess exits.
@type reason: L{twisted.python.failure.Failure}
"""

def processEnded(self, reason: failure.Failure):
def processEnded(self, reason: failure.Failure) -> None:
"""
Called when the child process exits and all file descriptors
associated with it have been closed.
Expand Down Expand Up @@ -746,7 +746,7 @@ def makeConnection(self, transport):
self.transport = transport
self.doStart()

def datagramReceived(self, datagram: bytes, addr):
def datagramReceived(self, datagram: bytes, addr: Any) -> None:
"""
Called when a datagram is received.
Expand Down Expand Up @@ -796,7 +796,7 @@ def datagramReceived(self, datagram):
@param datagram: the string received from the transport.
"""

def connectionFailed(self, failure: failure.Failure):
def connectionFailed(self, failure: failure.Failure) -> None:
"""
Called if connecting failed.
Expand All @@ -821,7 +821,7 @@ class FileWrapper:
def __init__(self, file):
self.file = file

def write(self, data: bytes):
def write(self, data: bytes) -> None:
try:
self.file.write(data)
except BaseException:
Expand Down
16 changes: 12 additions & 4 deletions src/twisted/internet/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import socket
from queue import Queue
from typing import Any, Callable
from typing import Callable
from unittest import skipIf

from zope.interface import implementer

from typing_extensions import ParamSpec

from twisted.internet._resolver import FirstOneWins
from twisted.internet.base import DelayedCall, ReactorBase, ThreadedResolver
from twisted.internet.defer import Deferred
Expand All @@ -28,6 +30,8 @@
else:
signal = _signal

_P = ParamSpec("_P")


@implementer(IReactorTime, IReactorThreads)
class FakeReactor:
Expand All @@ -46,7 +50,9 @@ def __init__(self):

self._threadCalls = Queue()

def callFromThread(self, callable: Callable[..., Any], *args, **kwargs):
def callFromThread(
self, callable: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs
) -> None:
self._threadCalls.put((callable, args, kwargs))

def _runThreadCalls(self):
Expand All @@ -60,11 +66,13 @@ def getDelayedCalls(self):
# IReactorTime.getDelayedCalls
pass

def seconds(self):
def seconds(self) -> float:
# IReactorTime.seconds
pass

def callInThread(self, callable: Callable[..., Any], *args, **kwargs):
def callInThread(
self, callable: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs
) -> None:
# IReactorInThreads.callInThread
pass

Expand Down
19 changes: 15 additions & 4 deletions src/twisted/internet/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from collections.abc import Sequence
from io import BytesIO
from socket import AF_INET, AF_INET6
from typing import Any, Callable
from typing import Callable

from zope.interface import implementedBy, implementer
from zope.interface.verify import verifyClass

from typing_extensions import ParamSpec

from twisted.internet import address, error, protocol, task
from twisted.internet.abstract import _dataMustBeBytes, isIPv6Address
from twisted.internet.address import IPv4Address, IPv6Address, UNIXAddress
Expand Down Expand Up @@ -56,6 +58,8 @@
"EventLoggingObserver",
]

_P = ParamSpec("_P")


class AccumulatingProtocol(protocol.Protocol):
"""
Expand Down Expand Up @@ -547,8 +551,13 @@ def fireSystemEvent(self, eventType):
raise NotImplementedError()

def addSystemEventTrigger(
self, phase: str, eventType: str, callable: Callable[..., Any], *args, **kw
):
self,
phase: str,
eventType: str,
callable: Callable[_P, object],
*args: _P.args,
**kw: _P.kwargs,
) -> None:
"""
Fake L{IReactorCore.run}.
Keep track of trigger by appending it to
Expand All @@ -564,7 +573,9 @@ def removeSystemEventTrigger(self, triggerID):
"""
raise NotImplementedError()

def callWhenRunning(self, callable: Callable[..., Any], *args, **kw):
def callWhenRunning(
self, callable: Callable[_P, object], *args: _P.args, **kw: _P.kwargs
) -> None:
"""
Fake L{IReactorCore.callWhenRunning}.
Keeps a list of invocations to make in C{self.whenRunningHooks}.
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/mail/relaymanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def sentMail(self, code, resp, numOk, addresses, log):
del self.messages[0]
del self.names[0]

def connectionLost(self, reason: Failure = connectionDone):
def connectionLost(self, reason: Failure = connectionDone) -> None:
"""
called when connection is broken
Expand Down
1 change: 1 addition & 0 deletions src/twisted/newsfragments/11922.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disallow incomplete defs in mypy config
6 changes: 4 additions & 2 deletions src/twisted/test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
import time
from importlib import invalidate_caches as invalidateImportCaches
from typing import Callable
from typing import Callable, TypeVar

from zope.interface import Interface

Expand All @@ -23,6 +23,8 @@
from twisted.python.log import addObserver, removeObserver, textFromEventDict
from twisted.trial import unittest

_T = TypeVar("_T")


class ITestPlugin(Interface):
"""
Expand Down Expand Up @@ -96,7 +98,7 @@ def _clearCache(self):
"""
self.package.child("dropin.cache").remove()

def _withCacheness(meth: Callable):
def _withCacheness(meth: Callable[[_T], object]) -> Callable[[_T], None]:
"""
This is a paranoid test wrapper, that calls C{meth} 2 times, clear the
cache, and calls it 2 other times. It's supposed to ensure that the
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def __init__(self, channel, queued=_QUEUED_SENTINEL):

self.requestHeaders: Headers = Headers()
self.received_cookies = {}
self.responseHeaders = Headers()
self.responseHeaders: Headers = Headers()
self.cookies = [] # outgoing cookies
self.transport = self.channel.transport

Expand Down
6 changes: 4 additions & 2 deletions src/twisted/web/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ParentRedirectTests(SynchronousTestCase):
Test L{ParentRedirect}.
"""

def doLocationTest(self, requestPath: bytes):
def doLocationTest(self, requestPath: bytes) -> bytes:
"""
Render a response to a request with path *requestPath*
Expand All @@ -85,7 +85,9 @@ def doLocationTest(self, requestPath: bytes):
resource = ParentRedirect()
resource.render(request)

[location] = request.responseHeaders.getRawHeaders(b"Location")
headers = request.responseHeaders.getRawHeaders(b"Location")
assert headers is not None
[location] = headers
return location

def test_locationRoot(self):
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/words/im/basesupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self, account, chatui, logonDeferred):
def connectionMade(self):
self._protoBase.connectionMade(self)

def connectionLost(self, reason: Failure = connectionDone):
def connectionLost(self, reason: Failure = connectionDone) -> None:
self.account._clientLost(self, reason)
self.unregisterAsAccountClient()
return self._protoBase.connectionLost(self, reason) # type: ignore[arg-type]
Expand Down

0 comments on commit e7bd373

Please sign in to comment.