Skip to content

[openpyxl] Fix stubs for openpyxl.descriptors.sequence.UniqueSequence.seq_types - #14725

Merged
srittau merged 2 commits into
python:mainfrom
JanEricNitschke:fix-openpyxl-seq-types
Sep 18, 2025
Merged

srittau merged 2 commits into
python:mainfrom
JanEricNitschke:fix-openpyxl-seq-types

Conversation

@JanEricNitschke

@JanEricNitschke JanEricNitschke commented Sep 15, 2025 •

Copy link
Copy Markdown
Contributor

Linking #14718 for visibility there

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

# `_T` is the type of the elements in the sequence.
class UniqueSequence(Sequence[set[_T]]):
seq_types: tuple[type[list[_T]], type[tuple[_T, ...]], type[set[_T]]]
seq_types: tuple[type, ...] # defaults to `list`, `tuple`, `set`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just commented on the issue, but I think this makes the stub worse and it's better to ignore the error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should this be adjusted in Sequence then as well? And i think in principle this can be set to anything though, right?

UniqueSequence just includes a set in the default?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also think that the stubs in this particular case are best left unchanged, since any modification would reduce accuracy. It should be sufficient to change comment in stubtest_allowlist.txt entry explaining why we decided not to adjust these types."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As example (on pyright playgroun):

stuff1 = UniqueSequenceOld[int](seq_types=(list, tuple, set, frozenset), container=frozenset)
stuff2 = UniqueSequenceNew[int](seq_types=(list, tuple, set, frozenset), container=frozenset)

I think this is valid at runtime. But to allow it we would need to have unique sequence as:

class UniqueSequence(Sequence[[_ContainerT]]):
    seq_types: tuple[type, ...]
    container: type

which i guess feels weird?

So this would be a "useability vs purity" thing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I didn't realize the types could be overwritten. If so, using tuple[type, ...] does make more sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think so at the very least:
https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/default/openpyxl/descriptors/base.py#L17
https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/default/openpyxl/descriptors/sequence.py#L11

I think "UniqueSequence" just changes the defaults. So changing them would definitely make them more correct in the general sense i think. But it would also make them pretty useless in the (common?) case where you keep the defaults.

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@srittau
srittau merged commit e129873 into python:main Sep 18, 2025
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants