Skip to content

Conversation

@FBruzzesi
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

Heavily inspired by @dangotbanned code for arrow

@FBruzzesi FBruzzesi added internal pandas-like Issue is related to pandas-like backends labels Jul 16, 2025
def calculate_timestamp_datetime( # noqa: C901, PLR0912
s: NativeSeriesT, original_time_unit: str, time_unit: str
_TIMESTAMP_DATETIME_OP_FACTOR: Mapping[
tuple[UnitCurrent, UnitTarget], tuple[BinOpBroadcast, IntoRhs]
Copy link
Member

@dangotbanned dangotbanned Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2838 (comment)

I think the issue is that BinOpBroadcast is generic, but using a TypeVar with a default.

So you don't get a warning when using BinOpBroadcast without specialising - but it causes an issue later when the default conflicts with the constraints of the function signature.

Hopefully this works, I'm replying from my phone πŸ˜…

Suggested change
tuple[UnitCurrent, UnitTarget], tuple[BinOpBroadcast, IntoRhs]
tuple[UnitCurrent, UnitTarget], tuple[BinOpBroadcast[NativeSeriesT], IntoRhs]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

narwhals/_pandas_like/utils.py:583: error: Argument 1 has incompatible type "_CuDFSeries"; expected "Series[Any]"  [arg-type]
narwhals/_pandas_like/utils.py:583: error: Argument 1 has incompatible type "_ModinSeries"; expected "Series[Any]"  [arg-type]

NativeSeriesT = TypeVar(
"NativeSeriesT",
"pd.Series[Any]",
"_CuDFSeries",
"_ModinSeries",
default="pd.Series[Any]",
)

NativeSeriesT means exactly one of pd.Series[Any], _CuDFSeries, _ModinSeries, but default to pd.Series[Any] when not provided.

So the conflict is that

  • NativeSeriesT is still used for the function parameter and return type
  • But _TIMESTAMP_DATETIME_OP_FACTOR is currently saying it only accepts pd.Series[Any]

def calculate_timestamp_datetime(
s: NativeSeriesT, current: TimeUnit, time_unit: TimeUnit
) -> NativeSeriesT:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dangotbanned thanks for investigating this.

But _TIMESTAMP_DATETIME_OP_FACTOR is currently saying it only accepts pd.Series[Any]

Why would that be the case? The definition is:

BinOpBroadcast: TypeAlias = Callable[[NativeSeriesT, int], NativeSeriesT]

Regarding #2838 (comment), BinOpBroadcast is not a generic, and it would result in:

Type variable "NativeSeriesT" has no meaning in this contextPylance[reportGeneralTypeIssues]

🧐

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dangotbanned thanks for investigating this.

But _TIMESTAMP_DATETIME_OP_FACTOR is currently saying it only accepts pd.Series[Any]

Why would that be the case? The definition is:

BinOpBroadcast: TypeAlias = Callable[[NativeSeriesT, int], NativeSeriesT]

Regarding #2838 (comment), BinOpBroadcast is not a generic, and it would result in:

BinOpBroadcast is a generic TypeAlias because it uses a TypeVar (NativeSeriesT) in it's definition.

image

If we compare that to when you hover over the current use, it uses the default type - because it wasn't subscribed

image

Type variable "NativeSeriesT" has no meaning in this contextPylance[reportGeneralTypeIssues]

🧐

Ahh, so that message it about scoping - not that the TypeVar can't be used at all

So this is because _TIMESTAMP_DATETIME_OP_FACTOR is in a global scope, but we're trying to solve the TypeVar in a function scope

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just used Any instead in (bdb46aa)

The main reason I had that typing in _arrow was because the stubs caused too many false positives when I used the functions directly.

Here, we're just using operator functions, which are mostly typed with Any anyway πŸ˜‰

https://github.com/python/typeshed/blob/8e25bda401332ac56d4750cffda2127a4302cd2f/stdlib/_operator.pyi#L42-L71

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have NativeSeriesT preserved on the outside, so this is simpler than refactoring everything to provide a scope for the TypeVar πŸ˜…

@dangotbanned dangotbanned mentioned this pull request Jul 17, 2025
8 tasks
Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @FBruzzesi

@FBruzzesi
Copy link
Member Author

Thank you @dangotbanned πŸŽ‰

@FBruzzesi FBruzzesi merged commit 24c9c08 into main Jul 17, 2025
31 of 32 checks passed
@FBruzzesi FBruzzesi deleted the refactor/simplify-calculate_timestamp_datetime branch July 17, 2025 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal pandas-like Issue is related to pandas-like backends

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants