IssueRaiser

Introduce an Intersection

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: ``` python from typing import Iterable, Container class IterableContainer(Iterable[int], Container[int]): ... def f(x: IterableContainer) -> None: ... class Test(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 ``` python def assertIn(item: T, thing: Intersection[Iterable[T], Container[T]]) -> None: if item not in thing: # Debug output for it in thing: 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?

Posted by ilevkivskyi on May 6, 2016, 9:27 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.5,000 issues0 comments

github (Other)

Introduce an Intersection

ilevkivskyiMay 6, 2016, 9:27 AM

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: ``` python from typing import Iterable, Container class IterableContainer(Iterable[int], Container[int]): ... def f(x: IterableContainer) -> None: ... class Test(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 ``` python def assertIn(item: T, thing: Intersection[Iterable[T], Container[T]]) -> None: if item not in thing: # Debug output for it in thing: 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?

Comments (0)

No comments yet. Start the discussion.

Comment on this issue