Closed kiilerix closed 4 years ago
Given the pretty uncommon chances that a StackedObjectProxy is used as a callable and the even less common chances that it gets decorated, I think that https://github.com/TurboGears/tg2/commit/77132ba119733d4050e7dfac5f631edd0a590e8a is a reasonable work-around to the problem.
I hoped that removing __call__
support from the object would have solved the issue, but it seems that unwrap
doesn't check that what is being provided is a callable at all.
Yeah, that seems like an OK way to avoid the problem. Thanks.
Considering that AttributeError usually has args[0] like module 'os' has no attribute 'foo'
, perhaps use something more helpful than just foo
. Perhaps something as no 'context' has has been registered for this thread and there is thus no attribute 'foo'
.
But this also leads to the question of whether it just always should fail with AttributeError instead of TypeError.
If not always returning AttributeError, then perhaps do it for all __
methods - not just __wrapped__
.
Can you recommend a workaround for using pytest doctest with existing TG2 versions?
Probably going for all dunder methods makes sense. I'll update the patch.
Regarding how to avoid problem with current version. I think that the issue comes from the fact that it's exploring all variables exposed in modules or something like that. Maybe changing from tg import request
to things like import tg
and then using tg.request
instead of just request
might fix the issue.
A similar issue has also been reported for Flask (who uses threadlocal objects too) with pytest a few times, even though I think it's the first time I see it in the context of doctest.
TurboGears2 and pytest doesn't in all cases play well together. Like when using the
from tg import tmpl_context
pattern used in the documentation in combination with pytest for running doctests.pytest's doctest support is (in
_mock_aware_unwrap
) using py3 inspect.Inside inspect,
_is_wrapper
will do an innocent looking:hasattr(f, '__wrapped__')
But if the code under test has un (unused) import of a tg context (such as tg.request), it is no longer so innocent. tg will throw:
TypeError: No object (name: context) has been registered for this thread
(which in py2 would have caught by hasattr, but not in py3.)I don't know if it should be solved in pytest (perhaps by not using inspect), in TurboGears (perhaps by having a default context or at least be aware of how the standard library use
__wrapped__
), or in inspect.This might be the core of the 2nd problem mentioned on https://github.com/TurboGears/tg2/issues/117