-
Notifications
You must be signed in to change notification settings - Fork 249
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
A Comparable type? #59
Comments
I think that for now the first solution is fine -- Python's rules for what can be compared to what are complex (every type gets to decide for itself) so even the F-bounded nonsense won't capture reality. It looks like we could still add a more complex version in the future since it would involve a new keyword arg to |
Jukka, Mark, and myself believe that it's a good idea to have the generic type def min(x: Comparable, y: Comparable) -> Any:
... |
I was actually arguing for supporting bounds, but not using 'F-bounded polymorphism'. I.e., from typing import TypeVar, Comparable
T = TypeVar('T', bound=Comparable)
def min(x: T, y: T) -> T:
... |
And maybe define
Equality would be inherited from |
Are we going to include |
I would like it yes. |
@JukkaL please tell me what syntax this would have (I'm blanking out on what we decided). |
As far as I can remember, the only presented alternative has been this, and I'm okay with it:
To make it more explicit (but also more verbose and a bit technical sounding), we could use
We could also have
FWIW, Java uses this terminology (upper/lower bound): http://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/TypeVariable.html The example below would be invalid -- you can only have a bound or constraints (not sure if we should come up with a new term, since a bound is also a constraint?), but not both:
|
Hm, I would go with On Wed, May 6, 2015 at 9:11 PM, Jukka Lehtosalo notifications@github.com
--Guido van Rossum (python.org/~guido) |
Okay, let's go with just |
This needs text to be added to the PEP and an implementation for typing.py. |
Updated PEP and typing.py. Now it's up to mypy. |
Hm, but |
The implementation of import typing
from typing import Any
from typing_extensions import Protocol
from abc import abstractmethod
C = typing.TypeVar("C", bound="Comparable")
class Comparable(Protocol):
@abstractmethod
def __eq__(self, other: Any) -> bool:
pass
@abstractmethod
def __lt__(self: C, other: C) -> bool:
pass
def __gt__(self: C, other: C) -> bool:
return (not self < other) and self != other
def __le__(self: C, other: C) -> bool:
return self < other or self == other
def __ge__(self: C, other: C) -> bool:
return (not self < other) |
Hm, since ABCs in @Dentosal |
@Dentosal Thank you for sharing your implementation! 2.5 years later, would you change something (for Python 3.8+)? For example, the |
I don't think we need anything more complicated like the solutions here: python/typing#59 There are some questions about the correctness of other operators like <= and >= that you now get for free with functools.total_ordering, due to the unexpected interactions with the __eq__ you get for free from dataclasses.dataclass, but we can punt them to the future. Signed-off-by: Trishank Karthik Kuppusamy <trishank.kuppusamy@datadoghq.com>
Update timestamp. Also includes: * Minor separation of concerns. * A decent implementation of Comparable. I don't think we need anything more complicated like the solutions here: python/typing#59 There are some questions about the correctness of other operators like <= and >= that you now get for free with functools.total_ordering, due to the unexpected interactions with the __eq__ you get for free from dataclasses.dataclass, but we can punt them to the future. Signed-off-by: Trishank Karthik Kuppusamy <trishank.kuppusamy@datadoghq.com>
I (Guido) wondered how I would compare two arguments whose type is a type variable:
Jukka wrote:
"""
This is a long-standing issue, and we've discussed this before in some detail (e.g., https://github.com/JukkaL/typing/issues/9). However, it wasn't discussed in typehinting, as far as I can tell. Currently we need to use a cast to use < instead of ==, which is pretty ugly.
We could define a Comparable ABC and support that as an "upper bound" for a type variable (bounded polymorphism). About the simplest useful implementation would be like this:
However, this doesn't verify whether the types can be compared to each other, only that they can be compared to something (because of the Any argument type). This feature would be easy to add to mypy.
The way Java etc. do this is to support a fancier language feature called "F-bounded polymorphism" or "F-bounded quantification". With it we can say that int can be compared with only int (Comparable[int]), etc. For example:
This would be more involved to add to mypy. It would probably be one or two days of work for a minimal implementation. I'm not even quite sure that the above would be sufficiently general (Comparable should be contravariant, I think, so that Comparable[object] is a subtype of Comparable[int] :-/ ).
Comparable is probably the only common use case for this complex feature.
"""
The text was updated successfully, but these errors were encountered: