Skip to content

Commit

Permalink
Add type hints for twisted.application.twist._twist
Browse files Browse the repository at this point in the history
  • Loading branch information
wsanchez committed Sep 30, 2020
1 parent 406861a commit bed789a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ check_untyped_defs = False
[mypy-twisted.application.test.test_service]
allow_untyped_defs = True

[mypy-twisted.application.twist._twist]
allow_untyped_defs = True

[mypy-twisted.application.twist.test.test_options]
allow_untyped_defs = True
check_untyped_defs = False
Expand Down
17 changes: 13 additions & 4 deletions src/twisted/application/twist/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

from sys import stdout, stderr
from textwrap import dedent
from typing import Callable, Iterable, List, Mapping, NoReturn, Optional, Tuple, cast
from typing import (
Callable,
Iterable,
Mapping,
NoReturn,
Optional,
Sequence,
Tuple,
cast,
)

from twisted.copyright import version
from twisted.internet.interfaces import IReactorCore
Expand Down Expand Up @@ -152,7 +161,7 @@ def selectDefaultLogObserver(self) -> None:
self["fileLogObserverFactory"] = jsonFileLogObserver
self["logFormat"] = "json"

def parseOptions(self, options: Optional[List[str]] = None) -> None:
def parseOptions(self, options: Optional[Sequence[str]] = None) -> None:
self.selectDefaultLogObserver()

Options.parseOptions(self, options=options)
Expand All @@ -161,14 +170,14 @@ def parseOptions(self, options: Optional[List[str]] = None) -> None:
self["reactor"] = self.installReactor(self["reactorName"])

@property
def plugins(self) -> Mapping[str, IServiceMaker]:
def plugins(self) -> Mapping[Optional[str], IServiceMaker]:
if "plugins" not in self:
plugins = {}
for plugin in getPlugins(IServiceMaker):
plugins[plugin.tapname] = plugin
self["plugins"] = plugins

return cast(Mapping[str, IServiceMaker], self["plugins"])
return cast(Mapping[Optional[str], IServiceMaker], self["plugins"])

@property
def subCommands(
Expand Down
32 changes: 11 additions & 21 deletions src/twisted/application/twist/_twist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"""

import sys
from typing import Sequence

from twisted.application.app import _exitWithSignal
from twisted.internet.interfaces import _ISupportsExitSignalCapturing, IReactorCore
from twisted.python.usage import Options, UsageError

from twisted.python.usage import UsageError
from ..service import Application, IService
from ..runner._exit import exit, ExitStatus
from ..runner._runner import Runner
from ..service import Application, IService, IServiceMaker
from ._options import TwistOptions
from twisted.application.app import _exitWithSignal
from twisted.internet.interfaces import _ISupportsExitSignalCapturing


class Twist:
Expand All @@ -23,15 +25,12 @@ class Twist:
"""

@staticmethod
def options(argv):
def options(argv: Sequence[str]) -> TwistOptions:
"""
Parse command line options.
@param argv: Command line arguments.
@type argv: L{list}
@return: The parsed options.
@rtype: L{TwistOptions}
"""
options = TwistOptions()

Expand All @@ -43,19 +42,14 @@ def options(argv):
return options

@staticmethod
def service(plugin, options):
def service(plugin: IServiceMaker, options: Options) -> IService:
"""
Create the application service.
@param plugin: The name of the plugin that implements the service
application to run.
@type plugin: L{str}
@param options: Options to pass to the application.
@type options: L{twisted.python.usage.Options}
@return: The created application service.
@rtype: L{IService}
"""
service = plugin.makeService(options)
application = Application(plugin.tapname)
Expand All @@ -64,29 +58,25 @@ def service(plugin, options):
return IService(application)

@staticmethod
def startService(reactor, service):
def startService(reactor: IReactorCore, service: IService) -> None:
"""
Start the application service.
@param reactor: The reactor to run the service with.
@type reactor: L{twisted.internet.interfaces.IReactorCore}
@param service: The application service to run.
@type service: L{IService}
"""
service.startService()

# Ask the reactor to stop the service before shutting down
reactor.addSystemEventTrigger("before", "shutdown", service.stopService)

@staticmethod
def run(twistOptions):
def run(twistOptions: TwistOptions) -> None:
"""
Run the application service.
@param twistOptions: Command line options to convert to runner
arguments.
@type twistOptions: L{TwistOptions}
"""
runner = Runner(
reactor=twistOptions["reactor"],
Expand All @@ -101,7 +91,7 @@ def run(twistOptions):
_exitWithSignal(reactor._exitSignal)

@classmethod
def main(cls, argv=sys.argv):
def main(cls, argv: Sequence[str] = sys.argv) -> None:
"""
Executable entry point for L{Twist}.
Processes options and run a twisted reactor with a service.
Expand Down
6 changes: 3 additions & 3 deletions src/twisted/python/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ class Options(dict):
or doc/core/howto/options.xhtml in your Twisted directory.
"""

subCommand = None
defaultSubCommand = None
parent = None
subCommand = None # type: Optional[str]
defaultSubCommand = None # type: Optional[str]
parent = None # type: Optional[Options]
completionData = None
_shellCompFile = sys.stdout # file to use if shell completion is requested

Expand Down

0 comments on commit bed789a

Please sign in to comment.