3

This code that's in the doctest works when run by itself, but in this doctest it fails in 10 places. I can't figure out why it does though. The following is the entire module:

class requireparams(object):
    """
    >>> @requireparams(['name', 'pass', 'code'])
    >>> def complex_function(params):
    >>>     print(params['name'])
    >>>     print(params['pass'])
    >>>     print(params['code'])
    >>> 
    >>> params = {
    >>>     'name': 'John Doe',
    >>>     'pass': 'OpenSesame',
    >>>     #'code': '1134',
    >>> }
    >>> 
    >>> complex_function(params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" argument: code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, params):
        def wrapper(params):
            missing = self.required.difference(params)
            if missing:
                raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()

3 Answers 3

7

doctest requires that you use ... for continuation lines:

>>> @requireparams(['name', 'pass', 'code'])
... def complex_function(params):
...     print(params['name'])
...     print(params['pass'])
...     print(params['code'])
...
>>> params = {
...     'name': 'John Doe',
...     'pass': 'OpenSesame',
...     #'code': '1134',
... }
...
>>> complex_function(params)
Sign up to request clarification or add additional context in comments.

Comments

1

Try pasting the code exactly from the python prompt. That means it will contain some ... as well as >>>. Otherwise the doctest parser won't know when there's a multiline expression.

On preview: What Greg said.

Comments

0

Here's my module after I corrected it (it works now):

class requiresparams(object):
    """

    Used as a decorator with an iterable passed in, this will look for each item
    in the iterable given as a key in the params argument of the function being
    decorated. It was built for a series of PayPal methods that require
    different params, and AOP was the best way to handle it while staying DRY.


    >>> @requiresparams(['name', 'pass', 'code'])
    ... def complex_function(params):
    ...     print(params['name'])
    ...     print(params['pass'])
    ...     print(params['code'])
    >>> 
    >>> params = {
    ...     'name': 'John Doe',
    ...     'pass': 'OpenSesame',
    ...     #'code': '1134',
    ... }
    >>> 
    >>> complex_function(params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" argument: code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, params):
        def wrapper(params):
            missing = self.required.difference(params)
            if missing:
                raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.