-
Notifications
You must be signed in to change notification settings - Fork 237
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
Shorter syntax for Optional[...] #429
Comments
FWIW most modern languages that support optional/non-nullable types use |
One alternative in the line of The same could be applied with tuples of types (round brackets) or sets of types (curly brackets). |
I don't like the list form |
So here are the options proposed so far (that I am aware of).
Note that all but the first two are general to any union type and not specific to a union with |
In general I don't think the "verbosity" is a problem, I have worked with |
I can't seem to see where the Here are my two cents on the issue: the current syntax is a little more explicit, so it's harder to miss. However, if we go for a less verbose syntax, people will get used to it pretty fast and will not miss the optional types. At the same time, I think it's okay to keep the current syntax. If you're just starting to use types, then the explicit syntax is much easier to understand whereas any other syntax takes more time getting used to. |
I'm +1 on either str? or ?str. Very short and obvious. |
Maybe we should use `?str?` or `¿str?`.
|
I am -1 on either I have a few ideas to throw out (listing Jukka's first as I like
I think of all of these |
@ethanhs |
What is actually wrong with I think we ought to establish what problem we are solving before we attempt to solve it. |
My main problem with Optional[Callable[[MethodSigContext], CallableType]] If the return type would be
This doesn't look very readable to me, though part of this because of Here's the same type using
I don't like this, since it's hard to see that the Here's with
This is pretty readable, but the Here's the example using
It doesn't strike me as a significant win over |
The verbosity (and nesting of brackets) is one of the drawbacks, but I also like the chance to remove the confusion on Something that could be done from a styleguide standpoint is use the
which I think makes it more visible which parts are nullable, and IMO is more readable then the current syntax given the removed nesting |
It seems unavoidable that complex types will be lengthy. But longer does not necessarily mean harder to read. I don't see that any of the suggestions are significantly more readable, and most aren't really any shorter apart from |
Regarding @dmoisset point that from typing import Optional as NoneOr
x : NoneOr[str] |
@markshannon I could do renames like that in my own code, but "personal" approaches are not useful on annotations, because those are essentially placed on interfaces. I would make my annotations harder to read for third parties using my library unless I'm using a "standard" alias, so that's why it makes sense to discuss this change as part of the standard. |
Supporting Incidentally oftentimes my first two imports in a file are import typing as t
# and now
from typing import Optional as Op |
With PEP 604 now accepted and slated for Python 3.10, we can just say If we were to adopt It's also easier to backport |
Is PEP 604 the kind of thing that can go in typing-extensions as a backport? |
There's nothing to import (it's just an overloaded operator) -- but type checkers have to support it. It makes sense to me that type checkers could support it even for older Python versions, at least in stub files and under the scope of In earlier Python versions (even Python 2) it could be supported in type comments -- but again we first have to teach the type checkers about it. (I imagine there's a type checker where you have some influence. :-) Once type checkers support it we could start using it in typeshed. |
I agree with ethanhs here. One of the strengths of Python is that it is often read as simple phrases (like ternary expressions, |
Let’s close this issue. Maggie sent het proposal to typing-sig, which is where we discuss these things nowadays. |
For reference, because I had no idea what "to typing-sig" meant: That's a mailinglist (typing-sig@python.org) and the thread mentioned is here: Mailman 3 Update: Work on Optional Syntax PEP - Typing-sig - python.org |
Multiple people have suggested a shorter syntax to replace
Optional[...]
. The current syntax will become more inconvenient if we don't infer optional types fromNone
default values (see #275).Here are various options that I remember seeing proposed:
x?
or?x
This would require new Python syntax.
Hack uses
?x
. TypeScript usesx?
for names, not types, if I've understood things correctly.x | None
This would require support for
|
for all type objects, which would not be backward compatible.The
|
operator is used by TypeScript.{x}
Probably not an option as this is too cryptic. Suggested in #151 .
from typing import Optional as O
orfrom typing import Optional as Opt
This is not actually a new syntax and works currently. These are arguably inelegant.
The text was updated successfully, but these errors were encountered: