Skip to content

Openpyxl iter_rows is no more compatible with next function #13151

Closed

Description

@mutricyl

The following was fine with mypy until types-openpyxl-3.1.5.20241114:

next(wb[sheet].iter_rows(...)

But when I upgraded to types-openpyxl-3.1.5.20241126 I have the following error:

error: Argument 1 to "next" has incompatible type "Generator[tuple[Cell | MergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]"; expected "SupportsNext[tuple[str | float | datetime | int | None | Cell, ...]]"  [arg-type]

I do not understand why a Generator is not considered as SupportsNext ?

Activity

  1. mutricyl commented on Nov 28, 2024

    @mutricyl
    Author

    looks like commit 82b5fcc is the culprit. Any thought @Avasam ?

  2. Avasam commented on Nov 29, 2024

    @Avasam
    SponsorCollaborator

    @mutricyl Do you think you could provide a more complete example? I am unable to replicate.

    from openpyxl.workbook import Workbook
    from typing_extensions import reveal_type
    
    
    def foo(unknown_values_only: bool) -> None:
        reveal_type(Workbook()["sheet"].iter_rows())
        reveal_type(next(Workbook()["sheet"].iter_rows()))
        reveal_type(Workbook()["sheet"].iter_rows(values_only=True))
        reveal_type(next(Workbook()["sheet"].iter_rows(values_only=True)))
        reveal_type(Workbook()["sheet"].iter_rows(values_only=unknown_values_only))
        reveal_type(next(Workbook()["sheet"].iter_rows(values_only=unknown_values_only)))

    mypy --strict C:\Users\Avasam\Desktop\openpyxl_type_test.py

    PS C:\Users\Avasam> mypy --strict C:\Users\Avasam\Desktop\openpyxl_type_test.py
    Desktop\openpyxl_type_test.py:6: note: Revealed type is "typing.Generator[builtins.tuple[Union[openpyxl.cell.cell.Cell, openpyxl.cell.cell.MergedCell], ...], None, None]"
    Desktop\openpyxl_type_test.py:7: note: Revealed type is "builtins.tuple[Union[openpyxl.cell.cell.Cell, openpyxl.cell.cell.MergedCell], ...]"
    Desktop\openpyxl_type_test.py:8: note: Revealed type is "typing.Generator[builtins.tuple[Union[builtins.str, builtins.float, datetime.datetime, None], ...], None, None]"
    Desktop\openpyxl_type_test.py:9: note: Revealed type is "builtins.tuple[Union[builtins.str, builtins.float, datetime.datetime, None], ...]"
    Desktop\openpyxl_type_test.py:10: note: Revealed type is "Union[typing.Generator[builtins.tuple[Union[openpyxl.cell.cell.Cell, openpyxl.cell.cell.MergedCell], ...], None, None], typing.Generator[builtins.tuple[Union[builtins.str, builtins.float, datetime.datetime, None], ...], None, None]]"
    Desktop\openpyxl_type_test.py:11: note: Revealed type is "builtins.tuple[Union[builtins.str, builtins.float, datetime.datetime, None, openpyxl.cell.cell.Cell, openpyxl.cell.cell.MergedCell], ...]"
    Success: no issues found in 1 source file
    

    pyright C:\Users\Avasam\Desktop\openpyxl_type_test.py

    c:\Users\Avasam\Desktop\openpyxl_type_test.py
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:6:17 - information: Type of "Workbook()["sheet"].iter_rows()" is "Generator[tuple[Cell | MergedCell, ...], None, None]"
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:7:17 - information: Type of "next(Workbook()["sheet"].iter_rows())" is "tuple[Cell | MergedCell, ...]"
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:8:17 - information: Type of "Workbook()["sheet"].iter_rows(values_only=True)" is "Generator[tuple[str | float | datetime | None, ...], None, None]"
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:9:17 - information: Type of "next(Workbook()["sheet"].iter_rows(values_only=True))" is "tuple[str | float | datetime | None, ...]"
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:10:17 - information: Type of "Workbook()["sheet"].iter_rows(values_only=unknown_values_only)" is "Generator[tuple[Cell | MergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]"
      c:\Users\Avasam\Desktop\openpyxl_type_test.py:11:17 - information: Type of "next(Workbook()["sheet"].iter_rows(values_only=unknown_values_only))" is "tuple[Cell | MergedCell, ...] | tuple[str | float | datetime | None, ...]"
    0 errors, 0 warnings, 6 informations
    

    I do not understand why a Generator is not considered as SupportsNext

    The error seem to imply that the generic type of the expected SupportsNext is what's mismatched (notice how in your logs, there's no MergedCell as part of the expected type).

    My guess would be that you are assigning to a predefined value that doesn't match the inferred return type of next. But it's hard to say without a complete reproducer.

  3. mutricyl commented on Nov 29, 2024

    @mutricyl
    Author

    (notice how in your logs, there's no MergedCell as part of the expected type)

    I got the point ! I spent some time replicating the issue and there is a replicable example:

    import datetime
    from typing import TypeAlias, Union
    
    import openpyxl
    from openpyxl.workbook import Workbook
    from typing_extensions import reveal_type
    
    wb = Workbook()
    
    openpyxl_value_types: TypeAlias = Union[str, float, datetime.datetime, None,
                                            int]
    openpyxl_types: TypeAlias = Union[openpyxl_value_types,
                                      openpyxl.cell.cell.Cell]
    
    
    def test(value: bool, sheet: str) -> tuple[openpyxl_types, ...]:
        wb = Workbook()
        reveal_type(next(wb[sheet].iter_rows(values_only=value)))
        return next(wb[sheet].iter_rows(values_only=value))

    So I bascially miss the MergedCell in my return type...

    Thanks for the help @Avasam , I'll close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions