Skip to content

Implement _from_iterable on builtins.set andbuiltins.frozenset #144557

Closed as not planned

Description

@randolf-scholz

Proposal:

collections.abc.{Set, MutableSet} implement a _from_iterable method, documented here: https://docs.python.org/3/library/collections.abc.html

However, builtins.frozenset and builtins.set, which are marked as inherting from Set and MutableSet, do not implement this constructor, therefore typeshed is kind of blocked from adding these methods in the stubs, as it would lead to unsafe code:

def f(x: MutableSet) -> MutableSet:
    # passes type checking if _from_iterable is added to MutableSet
    return type(x)._from_iterable(x)  

f( {1,2,3} )   # Error at runtime

Alternatives

typeshed could just decide to never add the _from_iterable method, people can still override it regardless to get the desired runtime behavior on types that use the Set and MutableSet mixins that rely on this method.

Has this already been discussed elsewhere?

python/typeshed#15298

Links to previous discussion of this feature:

No response

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 7, 2026
  2. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    pendingThe issue will be closed if no feedback is provided
    and removed
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 7, 2026
  3. picnixz commented on Feb 7, 2026

    @picnixz
    Member

    I am not entirely sure we can do anything here. Changing builtins requires a PEP. If this is for the sake of completeness with typing, I'm not sure it's worth.

  4. randolf-scholz commented on Feb 11, 2026

    @randolf-scholz
    ContributorAuthor

    That's totally fair, but important feedback for typeshed on whether to include this method in the stubs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions