Skip to content

Commit

Permalink
use py3.6 style annotations for attr.s classes
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Mar 19, 2021
1 parent ef3e0a8 commit f9f0cab
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/twisted/internet/_sslverify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ def _verifyCallback(conn, cert, errno, depth, preverify_ok):


@implementer(ICipher)
@attr.s(frozen=True)
@attr.s(frozen=True, auto_attribs=True)
class OpenSSLCipher:
"""
A representation of an OpenSSL cipher.
Expand All @@ -1716,7 +1716,7 @@ class OpenSSLCipher:
@type fullName: L{unicode}
"""

fullName = attr.ib()
fullName: str


@lru_cache(maxsize=32)
Expand Down
35 changes: 20 additions & 15 deletions src/twisted/internet/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import attr
import os
from typing import Optional
from typing import Optional, Union
from warnings import warn

from typing_extensions import Literal
from zope.interface import implementer
from twisted.internet.interfaces import IAddress
from twisted.python.filepath import _asFilesystemBytes
Expand All @@ -19,7 +20,7 @@


@implementer(IAddress)
@attr.s(hash=True)
@attr.s(hash=True, auto_attribs=True)
class IPv4Address:
"""
An L{IPv4Address} represents the address of an IPv4 socket endpoint.
Expand All @@ -35,13 +36,15 @@ class IPv4Address:
@type port: C{int}
"""

type = attr.ib(validator=attr.validators.in_(["TCP", "UDP"]))
host = attr.ib()
port = attr.ib()
type: Union[Literal["TCP"], Literal["UDP"]] = attr.ib(
validator=attr.validators.in_(["TCP", "UDP"])
)
host: str
port: int


@implementer(IAddress)
@attr.s(hash=True)
@attr.s(hash=True, auto_attribs=True)
class IPv6Address:
"""
An L{IPv6Address} represents the address of an IPv6 socket endpoint.
Expand All @@ -65,11 +68,13 @@ class IPv6Address:
@type scopeID: L{int} or L{str}
"""

type = attr.ib(validator=attr.validators.in_(["TCP", "UDP"]))
host = attr.ib()
port = attr.ib()
flowInfo = attr.ib(default=0)
scopeID = attr.ib(default=0)
type: Union[Literal["TCP"], Literal["UDP"]] = attr.ib(
validator=attr.validators.in_(["TCP", "UDP"])
)
host: str
port: int
flowInfo: int = 0
scopeID: Union[str, int] = 0


@implementer(IAddress)
Expand All @@ -79,7 +84,7 @@ class _ProcessAddress:
"""


@attr.s(hash=True)
@attr.s(hash=True, auto_attribs=True)
@implementer(IAddress)
class HostnameAddress:
"""
Expand All @@ -92,11 +97,11 @@ class HostnameAddress:
@type port: L{int}
"""

hostname = attr.ib()
port = attr.ib()
hostname: bytes
port: int


@attr.s(hash=False, repr=False, eq=False)
@attr.s(hash=False, repr=False, eq=False, auto_attribs=True)
@implementer(IAddress)
class UNIXAddress:
"""
Expand Down
6 changes: 3 additions & 3 deletions src/twisted/internet/defer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ def returnValue(val: object) -> NoReturn:
raise _DefGen_Return(val)


@attr.s
@attr.s(auto_attribs=True)
class _CancellationStatus:
"""
Cancellation status of an L{inlineCallbacks} invocation.
Expand All @@ -1532,8 +1532,8 @@ class _CancellationStatus:
L{_inlineCallbacks} must fill out before returning)
"""

deferred: Deferred[object] = attr.ib()
waitingOn: Optional[Deferred[object]] = attr.ib(default=None)
deferred: Deferred[object]
waitingOn: Optional[Deferred[object]] = None


@_extraneous
Expand Down
26 changes: 16 additions & 10 deletions src/twisted/internet/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import sys
import os
import struct
from typing import Optional
from typing import Optional, Callable, List

import attr

from zope.interface import Interface, implementer
import typing_extensions

