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
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.5,000 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
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?
Comments (0)
No comments yet. Start the discussion.