Skip to content

feat: Let constructor fixtures return narwhals object directly#3569

Open
FBruzzesi wants to merge 5 commits intofeat/testing-constructorsfrom
feat/testing-constructors-to-narwhals
Open

feat: Let constructor fixtures return narwhals object directly#3569
FBruzzesi wants to merge 5 commits intofeat/testing-constructorsfrom
feat/testing-constructors-to-narwhals

Conversation

@FBruzzesi
Copy link
Copy Markdown
Member

Description

Alright, at this point I briefly mention this branch to all of you @MarcoGorelli @dangotbanned @camriddell so I might as well open the PR.

I think it is promising, but I might need some help for the typing, here is a repro for the issue I am facing:

from __future__ import annotations

from typing import TYPE_CHECKING, reveal_type

import narwhals as nw

if TYPE_CHECKING:
    from narwhals.testing.typing import FrameConstructor


def test_typing(nw_frame: FrameConstructor) -> None:
    data = {"a": [-1, 2, 3], "b": [1, 1, 2], "i": [0, 1, 2]}
    df = nw_frame(data, nw).lazy()
    reveal_type(df)  # -> LazyFrame[Any]

    native = df.to_native()
    reveal_type(native)  # -> Any
    
    frame = nw.from_native(native)
    reveal_type(frame)  # ->  Any

which seems far from ideal! I didn't manage to get the __call__ overload quite right, but it should not be far from the correct solution. I tried a few other variations without success.


Regarding the patches in conftest.py:

  1. I wouldn't worry too much for their typing, they are just temporary workaround to avoid touching every single test
  2. The hack is needed for the same reason, namely make narwhals (main namespace) the default namespace to the __call__ method and avoid having to pass it everywhere. This works since nw.from_native can also take a narwhals object

@FBruzzesi FBruzzesi added the enhancement New feature or request label Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant