Skip to content

Commit

Permalink
Enable disallow_untyped_defs and check_untyped_defs on more files in …
Browse files Browse the repository at this point in the history
…twisted.python.
  • Loading branch information
wsanchez committed Oct 2, 2020
1 parent 81c18d3 commit 82ebed6
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 162 deletions.
31 changes: 0 additions & 31 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,18 @@ warn_return_any = False
[mypy-twisted.protocols.test.test_basic]
allow_incomplete_defs = True

[mypy-twisted.python._appdirs]
allow_untyped_defs = True

[mypy-twisted.python._inotify]
allow_untyped_defs = True

[mypy-twisted.python._pydoctor]
allow_untyped_defs = True

[mypy-twisted.python._release]
allow_untyped_defs = True

[mypy-twisted.python._setup]
allow_untyped_defs = True

[mypy-twisted.python._shellcomp]
allow_untyped_defs = True

[mypy-twisted.python._textattributes]
allow_untyped_defs = True

[mypy-twisted.python._tzhelper]
allow_untyped_defs = True

[mypy-twisted.python.compat]
allow_untyped_defs = True

Expand Down Expand Up @@ -250,31 +238,12 @@ allow_untyped_defs = True
[mypy-twisted.python.roots]
allow_untyped_defs = True

[mypy-twisted.python.runtime]
allow_untyped_defs = True

[mypy-twisted.python.sendmsg]
allow_untyped_defs = True

[mypy-twisted.python.shortcut]
allow_untyped_defs = True

[mypy-twisted.python.syslog]
allow_untyped_defs = True

[mypy-twisted.python.systemd]
allow_untyped_defs = True

[mypy-twisted.python.test.modules_helpers]
allow_untyped_defs = True
check_untyped_defs = False

[mypy-twisted.python.test.pullpipe]
allow_untyped_defs = True

[mypy-twisted.python.test.test_appdirs]
allow_untyped_defs = True

[mypy-twisted.python.test.test_components]
allow_untyped_defs = True
check_untyped_defs = False
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/test/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def fakeRecvmsgUnsupportedAncillary(skt, *args, **kwargs):
data = b"some data"
ancillary = [(None, None, b"")]
flags = 0
return sendmsg.RecievedMessage(data, ancillary, flags)
return sendmsg.ReceivedMessage(data, ancillary, flags)

events = []
addObserver(events.append)
Expand Down
Empty file.
8 changes: 3 additions & 5 deletions src/twisted/python/_appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
Application data directory support.
"""


import appdirs
import inspect
from typing import cast

from twisted.python.compat import currentframe


def getDataDirectory(moduleName=None):
def getDataDirectory(moduleName: str = "") -> str:
"""
Get a data directory for the caller function, or C{moduleName} if given.
@param moduleName: The module name if you don't wish to have the caller's
module.
@type moduleName: L{str}
@returns: A directory for putting data in.
@rtype: L{str}
"""
if not moduleName:
caller = currentframe(1)
module = inspect.getmodule(caller)
assert module is not None
moduleName = module.__name__

return appdirs.user_data_dir(moduleName)
return cast(str, appdirs.user_data_dir(moduleName))
20 changes: 9 additions & 11 deletions src/twisted/python/_inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import ctypes
import ctypes.util
from typing import cast

from twisted.python.filepath import FilePath


class INotifyError(Exception):
Expand All @@ -19,37 +22,32 @@ class INotifyError(Exception):
"""


def init():
def init() -> int:
"""
Create an inotify instance and return the associated file descriptor.
"""
fd = libc.inotify_init()
fd = cast(int, libc.inotify_init())
if fd < 0:
raise INotifyError("INotify initialization error.")
return fd


def add(fd, path, mask):
def add(fd: int, path: FilePath, mask: int) -> int:
"""
Add a watch for the given path to the inotify file descriptor, and return
the watch descriptor.
@param fd: The file descriptor returned by C{libc.inotify_init}.
@type fd: L{int}
@param path: The path to watch via inotify.
@type path: L{twisted.python.filepath.FilePath}
@param mask: Bitmask specifying the events that inotify should monitor.
@type mask: L{int}
"""
wd = libc.inotify_add_watch(fd, path.asBytesMode().path, mask)
wd = cast(int, libc.inotify_add_watch(fd, path.asBytesMode().path, mask))
if wd < 0:
raise INotifyError("Failed to add watch on '%r' - (%r)" % (path, wd))
return wd