from twisted.logger import Logger
from twisted.logger import Logger, ILogObserver, LogEvent
from twisted.internet.interfaces import (
IHalfCloseableProtocol,
ITCPTransport,
Expand Down Expand Up @@ -934,8 +935,13 @@ def __exit__(excType, excValue, traceback):
"""


class _HasClose(typing_extensions.Protocol):
def close(self) -> object:
...


@implementer(_IFileDescriptorReservation)
@attr.s
@attr.s(auto_attribs=True)
class _FileDescriptorReservation:
"""
L{_IFileDescriptorReservation} implementation.
Expand All @@ -946,10 +952,10 @@ class _FileDescriptorReservation:
returns an object with a C{close} method.
"""

_log = Logger()
_log: typing_extensions.ClassVar[Logger] = Logger()

_fileFactory = attr.ib()
_fileDescriptor = attr.ib(init=False, default=None)
_fileFactory: Callable[[], _HasClose]
_fileDescriptor: Optional[_HasClose] = attr.ib(init=False, default=None)

def available(self):
"""
Expand Down Expand Up @@ -1085,7 +1091,7 @@ def __exit__(self, excType, excValue, traceback):
_ACCEPT_ERRORS = (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED)


@attr.s
@attr.s(auto_attribs=True)
class _BuffersLogs:
"""
A context manager that buffers any log events until after its
Expand All @@ -1099,9 +1105,9 @@ class _BuffersLogs:
@type _observer: L{twisted.logger.ILogObserver}.
"""

_namespace = attr.ib()
_observer = attr.ib()
_logs = attr.ib(default=attr.Factory(list))
_namespace: str
_observer: ILogObserver
_logs: List[LogEvent] = attr.ib(default=attr.Factory(list))

def __enter__(self):
"""
Expand Down
17 changes: 11 additions & 6 deletions src/twisted/internet/test/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
import socket

from functools import wraps
from typing import Optional, Sequence, Type
from typing import Optional, Sequence, Type, List, Callable
from unittest import skipIf

import attr

from typing_extensions import ClassVar
from zope.interface import Interface, implementer
from zope.interface.verify import verifyClass, verifyObject

Expand Down Expand Up @@ -964,7 +965,7 @@ def count():


@implementer(_IExhaustsFileDescriptors)
@attr.s
@attr.s(auto_attribs=True)
class _ExhaustsFileDescriptors:
"""
A class that triggers C{EMFILE} by creating as many file
Expand All @@ -977,10 +978,14 @@ class _ExhaustsFileDescriptors:
for passing to L{os.close}.
"""

_log = Logger()
_fileDescriptorFactory = attr.ib(default=lambda: os.dup(0), repr=False)
_close = attr.ib(default=os.close, repr=False)
_fileDescriptors = attr.ib(default=attr.Factory(list), init=False, repr=False)
_log: ClassVar[Logger] = Logger()
_fileDescriptorFactory: Callable[[], int] = attr.ib(
default=lambda: os.dup(0), repr=False
)
_close: Callable[[int], None] = attr.ib(default=os.close, repr=False)
_fileDescriptors: List[int] = attr.ib(
default=attr.Factory(list), init=False, repr=False
)

def exhaust(self):
"""
Expand Down
14 changes: 8 additions & 6 deletions src/twisted/runner/procmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""
Support for starting, monitoring, and restarting child process.
"""
from typing import List, Optional, Dict

import attr
import incremental

Expand All @@ -15,7 +17,7 @@
from twisted.logger import Logger


@attr.s(frozen=True)
@attr.s(frozen=True, auto_attribs=True)
class _Process:
"""
The parameters of a process to be restarted.
Expand All @@ -37,11 +39,11 @@ class _Process:
@type cwd: C{str}
"""

args = attr.ib()
uid = attr.ib(default=None)
gid = attr.ib(default=None)
env = attr.ib(default=attr.Factory(dict))
cwd = attr.ib(default=None)
args: List[str]
uid: Optional[int] = None
gid: Optional[int] = None
env: Dict[str, str] = attr.ib(default=attr.Factory(dict))
cwd: Optional[str] = None

@deprecate.deprecated(incremental.Version("Twisted", 18, 7, 0))
def toTuple(self):
Expand Down

0 comments on commit f9f0cab

Please sign in to comment.