Skip to content

[Bug]: str.contains fails if passed Expr as match #3459

@liamholmes31

Description

@liamholmes31

Describe the bug

not sure if this is a bug or intended, but behaviour for nw.Expr.str.contains does not match polars when an expr is passed as the match argument

Steps or code to reproduce the bug

import polars as pl
import narwhals as nw

df = pl.DataFrame(
    {
        'a': ['x', 'y'],
        'b': ['x', 'z']
    }
)

df = nw.from_native(df)

df.with_columns(
    nw.col('a').str.contains(nw.col('b'), literal=True)
)

TypeError: cannot create expression literal for value of type Expr.

Expected results

Running directly in polars gives the expected result:

shape: (2, 2)
┌───────┬─────┐
│ a     ┆ b   │
│ ---   ┆ --- │
│ bool  ┆ str │
╞═══════╪═════╡
│ true  ┆ x   │
│ false ┆ z   │
└───────┴─────┘

Actual results

TypeError: cannot create expression literal for value of type Expr.

Please run narwhals.show_versions() and enter the output below.

System:
    python: 3.12.12 (main, Oct 14 2025, 21:38:21) [Clang 20.1.4 ]
executable: /Users/liamholmes/Documents/Code/narwhals/.venv/bin/python
   machine: macOS-14.5-arm64-arm-64bit

Python dependencies:
     narwhals: 2.14.0
        numpy: 2.3.4
       pandas: 2.3.3
        modin: 0.37.1
         cudf: 
      pyarrow: 22.0.0
      pyspark: 
       polars: 1.34.0
         dask: 2025.11.0
       duckdb: 1.3.0
         ibis: 11.0.0
     sqlframe: 3.43.8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions