{"record":{"id":"09bc783e26d0b17b","repo":"nodejs/node","slug":"this-loader-cannot-iterate-over-all-templates","errorCode":null,"errorMessage":"this loader cannot iterate over all templates","messagePattern":"this loader cannot iterate over all templates","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/loaders.py","lineNumber":97,"sourceCode":"        otherwise `None`.  The filename is used by python for the tracebacks\n        if no loader extension is used.\n\n        The last item in the tuple is the `uptodate` function.  If auto\n        reloading is enabled it's always called to check if the template\n        changed.  No arguments are passed so the function must store the\n        old state somewhere (for example in a closure).  If it returns `False`\n        the template will be reloaded.\n        \"\"\"\n        if not self.has_source_access:\n            raise RuntimeError('%s cannot provide access to the source' %\n                               self.__class__.__name__)\n        raise TemplateNotFound(template)\n\n    def list_templates(self):\n        \"\"\"Iterates over all templates.  If the loader does not support that\n        it should raise a :exc:`TypeError` which is the default behavior.\n        \"\"\"\n        raise TypeError('this loader cannot iterate over all templates')\n\n    @internalcode\n    def load(self, environment, name, globals=None):\n        \"\"\"Loads a template.  This method looks up the template in the cache\n        or loads one by calling :meth:`get_source`.  Subclasses should not\n        override this method as loaders working on collections of other\n        loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`)\n        will not call this method but `get_source` directly.\n        \"\"\"\n        code = None\n        if globals is None:\n            globals = {}\n\n        # first we try to get the source for this template together\n        # with the filename and the uptodate function.\n        source, filename, uptodate = self.get_source(environment, name)\n\n        # try to load the code from the bytecode cache if there is a","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/loaders.py#L79-L115","documentation":"BaseLoader.list_templates() intentionally raises TypeError('this loader cannot iterate over all templates') as its default. Listing every template is not meaningful for all loader backends, so concrete loaders must opt in by overriding list_templates; otherwise the operation is unsupported.","triggerScenarios":"Calling environment.list_templates() or loader.list_templates() on a loader that does not implement enumeration (e.g., a custom loader, or a loader backed by a source that cannot be walked).","commonSituations":"Building admin/debug tooling that enumerates available templates; running tests that call list_templates against a minimal custom loader.","solutions":["Override list_templates() in your loader subclass to return a list of names.","Use a loader that supports enumeration (FileSystemLoader, PackageLoader, DictLoader, PrefixLoader, ChoiceLoader) if you need listing.","Guard callers by hasattr(loader, 'list_templates') or by catching TypeError when enumeration is optional."],"exampleFix":"# before\nclass MyLoader(BaseLoader):\n    pass\nMyLoader().list_templates()  # TypeError\n\n# after\nclass MyLoader(BaseLoader):\n    def list_templates(self):\n        return sorted(self._db.keys())","handlingStrategy":"type-guard","validationCode":"from jinja2 import BaseLoader\n\ndef loader_can_list(loader) -> bool:\n    return type(loader).list_templates is not BaseLoader.list_templates","typeGuard":"from jinja2 import BaseLoader\n\ndef supports_list_templates(loader) -> bool:\n    return type(loader).list_templates is not BaseLoader.list_templates","tryCatchPattern":"try:\n    names = env.list_templates()\nexcept TypeError:\n    names = []  # enumeration unsupported by this loader","preventionTips":["Override list_templates() in custom loaders that back enumerable sources.","Use a concrete loader (FileSystemLoader, DictLoader, etc.) when enumeration is required.","Treat list_templates as optional and guard callers against TypeError."],"tags":["jinja2","loader","list-templates","not-supported"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}