Discussion:
[FE-discuss] SimpleFormValidator obscures my docstrings (and doctests!)
Matthew Wilson
2008-12-11 04:04:47 UTC
Permalink
I wrote some doctests on a validator, then discovered that decorating
with SimpleFormValidator means that those doctests are on the inner
func attribute, not the outer object, so as far as I can tell, I can
run those doctests.
@SimpleFormValidator.decorate()
def f(value_dict, state, validator):
"""
Add a new key 'f' pointing to 99 to value_dict.
"""
value_dict['f'] = 99
...
f().to_python({}, None)
{'f': 99}

Here's the problem: the docstring on the decorated f is really the
f.__doc__ == SimpleFormValidator.__doc__
True
print f.func.__doc__
Add a new key 'f' pointing to 99 to value_dict.

I'm trying to write some doctests for my validator f, but my test
runner (nose) doesn't run the doctests.

Any ideas about a solution?

Matt
--
Matthew Wilson
***@tplus1.com
http://tplus1.com
Ian Bicking
2008-12-11 04:09:55 UTC
Permalink
I put a little (untested) fix in trunk, r3724. It just tries to copy
__doc__ over to the instance.
Post by Matthew Wilson
I wrote some doctests on a validator, then discovered that decorating
with SimpleFormValidator means that those doctests are on the inner
func attribute, not the outer object, so as far as I can tell, I can
run those doctests.
@SimpleFormValidator.decorate()
"""
Add a new key 'f' pointing to 99 to value_dict.
"""
value_dict['f'] = 99
...
f().to_python({}, None)
{'f': 99}
Here's the problem: the docstring on the decorated f is really the
f.__doc__ == SimpleFormValidator.__doc__
True
print f.func.__doc__
Add a new key 'f' pointing to 99 to value_dict.
I'm trying to write some doctests for my validator f, but my test
runner (nose) doesn't run the doctests.
Any ideas about a solution?
Matt
--
Ian Bicking : ***@colorstudy.com : http://blog.ianbicking.org
Matthew Wilson
2008-12-11 04:13:09 UTC
Permalink
That was fast!

I'm reading test_schema.py to see if I can figure out how to add a
test for this.
Post by Ian Bicking
I put a little (untested) fix in trunk, r3724. It just tries to copy
__doc__ over to the instance.
Post by Matthew Wilson
I wrote some doctests on a validator, then discovered that decorating
with SimpleFormValidator means that those doctests are on the inner
func attribute, not the outer object, so as far as I can tell, I can
run those doctests.
@SimpleFormValidator.decorate()
"""
Add a new key 'f' pointing to 99 to value_dict.
"""
value_dict['f'] = 99
...
f().to_python({}, None)
{'f': 99}
Here's the problem: the docstring on the decorated f is really the
f.__doc__ == SimpleFormValidator.__doc__
True
print f.func.__doc__
Add a new key 'f' pointing to 99 to value_dict.
I'm trying to write some doctests for my validator f, but my test
runner (nose) doesn't run the doctests.
Any ideas about a solution?
Matt
--
--
Matthew Wilson
***@tplus1.com
http://tplus1.com
Matthew Wilson
2008-12-11 04:36:18 UTC
Permalink
Here's a test to add to the bottom of formencode/tests/test_schema.py:

def test_SimpleFormValidator_1():
"""
Verify SimpleFormValidator preserves the decorated function's
docstring.
"""

BOGUS_DOCSTRING = "blah blah blah"

def f(value_dict, state, validator):
value_dict['f'] = 99

f.__doc__ = BOGUS_DOCSTRING
g = SimpleFormValidator(f)

assert f.__doc__ == g.__doc__, "Docstrings don't match!"

Right now, the test is failing. When I print g's docstring, it still
matches the docstring for SimpleFormValidator.
--
Matthew Wilson
***@tplus1.com
http://tplus1.com
Ian Bicking
2008-12-11 04:41:13 UTC
Permalink
Thanks for the test. You were actually using it somewhat differently
here, not using the decorator. Anyway, I moved the __doc__ assignment
around, and now this works.
Post by Matthew Wilson
"""
Verify SimpleFormValidator preserves the decorated function's
docstring.
"""
BOGUS_DOCSTRING = "blah blah blah"
value_dict['f'] = 99
f.__doc__ = BOGUS_DOCSTRING
g = SimpleFormValidator(f)
assert f.__doc__ == g.__doc__, "Docstrings don't match!"
Right now, the test is failing. When I print g's docstring, it still
matches the docstring for SimpleFormValidator.
--
Ian Bicking : ***@colorstudy.com : http://blog.ianbicking.org
Matthew Wilson
2008-12-11 04:47:32 UTC
Permalink
Thanks for the test. You were actually using it somewhat differently here,
not using the decorator. Anyway, I moved the __doc__ assignment around, and
now this works.
This uses the SimpleFormValidator.decorate call:

def test_SimpleFormValidator_2():
"""
Verify SimpleFormValidator.decorate preserves the decorated function's
docstring.
"""

BOGUS_DOCSTRING = "blah blah blah"

@SimpleFormValidator.decorate()
def f(value_dict, state, validator):
"blah blah blah"
value_dict['f'] = 99

assert f.__doc__ == BOGUS_DOCSTRING, "Docstrings don't match!"
--
Matthew Wilson
***@tplus1.com
http://tplus1.com
Loading...