I've added an assert False
statement in flask/helpers.py in the following class's __init__
method:
class locked_cached_property(object):
"""A decorator that converts a function into a lazy property. The
function wrapped is called the first time to retrieve the result
and then that calculated result is used the next time you access
the value. Works like the one in Werkzeug but has a lock for
thread safety.
"""
def __init__(self, func, name=None, doc=None):
assert False
# ^ this is the problem
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
self.lock = RLock()
def __get__(self, obj, type=None):
if obj is None:
return self
with self.lock:
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
and now when I'm running pytest
I get the following traceback:
Traceback (most recent call last):
File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 381, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('~/code/flask/tests')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 412, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local('~/code/flask/tests/conftest.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 418, in _importconftest
mod = conftestpath.pyimport()
File "~/code/virtualenv/project_a/lib/python3.6/site-packages/py/_path/local.py", line 668, in pyimport
__import__(modname)
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 290, in load_module
six.exec_(co, mod.__dict__)
File "~/code/flask/tests/conftest.py", line 19, in <module>
import flask
File "~/code/flask/flask/__init__.py", line 21, in <module>
from .app import Flask, Request, Response
File "~/code/flask/flask/app.py", line 26, in <module>
from . import cli, json
File "~/code/flask/flask/cli.py", line 32, in <module>
from .helpers import get_debug_flag, get_env, get_load_dotenv
File "~/code/flask/flask/helpers.py", line 894, in <module>
class _PackageBoundObject(object):
File "~/code/flask/flask/helpers.py", line 956, in _PackageBoundObject
@locked_cached_property
File "~/code/flask/flask/helpers.py", line 876, in __init__
assert False
AssertionError
ERROR: could not load ~/code/flask/tests/conftest.py
I'm curious how an assert False
statement can seem to cause an import failure when in the __init__
function. If the assert False
was in the __get__
function I am still able to import the module.
This occurs for pytest 3.8.1 and 4.0.2.
Does this mean that a decorator's __init__
is called during the importing of the module?
Your decorator will always raise an exception when being used. And remember that the decorator code is executed right after the def
line it decorates. So, in most cases the module can not be imported without an exception. Since pytest
imports a module first it will never get to the point where it executes the tests.