{"id":"477e0585e600b8ba","repo":"pypa/pip","slug":"lex-argument-must-be-a-lexer-instance-not-a-cla","errorCode":null,"errorMessage":"lex() argument must be a lexer instance, not a class","messagePattern":"lex\\(\\) argument must be a lexer instance, not a class","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pygments/__init__.py","lineNumber":47,"sourceCode":"__version__ = '2.20.0'\n__docformat__ = 'restructuredtext'\n\n__all__ = ['lex', 'format', 'highlight']\n\n\ndef lex(code, lexer):\n    \"\"\"\n    Lex `code` with the `lexer` (must be a `Lexer` instance)\n    and return an iterable of tokens. Currently, this only calls\n    `lexer.get_tokens()`.\n    \"\"\"\n    try:\n        return lexer.get_tokens(code)\n    except TypeError:\n        # Heuristic to catch a common mistake.\n        from pip._vendor.pygments.lexer import RegexLexer\n        if isinstance(lexer, type) and issubclass(lexer, RegexLexer):\n            raise TypeError('lex() argument must be a lexer instance, '\n                            'not a class')\n        raise\n\n\ndef format(tokens, formatter, outfile=None):  # pylint: disable=redefined-builtin\n    \"\"\"\n    Format ``tokens`` (an iterable of tokens) with the formatter ``formatter``\n    (a `Formatter` instance).\n\n    If ``outfile`` is given and a valid file object (an object with a\n    ``write`` method), the result will be written to it, otherwise it\n    is returned as a string.\n    \"\"\"\n    try:\n        if not outfile:\n            realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()\n            formatter.format(tokens, realoutfile)\n            return realoutfile.getvalue()","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pygments/__init__.py#L29-L65","documentation":"Raised as TypeError by pygments.lex() when the lexer argument is a Lexer *class* rather than an *instance*. lex() calls lexer.get_tokens(code); passing the class raises a TypeError from unbound-method access, which the heuristic catches and re-raises with this clearer message when the class subclasses RegexLexer.","triggerScenarios":"Calling pygments.lex(code, PythonLexer) (the class) instead of pygments.lex(code, PythonLexer()). The except-TypError branch detects issubclass(lexer, RegexLexer) and raises the helpful message.","commonSituations":"Forgetting to instantiate the lexer, copying example code that omitted parentheses, or passing get_lexer_by_name result incorrectly.","solutions":["Instantiate the lexer: pass PythonLexer() (with parentheses) to lex().","Use pygments.lexers.get_lexer_by_name('python') which returns a ready instance.","Double-check that your variable holds an instance, not the class object."],"exampleFix":"# before\nfrom pygments import lex\nfrom pygments.lexers import PythonLexer\ntokens = lex(code, PythonLexer)  # TypeError\n\n# after\ntokens = lex(code, PythonLexer())","handlingStrategy":"type-guard","validationCode":"from pygments.lexer import RegexLexer\nif isinstance(lexer, type) and issubclass(lexer, RegexLexer):\n    raise TypeError('pass a lexer instance, not the class')\npygments.lex(code, lexer)","typeGuard":"def is_lexer_instance(obj) -> bool:\n    from pygments.lexer import Lexer\n    return not isinstance(obj, type) and isinstance(obj, Lexer)","tryCatchPattern":"try:\n    tokens = pygments.lex(code, lexer)\nexcept TypeError as e:\n    if 'must be a lexer instance' in str(e):\n        tokens = pygments.lex(code, lexer())  # instantiate class\n    raise","preventionTips":["Always instantiate lexer classes with parentheses.","Use get_lexer_by_name() to obtain ready instances."],"tags":["python","pygments","lexer","api-misuse"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}