-
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
Shorthand for union/subclassing #387
Comments
There was a similar discussion #211 where a short notation for variance was proposed as class MyClass(Generic[-T, U<Comparable, +S], Mapping[T, int], Set[S]):
def my_method(self, lst: List[T]) -> S:
... I like such syntax in principle but there are some problems:
|
Also, I haven't seen frequent use of upper bounds or co/contravariant type variables in annotated code. If these features are only used occasionally, being easy to google is more important than brevity. Can you give some examples of use cases where these come up frequently? If you use union types a lot, type aliases may help. |
Upper bounds come up a bit when using generic types with subclasses, but it's the union types that show up often enough that it gets really irritating to have to define up a type alias each time. Furthermore this clearly doesn't scale--it'd require me to implement a type alias for every possible combination of types ;) Personally, I find the infix notation more consistent and easier to understand than the Lisp-y prefix stuff, and we already have convenience operators for sets. I don't particularly care about the covariant and contravariant markers since they are quite rare. Using The "extremely difficult to Google" bit I don't buy since TypeScript and Flow use them heavily, and I have never encountered that as a complaint. It certainly doesn't stop the use of google-unfriendly operators in python code! |
There are generic aliases, you could write: Vec = Iterable[Tuple[T, T]]
v: Vec[int] # Same as Iterable[Tuple[int, int]]
UInt = Union[List[T], int]
data: UInt[bytes] # Same as Union[List[bytes], int]
fancy: Vec[UInt[str]] # I am scared to expand this etc.
And this is the hardest part, |
For the kind of code I've worked on, needing to import |
Hmm, that would be irritating. What's the technical issue that means it breaks compatibility? Anyway, I ran into another issue where having union types comes up often in my codebase: Numpy array vs Pandas DataFrame. |
@nimish PEP 484 related changes to Python are currently localized to the A technically feasible approach for supporting Special case For example, the annotation object for This is sufficiently magical that it might be hard to get this accepted to Python 3.7, though. |
There is one more way of shortening union types that already works: import ... as. Example:
This even looks a bit like the mathematical symbol for union. You can also use |
Why not use It is very fast, it is perfectly clear what it means, and static checkers should have no trouble parsing it. |
This is an interesting idea, |
Let's not do this. |
To avoid unnecessary issues, let me ask here: what is current proper way to compare two E.g. how to compare |
@Pitometsu If you mean a runtime subclass check then this is not supported. There are several runtime type checking libraries like After all, typing is designed primarily to be used with static type checkers like |
@ilevkivskyi, thank you for answer. Well, I need it for own DSL implementation (and yes, I use |
Unfortunately, there are none, you would need to implement your own. |
Hi,
I searched and couldn't find a discussion on this:
Currently the syntax for declaring intersections and type upper bounds is verbose and unwieldy, for someone like me who is annotating a large codebase:
Union[Foo, Bar, Baz....]
andTypeVar('T', bound=SuperType).
In comparison, TypeScript and Flow both allow for using
|
; other languages use<
for upper bounds.If we view types as sets, it is surprising that although sets have overloaded operators, types don't--we have to use a very verbose syntax for declaring types, when we have a perfectly good one: Python itself.
Could someone point me to any previous discussion on this?
The text was updated successfully, but these errors were encountered: