Skip to content

pyopenxl: CellRange: title should not be required #10682

@oefe

Description

@oefe

#10563 introduced type annotations for title in CellRange (thanks!).

Unfortunately, it made it required in one of the overloads.

I don't think title should be required.

Discussion

A comment in the stub provides a justification for making it required:

    # Could be None if the caller forgot to set title and range_string doesn't contain "!"
    # but that's not intended and will lead to errors (ie: can't be None in __str__)
    title: str

However, __str__ works fine if title is none:

>>> from openpyxl.worksheet.cell_range import CellRange
>>> r = CellRange(min_col=1, min_row=1, max_col=10, max_row=100)
>>> r
<CellRange A1:J100>
>>> str(r)
'A1:J100'

I haven't found any other place in the CellRange source where title = None would be causing problems.
It is apparently treated as equivalent to an empty string.

Example

from openpyxl.worksheet.cell_range import CellRange

cell_range = CellRange(
    min_col=1,
    min_row=1,
    max_col=10,
    max_row=100,
)

This passed all checks with Pyright 1.1.324. But Pyright 1.1.326 (which includes the latest rev of typeshed) reports:

/issues/bug_cellrange_title.py
  /issues/bug_cellrange_title.py:3:14 - error: No overloads for "__init__" match the provided arguments
    Argument types: (Literal[1], Literal[1], Literal[10], Literal[100]) (reportGeneralTypeIssues)

Workaround

Set title to an empty string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions