-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PEP 677: Readability and organizational edits #2193
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
Conversation
Going with Pradeep's suggested order: - untyped example - correct types - partially-typed code - our proposal
pradeep90
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High-level: Wording changes look good. Can do a more detailed review later on.
However, I have some major readability concerns about -> in callable types being ambiguous with the -> from the function signature. See comment below.
| work as a drop-in replacement. | ||
|
|
||
|
|
||
| Motivation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High-level: Motivation is much clearer to read!
|
|
||
| With our proposal, the example looks like this:: | ||
|
|
||
| def flat_map(l: list[int], func: (int) -> list[int]) -> list[int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum @stroxler Wow, I'm having serious second thoughts about the readability of (int) -> str given that Python's def signatures also use ->. I had to read the above signature multiple times to follow it.
TypeScript, etc. don't have this problem because their function signatures don't use an arrow for the return type; they use a colon ::
function flat_map(xs: number[], f: (x: number) => number[]): number[] { }I think our example is actually a bit more readable with =>:
def flat_map(l: list[int], func: (int) => list[int]) -> list[int]:We no longer have to mentally disambiguate the arrow for the callable and the arrow for the def.
For completeness, here are all the other examples from this PEP:
With a dash arrow:
def with_retries(f: (**P) -> R) -> (bool, **P) -> R: ...
def customize_response(
response: Response,
customizer: async (Response, list[UserSetting]) -> Response,
) -> Response: ...
# the above example as a one-liner:
def foo(resp: Response, func: async (Response, list[Setting]) -> Response) -> Response: ...
def f() -> (int, str) -> bool: pass
def f() -> (int) -> (str) -> bool: pass
def f() -> async (int) -> async (str) -> bool: passWith an equals arrow:
def with_retries(f: (**P) => R) -> (bool, **P) => R: ...
def customize_response(
response: Response,
customizer: async (Response, list[UserSetting]) => Response,
) -> Response: ...
# the above example as a one-liner:
def foo(resp: Response, func: async (Response, list[Setting]) => Response) -> Response: ...
def f() -> (int, str) => bool: pass
def f() -> (int) => (str) => bool: pass
def f() -> async (int) => async (str) => bool: passWhat do you think? This might remove a lot of the ambiguity when reading or writing callback types. The downside is that it doesn't exactly resemble function arrows, which might be fine, since it doesn't have named parameters either.
If this seems reasonable, we could open it up for discussion (or do that in parallel with python-dev review).
Another option is to rewrite a few typeshed files with both styles to let people get a feel for it in real-world code. Until now, we were just going off the principle that "it should look like function signatures", which it turns out can actually hurt readability.
|
@pradeep90, your comment about readability of |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
|
@gvanrossum Bikeshedding can get messy and heated (as we have seen on this PEP 😄 ). It seems risky to do it for the first time on python-dev. I didn't want our main question about Python syntax changes to be derailed by bikeshedding over the arrow token. I thought it might be better to reach consensus on typing-sig and iron out any concerns before opening it up on python-dev. But we can raise it on python-dev if you feel strongly. |
|
I'm not opposed to changing to My personal on this is that
|
|
It seems like we'll stick with |
Almost nothing material to the proposal itself is changing, the only specification tweak is that we updated the grammar snippet for supporting PEP 646 to include a guard against
'...'.All other edits are just for readability:
This closes all open suggestions from review of the initial PR except that:
Statisticssubsection to theResourcessection and I haven't gotten to that yet