Skip to content
Myr

Improve the PackageLoader error message (#1706)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents 58a358f, aaa083d · Browse files · 3 files+18 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+18 added−7 removed

  • tests:+5 added−0 removed
  • other:+13 added−7 removed

3 files+18 added−7 removed

modified:src/jinja2/loaders.py+11 added-7 removed
325 unmodified lines
326 loader = spec.loader327 assert loader is not None, "A loader was not found for the package."328 self._loader = loader329 self._archive = None330 template_root = None330331 if isinstance(loader, zipimport.zipimporter):332 self._archive = loader.archive333 pkgdir = next(iter(spec.submodule_search_locations)) # type: ignore
8 unmodified lines
342 # A single module file, use the parent directory instead.343 elif spec.origin is not None:344 roots.append(os.path.dirname(spec.origin))345346 if not roots:347 raise ValueError(348 f"The {package_name!r} package was not installed in a"349 " way that PackageLoader understands."350 )351352 for root in roots:353 root = os.path.join(root, package_path)354355 if os.path.isdir(root):356 template_root = root357 break358 else:359 raise ValueError(360 f"PackageLoader could not find a {package_path!r} directory"361 f" in the {package_name!r} package."362 )363354 if template_root is None:355 raise ValueError(356 f"The {package_name!r} package was not installed in a"357 " way that PackageLoader understands."358 )359364 self._template_root = template_root365366 def get_source(367 self, environment: "Environment", template: str
326 unmodified lines
modified:tests/test_loader.py+5 added
427 unmodified lines
428 package_loader = PackageLoader("res")429 assert "test.html" in package_loader.list_templates()430 finally:431 sys.meta_path[:] = before432433434def test_package_loader_no_dir() -> None:435 with pytest.raises(ValueError, match="could not find a 'templates' directory"):436 PackageLoader("jinja2")
modified:CHANGES.rst+2 added
35 unmodified lines
36 :issue:`2027`37- ``Environment.overlay(enable_async)`` is applied correctly. :pr:`2061`38- The error message from ``FileSystemLoader`` includes the paths that were39 searched. :issue:`1661`40- ``PackageLoader`` shows a clearer error message when the package does not41 contain the templates directory. :issue:`1705`424344Version 3.1.445-------------
994 unmodified lines