FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[typing] Add missing `_from_iterable` classmethod to `Set` ABCs by emmanuel-ferdman · Pull Request #15331 · python/typeshed · GitHub

Repository navigation

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

Merged
srittau merged 4 commits into
python:mainfrom
emmanuel-ferdman:main
Feb 12, 2026
Merged

srittau merged 4 commits into
python:mainfrom
emmanuel-ferdman:main

Conversation

emmanuel-ferdman commented Jan 27, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

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>

This comment has been minimized.

This comment has been minimized.

Comment thread stdlib/typing.pyi Outdated
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread stdlib/typing.pyi Outdated
Comment thread stdlib/typing.pyi Outdated
def _hash(self) -> int: ...
# Mixin methods
@classmethod
def _from_iterable(cls, it: Iterable[Any]) -> AbstractSet[Any]: ...

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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

This comment has been minimized.

randolf-scholz commented Feb 7, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

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

randolf-scholz commented Feb 12, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

So, cpython will not support _from_iterable on builtins.set. (python/cpython#144557)

Therefore, the best we can do is put that definition into a if not TYPE_CHECKING block with a comment explaining the situation

Comment thread stdlib/typing.pyi
Comment on lines +707 to +708
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ...

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ...
if not TYPE_CHECKING:
# this method exists on collections.abc.Set, but since it is not
# supported by builtins.set, we exclude it from static typing
# see: cpython/issues/144557 and typeshed/issues/15298
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ...

Comment thread stdlib/typing.pyi
Comment on lines +741 to +742
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...
if not TYPE_CHECKING:
# this method exists on collections.abc.ItemsView, but since it is not
# supported by dict_items, we exclude it from static typing
# see: cpython/issues/144557 and typeshed/issues/15298
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...

Comment thread stdlib/typing.pyi
Comment on lines +756 to +757
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...
if not TYPE_CHECKING:
# this method exists on collections.abc.KeysView, but since it is not
# supported by dict_keys, we exclude it from static typing
# see: cpython/issues/144557 and typeshed/issues/15298
@classmethod
def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ...

srittau commented Feb 12, 2026

Copy link
Copy Markdown
Collaborator

Therefore, the best we can do is put that definition into a if not TYPE_CHECKING block with a comment explaining the situation

I don't think we should start putting TYPE_CHECKING checks into stub files. I'd rather have these methods not in the stubs than introducing such an idiom in stub files. But as I mentioned in #15298, I'd rather add these methods unconditionally.

Copy link
Copy Markdown
Contributor

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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks!

srittau merged commit a19c15d into python:main Feb 12, 2026
51 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

3 participants


Back | FazBrowse Home | New Git URL