Skip to content

pkg_resources.NullProvider since pytest 4.6.0+ #5392

@tobywf

Description

@tobywf

When using a pytest version 4.6.0 or up, Jinja's PackageLoader breaks with "NotImplementedError: Can't perform this operation for unregistered loader type". This is because pkg_resources returns a NullProvider.

Minimal working example:

python3 -m venv env && source env/bin/activate
pip install 'pytest>=4.6.0' 'jinja2==2.10.1'
mkdir -p tests/templates
touch tests/__init__.py
echo "hello, world" > tests/templates/test.txt
cat << EOF > tests/test_jinja.py
from jinja2 import Environment, PackageLoader

def test_package_loader():
    env = Environment(loader=PackageLoader(__name__))
    assert "hello" in env.get_template("test.txt").render()
EOF
pytest tests/
pip install 'pytest==4.5.0'
pytest tests/
echo "done"

With pytest>=4.6.0 (confirmed with 4.6.0, 4.6.1, and 4.6.2), it fails with:

self = <pkg_resources.NullProvider object at 0x10c2d69b0>, path = '/workplace/tobflem/pytest/tests/templates/test.txt'

    def _has(self, path):
        raise NotImplementedError(
>           "Can't perform this operation for unregistered loader type"
        )
E       NotImplementedError: Can't perform this operation for unregistered loader type

env/lib/python3.7/site-packages/pkg_resources/__init__.py:1457: NotImplementedError

After pytest is pinned to 4.5.0 again, no failure is observed.

There was an issue way back, #366, which seems to be quite similar, where "__loader__ is being wrapped in an AssertionRewritingHook". Could be a similar issue?

Hope this is enough info, let me know if you need more!

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