Skip to content

Instantly share code, notes, and snippets.

@mthuurne
Last active December 7, 2020 19:40
Show Gist options
  • Save mthuurne/d838bdc7453b2d039ace2a2b7bd5af13 to your computer and use it in GitHub Desktop.
Save mthuurne/d838bdc7453b2d039ace2a2b7bd5af13 to your computer and use it in GitHub Desktop.
Suggested fix for plugins annotation
diff --git a/src/twisted/application/twist/_options.py b/src/twisted/application/twist/_options.py
index d19395d78b..f0b9e48888 100644
--- a/src/twisted/application/twist/_options.py
+++ b/src/twisted/application/twist/_options.py
@@ -170,21 +170,21 @@ class TwistOptions(Options):
self["reactor"] = self.installReactor(self["reactorName"])
@property
- def plugins(self) -> Mapping[Optional[str], IServiceMaker]:
+ def plugins(self) -> Mapping[str, IServiceMaker]:
if "plugins" not in self:
plugins = {}
for plugin in getPlugins(IServiceMaker):
plugins[plugin.tapname] = plugin
self["plugins"] = plugins
- return cast(Mapping[Optional[str], IServiceMaker], self["plugins"])
+ return cast(Mapping[str, IServiceMaker], self["plugins"])
@property
def subCommands(
self,
) -> Iterable[Tuple[str, None, Callable[[IServiceMaker], Options], str]]:
plugins = self.plugins
- for name in sorted(plugins, key=lambda x: (x is None, x)):
+ for name in sorted(plugins):
plugin = plugins[name]
# Don't pass plugin.options along in order to avoid resolving the
diff --git a/src/twisted/application/twist/_twist.py b/src/twisted/application/twist/_twist.py
index 6d54af46af..ea96999a67 100644
--- a/src/twisted/application/twist/_twist.py
+++ b/src/twisted/application/twist/_twist.py
@@ -102,8 +102,12 @@ class Twist:
options = cls.options(argv)
reactor = options["reactor"]
+ # If subCommand is None, TwistOptions.parseOptions() raises UsageError
+ # and Twist.options() will exit the runner, so we'll never get here.
+ subCommand = options.subCommand
+ assert subCommand is not None
service = cls.service(
- plugin=options.plugins[options.subCommand],
+ plugin=options.plugins[subCommand],
options=options.subOptions,
)
diff --git a/src/twisted/application/twist/test/test_twist.py b/src/twisted/application/twist/test/test_twist.py
index c503bd913f..19abae09f0 100644
--- a/src/twisted/application/twist/test/test_twist.py
+++ b/src/twisted/application/twist/test/test_twist.py
@@ -108,6 +108,7 @@ class TwistTests(twisted.trial.unittest.TestCase):
options = Twist.options(["twist", "web"])
reactor = options["reactor"]
+ assert options.subCommand is not None
service = Twist.service(
plugin=options.plugins[options.subCommand],
options=options.subOptions,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment