[Python] Mypy type hints for arguments which are pandas series

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following, which I would expect to raise an error:

    # check.py
    import pandas as pd

    def f(s : str) -> None:
    print(s)

    def main() -> None:
    f(s = pd.Series([1,2]))

    if __name__ == "__main__":
    main()


    However, mypy check.py returns Success: no issues found in 1 source file.

    How is one supposed to provide typehints for pandas series?

    In the pandas code (https://github.com/pandas-dev/panda...f1c3277cfe8c6b9ce7/pandas/core/series.py#L511) there's:

    def _constructor(self) -> type[Series]:
    return Series


    type[Series], I'm not sure if this is how we're supposed to typehint series though?

    edit


    updating the code to:

    # check.py
    import pandas as pd

    def f(s : str) -> None:
    print(s)

    def g(s : str) -> None:
    print(s)

    def main() -> None:
    f(s = pd.Series([1,2]))
    g(s = 12.9)

    if __name__ == "__main__":
    main()


    gives the output:

    check.py:12: error: Argument "s" to "g" has incompatible type "float"; expected "str" [arg-type]
    Found 1 error in 1 file (checked 1 source file)

    Continue reading...

Compartilhe esta Página