`TypeIs` guard does not narrow mixed input union in Pyrefly

Open
#4,064 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with the is_into_series type guard in narwhals/stable/v2/dependencies and the typing behavior described in the linked Pyrefly issue #4849. Check whether the guard can narrow the shown union in Pyrefly while following the relevant typing specification. Done means the example narrows value sufficiently for nw.from_native(value, series_only=True) to type-check, or the issue documents why Narwhals cannot provide that narrowing.

Written by the indexing model from the issue text.

Description

Not sure if narwhals can do something on the typing side, but I recently stumbled over this situation here:

from collections.abc import Iterable
from typing import Any, reveal_type

import narwhals.stable.v2 as nw
from narwhals.stable.v2.dependencies import is_into_series


def func(value: nw.IntoSeries | Iterable[Any] | None) -> None:
    if is_into_series(value):
        reveal_type(value)
        nw.from_native(value, series_only=True)

for which pyrefly produces:

ERROR No matching overload found for function `narwhals.stable.v2.from_native` called with arguments: (Iterable[Any] | NativeSeries | None, series_only=Literal[True]) [no-matching-overload]

However, pyrefly seems to just follow the spec:
https://github.com/facebook/pyrefly/issues/4849

Dominant language
Python
Stars
1.7k
Forks
242
Avg merge
1d 5h
Merged PRs (30d)
49

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from narwhals-dev/narwhals

All issues in narwhals-dev/narwhals

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.