Skip to content

Introduce an Intersection #213

Description

@ilevkivskyi

This question has already been discussed in #18 long time ago, but now I stumble on this in a practical question: How to annotate something that subclasses two ABC's. Currently, a workaround is to introduce a "mix" class:

fromtypingimportIterable, ContainerclassIterableContainer(Iterable[int], Container[int]):
...
deff(x: IterableContainer) ->None: ...
classTest(IterableContainer):
def__iter__(self): ...
def__contains__(self, item: int) ->bool: ...
f(Test())

but mypy complains about this

error: Argument 1 of "__contains__" incompatible with supertype "Container"

But then I have found this code snippet in #18

defassertIn(item: T, thing: Intersection[Iterable[T], Container[T]]) ->None:
ifitemnotinthing:
# Debug outputforitinthing:
print(it)

Which is exactly what I want, and it is also much cleaner than introducing an auxiliary "mix" class. Maybe then introducing Intersection is a good idea, @JukkaL is it easy to implement it in mypy?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions