Skip to content

Should collections.abc.Container be generic? #15307

@alanhdu

Description

@alanhdu

This is more of a question, but I am a bit confused about the current definition of Container. Right now, it looks like it is

typeshed/stdlib/typing.pyi

Lines 643 to 647 in a3568ac

@runtime_checkable
class Container(Protocol[_T_co]):
# This is generic more on vibes than anything else
@abstractmethod
def __contains__(self, x: object, /) -> bool: ...

This strikes me as an odd definition though: it has a generic parameter (that AFAIK is totally unused), and requires that __contains__ always takes an object type. I feel like it would make sense to either:

  1. Make Container not generic (since AFAICT Container[str] vs Container[int] has no actual meaning)
  2. Make the type annotation on def __contains__(self, x: _T_co, /) -> bool: ... so that the generic parameter is actually used.

Naively, option 2 seems to be more useful (since you can always have Container[object] if you want) but perhaps I am missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions