Skip to content

documentation recommends accessing config.cache in an unsafe way #14148

Description

@DetachHead

https://docs.pytest.org/en/stable/how-to/cache.html#the-new-config-cache-object

in this example recommends accessing pytestconfig.cache which will crash at runtime if the cacheprovider plugin has been disabled as mentioned here:

_______________________________________________________________________________ ERROR at setup of test_function ________________________________________________________________________________

pytestconfig = <_pytest.config.Config object at 0x749189eaa7b0>

    @pytest.fixture
    def mydata(pytestconfig):
>       val = pytestconfig.cache.get("example/value", None)
E       AttributeError: 'Config' object has no attribute 'cache'

python/tests/test_asdf.py:12: AttributeError
=================================================================================== short test summary info ====================================================================================
ERROR python/tests/test_asdf.py::test_function - AttributeError: 'Config' object has no attribute 'cache'
======================================================================================= 1 error in 0.07s =======================================================================================

pytest should set the cache object to None when the plugin isn't enabled. that way, type checkers will report an error when attempting to use it without checking whether it's None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions