-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Representation of ParamSpecs at runtime compared to Callable #102615
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
Comments
Just clarifying for anyone who plans to contribute: underneath both |
No, they are different:
I've sent a PR with the |
Oh woops, I'm wrong then :). That's a bug. The tuples should be flattened and they should be the same. I remember trying to flatten all the args representations when I implemented PEP 612.
There's a very important reason why they need to be flattened -- generic expects a list of types in E.g this works.
But ugh these two are broken
|
Yeah, I was not sure if this is intentional or a bug. Got it now! |
After reading quite a lot of tests, I think that >>> MyCallable[[int, str], bool].__args__
((<class 'int'>, <class 'str'>), <class 'bool'>) is correct. For example, take a look at this test: def test_multiple_paramspecs_in_user_generics(self):
P = ParamSpec("P")
P2 = ParamSpec("P2")
class X(Generic[P, P2]):
f: Callable[P, int]
g: Callable[P2, str]
G1 = X[[int, str], [bytes]]
G2 = X[[int], [str, bytes]]
self.assertNotEqual(G1, G2)
self.assertEqual(G1.__args__, ((int, str), (bytes,)))
self.assertEqual(G2.__args__, ((int,), (str, bytes))) If we are going to flatten So, I think it is intentional: args are not flattened for ParamSpec substitution. @Fidget-Spinner, however, your examples are broken indeed. |
Thanks for looking into this. I vaguely recall that for user defined generics with ParamSpec, we don't flatten because of the following:
The subsitution of the above, if flattened, would be impossible to reconstruct with I need to dig through the history. I think I left a comment somewhere about this. |
I think the only thing that needs fixing here is the repr and |
…ble` and custom generics with `ParamSpec`
Are these two examples valid uses of ParamSpec? I thought you had to use So surely the runtime is correct in disallowing both of these: they both involve a ParamSpec being substituted with an invalid parameters list? |
@AlexWaygood thanks. Sorry I recalled wrongly the semantics of ParamSpec. I now see that PEP 612 forbids these uses. So does this mean the actual fix is to a. disallow it in typing (because currently only collections.abc and typing.Generic correctly fails) OR which do you prefer? @sobolevn I'm sorry for leading you on a wild goose chase. Thank you for your efforts in all of this. Sorry for wasting your time. In the end, the right fix might truly be just to fix the repr. I think I'll wait for a week or so to see the consensus on this thread. TLDR: I'm wrong and I think Alex is right. Nikita has spent a great deal of effort on understanding this issue. So I'll defer judgement to both Nikita and Alex, I think I'm too rusty with typing right now to make the right call. |
@Fidget-Spinner I don't think you've got anything to apologise for — the spec is horrendously complicated for PEP-612! I only spotted the error on, like, the 5th read :) My top priority here would be improving the error message so that it's clearer for users (including typing maintainers, including me!) exactly why the runtime is raising a TypeError here. It's too easy to forget the finer points of PEP-612. In terms of harmonising the behaviour between |
@Fidget-Spinner no worries, I've spent my time on a very interesting topic :) I now agree that there are several related, but distinct problems:
So, my plan is:
Please, bare with me and my multiple PRs 😆 Thanks everyone for the productive discussion! |
Oh, and better errors (if we can) as |
The original issue here has been fixed, and we won't be backporting the fix (see #102637 (review) for the rationale). As such, I'm going to close this issue and open followup issues for the other problems we identified in this thread. @sobolevn has already opened the first followup issue: |
…python#102637) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…python#102637) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Originally posted by @JelleZijlstra in python/typing#1274 (comment)
Linked PRs
list
instead oftuple
inrepr
of paramspec #102637collections.abc.Callable
and custom generics withParamSpec
#102681The text was updated successfully, but these errors were encountered: