Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from zopefoundation/patrick-annotation-fix
Browse files Browse the repository at this point in the history
Make function annotations in route handlers work
  • Loading branch information
Jim Fulton committed Apr 6, 2014
2 parents 4f73b56 + b6fe77b commit f1eeb9a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bobo/src/bobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

_json_content_type = re.compile('application/json;?').match

getargspec = inspect.getargspec if six.PY2 else inspect.getfullargspec

class Application:
"""Create a WSGI application.
Expand Down Expand Up @@ -1172,7 +1174,7 @@ def handle(*args, **route):
return handle

def _make_caller(obj, paramsattr):
spec = inspect.getargspec(obj)
spec = getargspec(obj)
nargs = nrequired = len(spec.args)
if spec.defaults:
nrequired -= len(spec.defaults)
Expand Down
27 changes: 27 additions & 0 deletions bobodoctestumentation/src/bobodoctestumentation/annotations.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
====================
Function Annotations
====================

There was a bug that would cause things to break when using Python 3
function annotations in a route handler. In the future, we could use
annotations to perform automatic type conversion, but for now we're just
demonstrating that simply using annotations no longer breaks things.

.. code-block:: python

import bobo
import bobo.testmodule1
import webtest

@bobo.get("/foo/:bar/:baz")
def foo(bobo_request, bar:"bar", baz:"baz"):
return "OK"

bobo.testmodule1.foo = foo
app = webtest.TestApp(
bobo.Application(bobo_resources='bobo.testmodule1'))

We get back the exepcted result when making a request to this route:

>>> app.get("/foo/1/2")
<200 OK text/html body=b'OK'>
10 changes: 9 additions & 1 deletion bobodoctestumentation/src/bobodoctestumentation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from zope.testing import doctest, setupstack, renormalizing
import bobo
import manuel.capture
import manuel.codeblock
import manuel.doctest
import manuel.testing
import re
Expand Down Expand Up @@ -78,7 +79,7 @@ def test_suite():
(re.compile("u('.*?')"), r"\1"),
(re.compile('u(".*?")'), r"\1"),
])
return unittest.TestSuite((
suite = unittest.TestSuite((
manuel.testing.TestSuite(
manuel.doctest.Manuel(optionflags=options,
checker=unicode_literal_normalizer) +
Expand All @@ -101,3 +102,10 @@ def test_suite():
])
),
))
if not six.PY2:
suite.addTest(manuel.testing.TestSuite(
manuel.doctest.Manuel() + manuel.codeblock.Manuel(),
"annotations.test",
setUp=setUp,
tearDown=setupstack.tearDown))
return suite

0 comments on commit f1eeb9a

Please sign in to comment.