Skip to content

[multiprocessing] ShareableList is iterable but has no __iter__ - #16844

Merged
srittau merged 2 commits into
python:mainfrom
vmphase:fix/shareablelist-iter
Nov 16, 2026
Merged

srittau merged 2 commits into
python:mainfrom
vmphase:fix/shareablelist-iter

Conversation

@vmphase

@vmphase vmphase commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

ShareableList is iterable at runtime (via __getitem__). However, the stub has no __iter__, so type checkers reject it as not iterable and it fails the Iterable protocol.

@vmphase

vmphase commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Anyway, I am sorry for the amount of PRs lately but this is what I stumble upon while refactoring a legacy project :)

@srittau

srittau commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Type checkers should implement special handling for this. Do you have a concrete type checker/code combo where this fails?

@github-actions

Copy link
Copy Markdown
Contributor

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

@vmphase

vmphase commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Type checkers should implement special handling for this. Do you have a concrete type checker/code combo where this fails?

Special handling in the checkers could perhaps cover for x in sl 🤔 ; not sure how could they cover the need of an Iterable protocol.

Nevertheless, I face the problem when computing stats over sensor readings kept in shared memory:

readings = ShareableList([0.5, 0.7, 0.6])
mean = statistics.fmean(readings)

(based)pyright / mypy report:

error: Argument of type "ShareableList[float]" cannot be assigned to parameter "data" of type "Iterable[SupportsFloat]" in function "fmean"
error: Argument 1 to "fmean" has incompatible type "ShareableList[float]"; expected "Iterable[SupportsFloat]"  [arg-type]

It works on runtime, but can't satisfy anything that takes an Iterable protoco, like fmean(), max(), sorted(), etc.

@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.

It seems we already do this in a few other cases.

@srittau
srittau merged commit ec94fbc into python:main Nov 16, 2026
88 checks passed
@vmphase

vmphase commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

It seems we already do this in a few other cases.

Yes, at least in mmap.mmap 👍

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.

2 participants