{"id":"0495c6e2ebe75acf","repo":"pypa/pip","slug":"self-class-name-r-used-without-bound-fun","errorCode":null,"errorMessage":"{self.__class__.__name__!r} used without bound function","messagePattern":"(.+?) used without bound function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pygments/filter.py","lineNumber":65,"sourceCode":"    def __init__(self, **options):\n        self.options = options\n\n    def filter(self, lexer, stream):\n        raise NotImplementedError()\n\n\nclass FunctionFilter(Filter):\n    \"\"\"\n    Abstract class used by `simplefilter` to create simple\n    function filters on the fly. The `simplefilter` decorator\n    automatically creates subclasses of this class for\n    functions passed to it.\n    \"\"\"\n    function = None\n\n    def __init__(self, **options):\n        if not hasattr(self, 'function'):\n            raise TypeError(f'{self.__class__.__name__!r} used without bound function')\n        Filter.__init__(self, **options)\n\n    def filter(self, lexer, stream):\n        # pylint: disable=not-callable\n        yield from self.function(lexer, stream, self.options)\n","sourceCodeStart":47,"sourceCodeEnd":71,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pygments/filter.py#L47-L71","documentation":"Raised as TypeError by FunctionFilter.__init__ when the subclass instance has no 'function' attribute set. FunctionFilter is an abstract base used by the @simplefilter decorator, which injects the 'function' class attribute; instantiating FunctionFilter (or a subclass) directly without that attribute is invalid.","triggerScenarios":"Directly constructing FunctionFilter(**options), or a subclass that forgot to set 'function', triggers hasattr(self,'function') == False at __init__ and raises this TypeError.","commonSituations":"Subclassing FunctionFilter manually instead of using @simplefilter, or forgetting to apply the decorator that binds the function.","solutions":["Use the @simplefilter decorator on your function to create a properly bound FunctionFilter subclass.","If subclassing FunctionFilter directly, set a 'function' class attribute (a callable) before instantiation.","Instantiate one of the built-in filters from pygments.filters instead of FunctionFilter itself."],"exampleFix":"# before\nf = FunctionFilter()  # TypeError: used without bound function\n\n# after\nfrom pygments.filter import simplefilter\n\n@simplefilter\ndef my_filter(self, lexer, stream, options):\n    for ttype, value in stream:\n        yield ttype, value.upper()\n\nf = my_filter()","handlingStrategy":"type-guard","validationCode":"from pygments.filter import FunctionFilter\nif isinstance(f, type) and issubclass(f, FunctionFilter):\n    raise TypeError('use @simplefilter to bind a function')\nf = f()  # only if properly decorated","typeGuard":"def is_bound_function_filter(obj) -> bool:\n    from pygments.filter import FunctionFilter\n    return isinstance(obj, FunctionFilter) and getattr(obj, 'function', None) is not None","tryCatchPattern":"try:\n    f = MyFilter()\nexcept TypeError as e:\n    if 'used without bound function' in str(e):\n        # apply @simplefilter to define the function, then retry\n        ...\n    raise","preventionTips":["Use @simplefilter to create filters instead of subclassing FunctionFilter directly.","Never instantiate FunctionFilter or its subclasses without a 'function' attribute."],"tags":["python","pygments","filter","api-misuse"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}