Skip to content

[typing] Add missing _from_iterable classmethod to Set ABCs#15331

Open
emmanuel-ferdman wants to merge 3 commits intopython:mainfrom
emmanuel-ferdman:main
Open

[typing] Add missing _from_iterable classmethod to Set ABCs#15331
emmanuel-ferdman wants to merge 3 commits intopython:mainfrom
emmanuel-ferdman:main

Conversation

@emmanuel-ferdman
Copy link
Contributor

@emmanuel-ferdman emmanuel-ferdman commented Jan 27, 2026

PR Summary

This PR adds _from_iterable to AbstractSet (returning AbstractSet[_S] to preserve element types while allowing subclasses that return set), and to KeysView/ItemsView which override it to return set[_S]. MutableSet inherits from AbstractSet so no separate definition is needed.

Fixes #15298.

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@emmanuel-ferdman
Copy link
Contributor Author

Hey @srittau could you please review this small PR when you get the chance? thank you!

class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
def __init__(self, mapping: SupportsGetItemViewable[_KT_co, _VT_co]) -> None: ... # undocumented
@classmethod
def _from_iterable(cls, it: Iterable[Any]) -> set[Any]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not def [S](cls, it: Iterable[S]) -> set[S] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, updated to use Iterable[_S] -> set[_S]. Used the existing _S TypeVar since PEP 695 syntax isn't used elsewhere in typeshed.

def _hash(self) -> int: ...
# Mixin methods
@classmethod
def _from_iterable(cls, it: Iterable[Any]) -> AbstractSet[Any]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

This should return Self, not any AbstractSet, since the implementation is return cls(it) and other methods on MutableSet that rely on this are annotated to return Self.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to AbstractSet[_S] for type preservation, but can't use Self here - KeysView and ItemsView override this to return set, not Self. Using Self on the base would make those overrides invalid. Also MutableSet.__ior__/__iand__ etc. don't actually call _from_iterable, they mutate in-place directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, the type we want here is more like def [S](cls, Iterable[S]) -> Self & AbstractSet[S], which is not expressible at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

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

So -> AbstractSet[S] seems like the best approximation atm.

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
@emmanuel-ferdman
Copy link
Contributor Author

The failing test is unrelated to this PR - Python 3.9 support is EOLd I guess.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

discord.py (https://github.com/Rapptz/discord.py)
- ...typeshed_to_test/stdlib/typing.pyi:1049: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1055: note: "update" of "TypedDict" defined here

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Feb 7, 2026

Btw. in the original issue I posted a comment that this is probably a bad idea at the moment -- sorry I didn't mention it here earlier, I didn't realize this was a different account than the OP of #15298

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.

collections.abc.Set, etc., missing _from_iterable() classmethod

2 participants