Skip to content

Commit

Permalink
Remove _get_async_param from compat
Browse files Browse the repository at this point in the history
`_get_async_param()` was added in Twisted 18.9.0 to mitigate an issue
where Python 3.7 introduced the `async` keyword which caused problems
where variables were trying to use that name also.

There is no need to maintain backwards compatability for Python 3.6 and
older, which may have been using the 'async' kwargs argument. Twisted
today only supports Python 3.8+ and we can rely on the 'isAsync'
function argument instead.

Supplying 'async' argument to `_get_async_param` has been deprecated
since 18.9.0 and is now removed with this commit.
  • Loading branch information
gudnimg committed Apr 20, 2024
1 parent 36832d4 commit d9fd79b
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 71 deletions.
7 changes: 2 additions & 5 deletions src/twisted/conch/manhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from twisted.conch import recvline
from twisted.internet import defer
from twisted.python.compat import _get_async_param
from twisted.python.htmlizer import TokenPrinter
from twisted.python.monkey import MonkeyPatcher

Expand Down Expand Up @@ -161,8 +160,7 @@ def _ebDisplayDeferred(self, failure, k, obj):
del self._pendingDeferreds[id(obj)]
return failure

def write(self, data, isAsync=None, **kwargs):
isAsync = _get_async_param(isAsync, **kwargs)
def write(self, data, isAsync=None):
self.handler.addOutput(data, isAsync)


Expand Down Expand Up @@ -239,8 +237,7 @@ def _needsNewline(self):
w = self.terminal.lastWrite
return not w.endswith(b"\n") and not w.endswith(b"\x1bE")

def addOutput(self, data, isAsync=None, **kwargs):
isAsync = _get_async_param(isAsync, **kwargs)
def addOutput(self, data, isAsync=None):
if isAsync:
self.terminal.eraseLine()
self.terminal.cursorBackward(len(self.lineBuffer) + len(self.ps[self.pn]))
Expand Down
4 changes: 1 addition & 3 deletions src/twisted/mail/imap4.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
from twisted.protocols import basic, policies
from twisted.python import log, text
from twisted.python.compat import (
_get_async_param,
_matchingString,
iterbytes,
nativeString,
Expand Down Expand Up @@ -1076,8 +1075,7 @@ def sendPositiveResponse(self, tag=None, message=b""):
def sendNegativeResponse(self, tag=None, message=b""):
self._respond(b"NO", tag, message)

def sendUntaggedResponse(self, message, isAsync=None, **kwargs):
isAsync = _get_async_param(isAsync, **kwargs)
def sendUntaggedResponse(self, message, isAsync=None):
if not isAsync or (self.blocked is None):
self._respond(message, None, None)
else:
Expand Down
1 change: 1 addition & 0 deletions src/twisted/newsfragments/12130.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.conch.manhole.ManholeInterpreter.write, twisted.conch.manhole.ManholeInterpreter.addOutput, twisted.mail.imap4.IMAP4Server.sendUntaggedResponse `async` argument, deprecated since 18.9.0, has been removed.
31 changes: 0 additions & 31 deletions src/twisted/python/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import platform
import socket
import urllib.parse as urllib_parse
import warnings
from collections.abc import Sequence
from functools import reduce
from html import escape
Expand Down Expand Up @@ -497,35 +496,6 @@ def _constructMethod(cls, name, self):
return _MethodType(func, self)


def _get_async_param(isAsync=None, **kwargs):
"""
Provide a backwards-compatible way to get async param value that does not
cause a syntax error under Python 3.7.
@param isAsync: isAsync param value (should default to None)
@type isAsync: L{bool}
@param kwargs: keyword arguments of the caller (only async is allowed)
@type kwargs: L{dict}
@raise TypeError: Both isAsync and async specified.
@return: Final isAsync param value
@rtype: L{bool}
"""
if "async" in kwargs:
warnings.warn(
"'async' keyword argument is deprecated, please use isAsync",
DeprecationWarning,
stacklevel=2,
)
if isAsync is None and "async" in kwargs:
isAsync = kwargs.pop("async")
if kwargs:
raise TypeError
return bool(isAsync)


def _pypy3BlockingHack():
"""
Work around U{https://foss.heptapod.net/pypy/pypy/-/issues/3051}
Expand Down Expand Up @@ -645,6 +615,5 @@ def fromFDWithoutModifyingFlags(fd, family, type, proto=None):
"intern",
"unichr",
"raw_input",
"_get_async_param",
"Sequence",
]
32 changes: 0 additions & 32 deletions src/twisted/test/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from twisted.python.compat import (
_PYPY,
_get_async_param,
bytesEnviron,
cmp,
comparable,
Expand Down Expand Up @@ -547,34 +546,3 @@ def test_alwaysBytes(self):
types.add(type(val))

self.assertEqual(list(types), [bytes])


class GetAsyncParamTests(SynchronousTestCase):
"""
Tests for L{twisted.python.compat._get_async_param}
"""

def test_get_async_param(self):
"""
L{twisted.python.compat._get_async_param} uses isAsync by default,
or deprecated async keyword argument if isAsync is None.
"""
self.assertEqual(_get_async_param(isAsync=False), False)
self.assertEqual(_get_async_param(isAsync=True), True)
self.assertEqual(_get_async_param(isAsync=None, **{"async": False}), False)
self.assertEqual(_get_async_param(isAsync=None, **{"async": True}), True)
self.assertRaises(TypeError, _get_async_param, False, {"async": False})

def test_get_async_param_deprecation(self):
"""
L{twisted.python.compat._get_async_param} raises a deprecation
warning if async keyword argument is passed.
"""
self.assertEqual(_get_async_param(isAsync=None, **{"async": False}), False)
currentWarnings = self.flushWarnings(
offendingFunctions=[self.test_get_async_param_deprecation]
)
self.assertEqual(
currentWarnings[0]["message"],
"'async' keyword argument is deprecated, please use isAsync",
)

0 comments on commit d9fd79b

Please sign in to comment.