-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce an Intersection #213
Comments
Mypy complains about your code because I'm worried that intersection types would be tricky to implement in mypy, though conceptually it should be feasible. I'd prefer supporting structural subtyping / protocols -- they would support your use case, as
|
It would be really cool to implement protocols. Still, in this case intersection could be added as a "syntactic sugar", since there would be a certain asymmetry: Assume you want a type alias for something that implements either protocol, then you write: class _Intersection:
def __getitem__(self, bases):
full_bases = bases+(Protocol,)
class Inter(*full_bases): ...
return Inter
Intersection = _Intersection() then one could write: |
|
I understand what you mean. That could be indeed tricky in general case. Concerning protocols, I think structural subtyping would be quite natural for Python users, but only practice could show whether it will be useful. I think it will be useful. |
This keeps coming up, in particular when people have code that they want to support both sets and sequences -- there is no good common type, and many people believe Iterable is the solution, but it isn't (it doesn't support |
I think I don't think that one needs to choose between protocols and It is easy to add |
For cross-reference from #2702, this would be useful for type variables, e.g. |
|
If we had an intersection class in
|
As a data point, I first looked for |
Just as a random idea I was thinking about T = TypeVar('T')
CBack = Optional[Callable[[T], None]]
def process(data: bytes, on_error: CBack[bytes]) -> None:
... |
I just opened #483 hoping for exactly the same thing. I literally named it the same. I would be all for |
Requests for |
I think we should note the use cases but not act on it immediately -- there
are other tasks that IMO are more important.
|
OK, I will focus now on PEP 560 plus related changes to Btw, looking at the milestone "PEP 484 finalization", there are two important issues that need to be fixed soon: #208 ( |
I agree that now's not the right time to add intersection types, but it may make sense later. |
(been redirected here from the mailing list) I think the Intersection[Any, Not[None]] Would mean anything but |
How about the expression example:
|
@rnarkk these have already been proposed many times, but have not been accepted. |
The What's the use case for that? It's not something I've ever missed in a medium-sized typed codebase at work and in lots of work on typeshed. |
This comment was marked as resolved.
This comment was marked as resolved.
The specification was moved: https://github.com/CarliJoy/intersection_examples/blob/main/docs/specification.rst |
Can I ask why this has not been implemented yet? What is the problem with the Intersection concept? |
@ViktorSky if your question is about a specific type checker, you might want to open a bug on their bug tracker instead. Otherwise, the short answer is that there is no PEP/specification ready for type checkers to adopt. |
@ViktorSky well the devil is in the details. Currently nobody was able to create PEP with a sound definition of Intersections within the existing python typing system. Especially handling Intersections with If you want to dig into details see CarliJoy/intersection_examples#38 and the other still open issues. A current draft is discussed here. You are invited to contribute there. BTW: It turned out that github is a rather poor system to discuss / keep track of issue as it misses quite a lot of basic features like answering in threads, adding related issues/block etc. |
Thanks @CarliJoy - yeah it turns out there's a lot of considerations to developing a fully fledged PEP - at the moment the discussion has been quite focused on whether to go for an ordered or unordered variant, as there are pros and cons to each. Please anyone feel free to have a review of the PR - there's been some discussion about whether we should go back to the unordered approach, but we don't have a PEP for this yet. |
Thank you very much, I will be looking for what could be done to solve intersections with |
Switch back to protocols such that we can require multiple protocols (as long as there are no [intersection types](python/typing#213) and `TypeVar`s cannot have multiple bounds).
Switch back to protocols such that we can require multiple protocols (as long as there are no [intersection types](python/typing#213) and `TypeVar`s cannot have multiple bounds).
@mark-todd , ordered sounds far too easy to make mistakes with. What benefits does it bring? |
@tylerlaprade pls follow the discussions in this repo. This issue is not suitable for further discussions. Don't post here unless there is general progress in the intersection. In short the ordered nature was thought about while handling the |
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:
but mypy complains about this
But then I have found this code snippet in #18
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?The text was updated successfully, but these errors were encountered: