pypa/pip · error · ClassNotFound

no lexer for alias {_alias!r} found

Error message

no lexer for alias {_alias!r} found

What it means

Raised by find_lexer_class_by_name when the alias argument is empty/falsy (None or empty string) before any lookup is attempted. It is the explicit empty-input guard at the top of that function.

Source

Thrown at src/pip/_vendor/pygments/lexers/__init__.py:96

    for cls in find_plugin_lexers():
        if cls.name == name:
            return cls


def find_lexer_class_by_name(_alias):
    """
    Return the `Lexer` subclass that has `alias` in its aliases list, without
    instantiating it.

    Like `get_lexer_by_name`, but does not instantiate the class.

    Will raise :exc:`pygments.util.ClassNotFound` if no lexer with that alias is
    found.

    .. versionadded:: 2.2
    """
    if not _alias:
        raise ClassNotFound(f'no lexer for alias {_alias!r} found')
    # lookup builtin lexers
    for module_name, name, aliases, _, _ in LEXERS.values():
        if _alias.lower() in aliases:
            if name not in _lexer_cache:
                _load_lexers(module_name)
            return _lexer_cache[name]
    # continue with lexers from setuptools entrypoints
    for cls in find_plugin_lexers():
        if _alias.lower() in cls.aliases:
            return cls
    raise ClassNotFound(f'no lexer for alias {_alias!r} found')


def get_lexer_by_name(_alias, **options):
    """
    Return an instance of a `Lexer` subclass that has `alias` in its
    aliases list. The lexer is given the `options` at its
    instantiation.

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Supply a non-empty, known alias string.
  2. Default the variable to a safe alias like 'python' or 'text' before calling.
  3. Validate the input is a non-empty string first.

Example fix

# before
cls = find_lexer_class_by_name(lang)  # lang is None
# after
lang = lang or 'text'
cls = find_lexer_class_by_name(lang)
Defensive patterns

Strategy: validation

Validate before calling

def valid_alias(a):
    return isinstance(a, str) and bool(a.strip())
if not valid_alias(alias):
    raise ValueError('alias must be a non-empty string')

Type guard

def is_nonempty_alias(a):
    return isinstance(a, str) and len(a.strip()) > 0

Prevention

When it happens

Trigger: find_lexer_class_by_name(''), find_lexer_class_by_name(None), or passing a variable that was never assigned a language string.

Common situations: Reading the language from optional config that's absent; a CLI flag that wasn't supplied defaulting to None; upstream code passing through an unset value.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/dba28d89d930deb9.json. Report an issue: GitHub.