Skip to content

Scalar type to float error with pyright #1545

@acampove

Description

@acampove

Describe the bug

Type checker error when converting the result of Series.var() (and other statistical methods) to float. The type checker reports that Scalar cannot be assigned to parameter x of type ConvertibleToFloat, even though this conversion works correctly at runtime.

To Reproduce

  1. Minimal runnable example:
import pandas as pd

s = pd.Series([1.0, 2.0, 3.0])
variance = float(s.var())  # Type error here
  1. Type checker: pyright

  2. Error message:

Argument of type "Scalar" cannot be assigned to parameter "x" of type "ConvertibleToFloat" in function "__new__"

The same issue occurs with other statistical methods: .mean(), .std(), .median(), etc.

Please complete the following information:

  • OS: AlmaLinux 9
  • python version: 3.13.1
  • version of type checker: pyright 1.1.407
  • version of installed pandas-stubs: 2.3.3.251201

Additional context

The code works correctly at runtime - Series.var() does return a numeric value that can be converted to float. The issue is that the return type is annotated as Scalar in the type stubs, which the type checker doesn't recognize as convertible to float. Current workaround is to use # type: ignore comments.

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