cel-python icon indicating copy to clipboard operation
cel-python copied to clipboard

Implement string.contains() per CEL spec section 6.2.5

Open Quantum0uasar opened this issue 6 months ago • 1 comments

Summary

The CEL specification (section 6.2.5) defines string.contains(string) -> bool as a standard string function. Currently, calling .contains() on a StringType raises CELEvalError: no such overload in cel-python.

Steps to reproduce

import celpy

env = celpy.Environment()
ast = env.compile('"hello world".contains("world")')
prog = env.program(ast)
result = prog.evaluate({})
print(result)  # Expected: true

Actual behaviour

celpy.celenv.CELEvalError: no such overload

Expected behaviour

contains() should return true when the substring is present and false otherwise, matching the Go and Java CEL implementations.

Proposed fix

Add a contains method to StringType in src/celpy/celtypes.py:

def contains(self, other: 'StringType') -> BoolType:
    return BoolType(str(other) in str(self))

And register the overload in the function dispatch table so that "x".contains("y") is correctly dispatched.

Related

  • Issue #128 (user question about .contains() not working)
  • CEL spec reference: https://github.com/google/cel-spec/blob/master/doc/langdef.md#standard-definitions

Quantum0uasar avatar May 28 '26 18:05 Quantum0uasar

This looks like a great idea. Thanks for pursuing it.

Note that there may be related acceptance tests that are marked with @wip. (I haven't looked, I'm only assuming there might be some test marked @wip.) There are two parts to changing the marking on test.

  1. Present. Update the features/*/*.feature file to remove the @wip tag from tests related to contains(). This demonstrates the fix works.
  2. Future. Update the tools/tags.toml file to remove the test name so that all future extracts of test cases will not add a @wip tag by surprise.

slott56 avatar Jun 01 '26 12:06 slott56