Skip to content

Question: Implementation/backend-related mypy complaint since narwhals 2.2.0 #3076

@kklein

Description

@kklein

Hi!

Since narwhals 2.2.0 I observe that running mypy against

import narwhals as nw
import numpy as np
import polars as pl

X = pl.DataFrame({"col_a": [1, 2, 3], "col_b": [1, 2, 3]})
Y = np.array([1,2,3]).reshape(-1, 1)

X_nw = nw.from_native(X)
Y_nw = nw.from_numpy(Y, backend=X_nw.implementation)

yields

error: Argument "backend" to "from_numpy" has incompatible type "Implementation"; expected "Literal['pandas', 'cudf', 'modin', Implementation.PANDAS, Implementation.CUDF, Implementation.MODIN, 'pyarrow', Implementation.PYARROW, 'polars', Implementation.POLARS] | Module"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

If I pass str(X_nw.implementation) or X_nw.implementation.value instead of X_nw.implementation I get

error: Argument "backend" to "from_numpy" has incompatible type "str"; expected "Literal['pandas', 'cudf', 'modin', Implementation.PANDAS, Implementation.CUDF, Implementation.MODIN, 'pyarrow', Implementation.PYARROW, 'polars', Implementation.POLARS] | Module"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Is that behaviour expected? If yes, how can I do better?

  • MacOS ARM
  • python: 3.13.5
  • mypy: 1.17.1
  • narwhals: 2.2.0

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