def remove(fd, wd):
def remove(fd: int, wd: int) -> None:
"""
Remove the given watch descriptor from the inotify file descriptor.
"""
Expand All @@ -76,7 +74,7 @@ def remove(fd, wd):
libc.inotify_rm_watch(fd, wd)


def initializeModule(libc):
def initializeModule(libc: ctypes.CDLL) -> None:
"""
Initialize the module, checking if the expected APIs exist and setting the
argtypes and restype for C{inotify_init}, C{inotify_add_watch}, and
Expand Down
33 changes: 16 additions & 17 deletions src/twisted/python/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import platform
import re
import sys
from typing import Any, Dict, cast
from typing import Any, Dict, List, Tuple, cast

from distutils.command import build_ext
from distutils.errors import CompileError
Expand Down Expand Up @@ -149,7 +149,7 @@ class ConditionalExtension(Extension):
things about the platform.
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args: object, **kwargs: object) -> None:
self.condition = kwargs.pop("condition", lambda builder: True)
Extension.__init__(self, *args, **kwargs)

Expand All @@ -173,15 +173,13 @@ def __init__(self, *args, **kwargs):
]


def _longDescriptionArgsFromReadme(readme):
def _longDescriptionArgsFromReadme(readme: str) -> Dict[str, str]:
"""
Generate a PyPI long description from the readme.
@param readme: Path to the readme reStructuredText file.
@type readme: C{str}
@return: Keyword arguments to be passed to C{setuptools.setup()}.
@rtype: C{str}
"""
with io.open(readme, encoding="utf-8") as f:
readmeRst = f.read()
Expand All @@ -202,20 +200,19 @@ def _longDescriptionArgsFromReadme(readme):
}


def getSetupArgs(extensions=_EXTENSIONS, readme="README.rst"):
def getSetupArgs(
extensions: List[ConditionalExtension] = _EXTENSIONS,
readme: str = "README.rst",
) -> Dict[str, Any]:
"""
Generate arguments for C{setuptools.setup()}
@param extensions: C extension modules to maybe build. This argument is to
be used for testing.
@type extensions: C{list} of C{ConditionalExtension}
@param readme: Path to the readme reStructuredText file. This argument is
to be used for testing.
@type readme: C{str}
@return: The keyword arguments to be used by the setup method.
@rtype: L{dict}
"""
arguments = STATIC_PACKAGE_METADATA.copy()
if readme:
Expand Down Expand Up @@ -273,7 +270,9 @@ class BuildPy3(build_py):
ported to Python 3.
"""

def find_package_modules(self, package, package_dir):
def find_package_modules(
self, package: str, package_dir: str
) -> List[Tuple[str, str, str]]:
modules = [
module
for module in build_py.find_package_modules(self, package, package_dir)
Expand All @@ -285,13 +284,13 @@ def find_package_modules(self, package, package_dir):
# Helpers and distutil tweaks


class build_ext_twisted(build_ext.build_ext): # type: ignore[name-defined] # noqa
class build_ext_twisted(build_ext.build_ext): # type: ignore[name-defined]
"""
Allow subclasses to easily detect and customize Extensions to
build at install-time.
"""

def prepare_extensions(self):
def prepare_extensions(self) -> None:
"""
Prepare the C{self.extensions} attribute (used by
L{build_ext.build_ext}) by checking which extensions in
Expand Down Expand Up @@ -319,21 +318,21 @@ def prepare_extensions(self):
for ext in self.extensions:
ext.define_macros.extend(self.define_macros)

def build_extensions(self):
def build_extensions(self) -> None:
"""
Check to see which extension modules to build and then build them.
"""
self.prepare_extensions()
build_ext.build_ext.build_extensions(self) # type: ignore[attr-defined]

def _remove_conftest(self):
def _remove_conftest(self) -> None:
for filename in ("conftest.c", "conftest.o", "conftest.obj"):
try:
os.unlink(filename)
except EnvironmentError:
pass

def _compile_helper(self, content):
def _compile_helper(self, content: str) -> bool:
conftest = open("conftest.c", "w")
try:
with conftest:
Expand All @@ -347,7 +346,7 @@ def _compile_helper(self, content):
finally:
self._remove_conftest()

def _check_header(self, header_name):
def _check_header(self, header_name: str) -> bool:
"""
Check if the given header can be included by trying to compile a file
that contains only an #include line.
Expand Down
Loading

0 comments on commit 82ebed6

Please sign in to comment.