This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

classification
Title: `Concatenate` should not raise any semantic errors
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, gvanrossum, kj, sobolevn
Priority: normal Keywords: patch

Created on 2022-01-16 08:38 by sobolevn, last changed 2022-01-25 15:16 by gvanrossum. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30619 closed sobolevn, 2022-01-16 08:46
Messages (10)
msg410682 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-16 08:38
Right now if I remove these lines:

```python
    if parameters == ():
        raise TypeError("Cannot take a Concatenate of no types.")
    if not isinstance(parameters, tuple):
        parameters = (parameters,)
    if not isinstance(parameters[-1], ParamSpec):
        raise TypeError("The last parameter to Concatenate should be a "
                        "ParamSpec variable.")
```

from here: https://github.com/python/cpython/blob/09087b8519316608b85131ee7455b664c00c38d2/Lib/typing.py#L601-L607

And run the test suite - it passes.

To increase coverage I plan to:
1. Add a test for empty `Concatenate[]`
2. Add a test case when `ParamSpec` is not the last item of `Concatenate`
3. When passed parameter to `Concatenate` is not `tuple`

PR is on its way :)
msg411189 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-21 21:37
Arguably the runtime should not check for incorrect calls to Concatenate. That's the job of static type checkers. So maybe that code could just be removed? (We don't bother checking things like list[0] either.)
msg411238 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-22 09:08
> So maybe that code could just be removed?

I see this trend in typing.py :)

I think it makes sense to make all new types (I still consider `ParamSpec` and `Concatenate` as new, desipte the fact they are already released in 3.10) as flexible as possible.

I will change my PR.
msg411240 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-22 09:30
With this change `typing.py` and `typing_extensions.py` will have different logic: https://github.com/python/typing/blob/523cf0233edc7a29502fbd30dc6bf641a7408e16/typing_extensions/src/typing_extensions.py#L1805-L1811 

Jelle, what should we do?
msg411259 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-01-22 15:18
Let's change typing_extensions.py too in the same way.

Speaking of typing-extensions, it'd be great if someone could review https://github.com/python/typing/pull/963 :)
msg411472 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-24 13:22
PR is updated, now `Concatenate` does not raise any semantic errors: https://github.com/python/cpython/pull/30619
msg411486 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2022-01-24 16:04
I'm a little conflicted on this specific use case (but maybe I'm biased because I wrote the checks :).

I'm a strong proponent of less runtime checks. However, in this case, the exceptions serve as easy hints to the user, and they're meant to catch the most basic of errors. At the same time they barely add any overhead to our code.

Also, the last check may be required so that we can easily support subscripts (due to runtime quirks). I vaguely remember Serhiy mentioning that the semantics for subscripting a Callable[Concatenate[...], ...] is undefined by PEP 612. We were planning to formalize/reject the behavior, but I dropped the ball on that due to a lack of time. If that materializes, we're gonna raise an unclear runtime error about not enough typevars at subscript time anyways, so we might as well clearly tell the user at the start to prevent confusion.
msg411494 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-24 16:48
"When in doubt, the status quo wins."

Maybe we should just close this and the PR without merging.
msg411590 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-25 13:51
I think that in case when we preserve the runtime checks, they should be covered by tests. Right now - they are not.

So, I will switch my PR to the initial version with a new test case.

Does this sound right? :)
msg411612 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-25 15:16
No, that would make the behavior normative.

Let sleeping dogs lie.
History
Date User Action Args
2022-01-25 15:16:56gvanrossumsetstatus: open -> closed
resolution: wont fix
messages: + msg411612

stage: patch review -> resolved
2022-01-25 13:51:08sobolevnsetmessages: + msg411590
2022-01-24 16:48:53gvanrossumsetmessages: + msg411494
2022-01-24 16:04:20kjsetmessages: + msg411486
2022-01-24 13:22:05sobolevnsetmessages: + msg411472
components: + Library (Lib), - Tests
title: Typing: test invalid usages of `Concatenate` -> `Concatenate` should not raise any semantic errors
2022-01-22 15:18:14JelleZijlstrasetmessages: + msg411259
2022-01-22 09:30:14sobolevnsetnosy: + JelleZijlstra
messages: + msg411240
2022-01-22 09:08:49sobolevnsetmessages: + msg411238
2022-01-21 21:37:39gvanrossumsetmessages: + msg411189
2022-01-21 21:27:11terry.reedysettitle: Invalid usage of `Concatenate` is not covered at all -> Typing: test invalid usages of `Concatenate`
2022-01-16 10:50:45AlexWaygoodsetnosy: + gvanrossum, kj
2022-01-16 08:46:42sobolevnsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28823
2022-01-16 08:38:58sobolevncreate