Skip to content

Commit

Permalink
Add mypy and start adding type hints
Browse files Browse the repository at this point in the history
As a first step, go back to definining exposed and user_facing in all
app controllers rather than using importlib in the server to set
defaults. This is more repetitive, but sidesteps some typing
complications. Although importlib was valued at the time it was
implemented for cutting down on repetition, I'm more interested in
having things be more obvious and less magical.
  • Loading branch information
lovett committed Oct 17, 2019
1 parent 9cbbe87 commit 30b38a6
Show file tree
Hide file tree
Showing 39 changed files with 76 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ venv
cache
node_modules
.pytest_cache
.mypy_cache
2 changes: 2 additions & 0 deletions apps/applog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "App Log"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
1 change: 1 addition & 0 deletions apps/azure/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Controller:
"""Dispatch application requests based on HTTP verb."""

exposed = True
user_facing = False

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions apps/bookmarklets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Bookmarklets"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/bookmarks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Bookmarks"
exposed = True
user_facing = True

@staticmethod
def check_wayback_availability(url):
Expand Down
2 changes: 2 additions & 0 deletions apps/bounce/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Bounce"
exposed = True
user_facing = True

common_names = {
"dev",
Expand Down
2 changes: 2 additions & 0 deletions apps/calls/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Calls"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/captures/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Captures"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/countries/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class Controller:
"""Dispatch application requests based on HTTP verb."""

exposed = True

user_facing = False

cache_key = "countries"
Expand Down
2 changes: 2 additions & 0 deletions apps/coverage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Coverage"
exposed = True
user_facing = True

@staticmethod
def GET():
Expand Down
2 changes: 1 addition & 1 deletion apps/coverage/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def tearDownClass(cls):
def test_allow(self):
"""Verify the controller's supported HTTP methods"""
response = self.request("/", method="HEAD")
self.assertAllowedMethods(response, ("GET",))
self.assert_allowed(response, ("GET",))

@mock.patch("cherrypy.engine.publish")
def test_redirect(self, publish_mock):
Expand Down
1 change: 1 addition & 0 deletions apps/geodb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Controller:
"""Dispatch application requests based on HTTP verb."""

exposed = True
user_facing = False

@staticmethod
Expand Down
4 changes: 3 additions & 1 deletion apps/grids/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Grids"
exposed = True
user_facing = True

@cherrypy.tools.negotiable()
def GET(self, *_args, **kwargs):
Expand Down Expand Up @@ -72,7 +74,7 @@ def GET(self, *_args, **kwargs):
"html": ("grids.jinja.html", {
"headers": headers,
"name": name,
"names": [key for key in grids.keys()],
"names": grids.keys(),
"options": options,
"rows": rows
})
Expand Down
2 changes: 2 additions & 0 deletions apps/headers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Headers"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/headlines/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Headlines"
exposed = True
user_facing = True

cache_key = "headlines"

Expand Down
2 changes: 2 additions & 0 deletions apps/homepage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Homepage"
exposed = True
user_facing = True

@decorators.log_runtime
def catalog_apps(self, apps):
Expand Down
2 changes: 2 additions & 0 deletions apps/htmlhead/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "HTML Head"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/ip/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "IP"
exposed = True
user_facing = True

cache_key = "ip:external"

Expand Down
1 change: 1 addition & 0 deletions apps/jenkins/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
class Controller:
"""Dispatch application requests based on HTTP verb."""

exposed = True
user_facing = False

@cherrypy.tools.json_in()
Expand Down
2 changes: 2 additions & 0 deletions apps/later/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Later"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/logindex/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Controller:
@staticmethod
def parse_log_date(val):
"""Convert a date string in either date or filename format
exposed = True
user_facing = True
to a datetime.
Date format is YYYY-mm-dd. Filename format is the same, but with
Expand Down
4 changes: 3 additions & 1 deletion apps/maintenance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

user_facing = False
exposed = True

def POST(self, group):
@staticmethod
def POST(group=None):
"""
Schedule maintenance operations.
"""
Expand Down
2 changes: 1 addition & 1 deletion apps/maintenance/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_group_required(self):
"/",
method="POST"
)
self.assertEqual(response.code, 404)
self.assertEqual(response.code, 400)

def test_group_valid(self):
"""The group parameter must be valid."""
Expand Down
1 change: 1 addition & 0 deletions apps/notification/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

user_facing = False
exposed = True

@staticmethod
@cherrypy.tools.capture()
Expand Down
2 changes: 2 additions & 0 deletions apps/phone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Phone"
exposed = True
user_facing = True

messages = {
"invalid": "The number provided was invalid",
Expand Down
2 changes: 1 addition & 1 deletion apps/redirect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Redirect"

exposed = True
user_facing = False

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions apps/registry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Registry"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/reminder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Reminder"
exposed = True
user_facing = True

registry_key = "reminder:template"

Expand Down
1 change: 1 addition & 0 deletions apps/shared/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

user_facing = False
exposed = True

@staticmethod
def GET(*_args, **_kwargs):
Expand Down
5 changes: 4 additions & 1 deletion apps/speak/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Speak"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
def GET(self, *_args, **_kwargs):
def GET(*_args, **_kwargs):
"""Present an interface for on-demand and scheduled muting of the
application.
Expand Down
2 changes: 2 additions & 0 deletions apps/startpage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Startpage"
exposed = True
user_facing = True

default_page_name = "default"

Expand Down
2 changes: 2 additions & 0 deletions apps/transform/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Transform"
exposed = True
user_facing = True

transforms = {}

Expand Down
2 changes: 2 additions & 0 deletions apps/visitors/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Controller:
"""

name = "Visitors"
exposed = True
user_facing = True

@cherrypy.tools.negotiable()
def GET(self, *_args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions apps/wakeup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Wakeup"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
2 changes: 2 additions & 0 deletions apps/weather/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Weather"
exposed = True
user_facing = True

@cherrypy.tools.negotiable()
def GET(self, *_args, **_kwargs):
Expand Down
2 changes: 2 additions & 0 deletions apps/whois/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Controller:
"""Dispatch application requests based on HTTP verb."""

name = "Whois"
exposed = True
user_facing = True

@staticmethod
@cherrypy.tools.negotiable()
Expand Down
15 changes: 2 additions & 13 deletions medley.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

# pylint: disable=too-many-statements
@plugins.decorators.log_runtime
def setup():
def setup() -> None:
"""Configure and start the application server
The application server is the backbone that individual
Expand Down Expand Up @@ -128,17 +128,6 @@ def setup():

app_module = importlib.import_module(f"apps.{app}.main")

# Treat all controllers as exposed by default.
# This is a Cherrypy-ism.
if not hasattr(app_module.Controller, "exposed"):
app_module.Controller.exposed = True

# Treat all controllers as user-facing by default. This is a
# Medley-ism. Service apps should override this attribute
# locally.
if not hasattr(app_module.Controller, "user_facing"):
app_module.Controller.user_facing = True

app_config = {
"/": {
"request.dispatch": cherrypy.dispatch.MethodDispatcher()
Expand Down Expand Up @@ -186,7 +175,7 @@ def setup():
})

cherrypy.tree.mount(
app_module.Controller(),
app_module.Controller(), # type: ignore
app_path,
app_config
)
Expand Down
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mypy]
python_version = 3.7
warn_return_any = True
warn_unused_configs = True
disallow_untyped_defs = True
ignore_missing_imports = True
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage==4.5.4
flake8==3.7.8
flake8-comprehensions==2.2.0
mock==3.0.5
mypy==0.740
pylint==2.4.2
pytest-cov==2.8.1
pytest-timeout==1.3.3
Expand Down

0 comments on commit 30b38a6

Please sign in to comment.