pypa/pip · error · TypeError

{self.__class__.__name__!r} used without bound function

Error message

{self.__class__.__name__!r} used without bound function

What it means

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.

Source

Thrown at src/pip/_vendor/pygments/filter.py:65

    def __init__(self, **options):
        self.options = options

    def filter(self, lexer, stream):
        raise NotImplementedError()


class FunctionFilter(Filter):
    """
    Abstract class used by `simplefilter` to create simple
    function filters on the fly. The `simplefilter` decorator
    automatically creates subclasses of this class for
    functions passed to it.
    """
    function = None

    def __init__(self, **options):
        if not hasattr(self, 'function'):
            raise TypeError(f'{self.__class__.__name__!r} used without bound function')
        Filter.__init__(self, **options)

    def filter(self, lexer, stream):
        # pylint: disable=not-callable
        yield from self.function(lexer, stream, self.options)

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Use the @simplefilter decorator on your function to create a properly bound FunctionFilter subclass.
  2. If subclassing FunctionFilter directly, set a 'function' class attribute (a callable) before instantiation.
  3. Instantiate one of the built-in filters from pygments.filters instead of FunctionFilter itself.

Example fix

# before
f = FunctionFilter()  # TypeError: used without bound function

# after
from pygments.filter import simplefilter

@simplefilter
def my_filter(self, lexer, stream, options):
    for ttype, value in stream:
        yield ttype, value.upper()

f = my_filter()
Defensive patterns

Strategy: type-guard

Validate before calling

from pygments.filter import FunctionFilter
if isinstance(f, type) and issubclass(f, FunctionFilter):
    raise TypeError('use @simplefilter to bind a function')
f = f()  # only if properly decorated

Type guard

def is_bound_function_filter(obj) -> bool:
    from pygments.filter import FunctionFilter
    return isinstance(obj, FunctionFilter) and getattr(obj, 'function', None) is not None

Try / catch

try:
    f = MyFilter()
except TypeError as e:
    if 'used without bound function' in str(e):
        # apply @simplefilter to define the function, then retry
        ...
    raise

Prevention

When it happens

Trigger: Directly constructing FunctionFilter(**options), or a subclass that forgot to set 'function', triggers hasattr(self,'function') == False at __init__ and raises this TypeError.

Common situations: Subclassing FunctionFilter manually instead of using @simplefilter, or forgetting to apply the decorator that binds the function.

Related errors


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