[Python-ideas] PEP Idea: Better return type annotation syntax

12 views
Skip to first unread message

Tushar Sadhwani

unread,
Jan 6, 2022, 3:43:56 PMJan 6
to python...@python.org
With the recent submission of [PEP 677][1], I was reminded of an idea I had with function annotation syntax since the very beginning:

why not let me write:

```python
def f() -> tuple[int, str]:
return 42, 'foo'
```

as:

```python
def f() -> (int, str):
return 42, 'foo'
```

Is there any inherent reason for this, other than that it isn't an actual "type"?

I think it looks much cleaner, and if there isn't any drawbacks to adding this syntax, I'd love to work on bringing this to life.

[1]: https://www.python.org/dev/peps/pep-0677/
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/NLZOBGSJT3A7KVSZVIHCQBOGKZ2E7AI2/
Code of Conduct: http://python.org/psf/codeofconduct/

Jelle Zijlstra

unread,
Jan 7, 2022, 1:23:42 AMJan 7
to Tushar Sadhwani, python...@python.org
On Thu, Jan 6, 2022 at 10:44 PM Tushar Sadhwani <tushar.sa...@gmail.com> wrote:
With the recent submission of [PEP 677][1], I was reminded of an idea I had with function annotation syntax since the very beginning:

why not let me write:

```python
def f() -> tuple[int, str]:
    return 42, 'foo'
```

as:

```python
def f() -> (int, str):
    return 42, 'foo'
```

Is there any inherent reason for this, other than that it isn't an actual "type"?
I like this too. A practical issue is that list[(a, b)] and list[a, b] look the same to the compiler, but they would mean very different things. It's not obvious how to fix this in a backward-compatible way.

Tushar Sadhwani

unread,
Jan 16, 2022, 10:36:08 AMJan 16
to python...@python.org
Jelle Zijlstra wrote:
> > I like this too. A practical issue is that list[(a, b)] and list[a, b] look
> the same to the compiler, but they would mean very different things. It's
> not obvious how to fix this in a backward-compatible way.
> > I think it looks much cleaner, and if there isn't any drawbacks to adding
> > this syntax, I'd love to work on bringing this to life.

Restricting it to only the top level of a return type annotation should do the trick!

Such that:

def f() -> (X, Y, Z): ...

is interpreted as tuple[X, Y, Z], but:

def f(x: (X, Y, Z)) -> None: ...
def f() -> Union[int, (X, Y, Z)]: ...
def f() -> list[(X, Y, Z)]: ...

Are not considered as tuple[...]
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/GDTLCSBIGY7W6GNWHUMRGY7KKFDKT3LU/
Reply all
Reply to author
Forward
0 new messages