GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing by Fidget-Spinner · Pull Request #23060 · python/cpython

Conversation

@Fidget-Spinner

@Fidget-Spinner Fidget-Spinner commented Oct 31, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This patch fixes bpo-42195 via changing the GenericAlias builtin and subclassing for collections.abc.Callable's __class_getitem__. The changes are:

  1. Made GenericAlias subclassable.
  2. Subclassed it in collections.abc.Callable's __class_getitem__ to allow for consistentency with typing.Callable's __args__. This includes flattening the __args__.
>>> import collections.abc
>>> alias = collections.abc.Callable[[int, str], dict]

>>> alias
collections.abc.Callable[[int, str], dict]

>>> alias.__args__
(int, str, dict)
  1. Added tests for the above.
  2. Remove bad typing tests which subclassed from Callable[... T].
  3. Loosened type checks for Callable's argtypes in Callable[[argtypes], resulttype] to prepare for PEP 612.

https://bugs.python.org/issue42195

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fidget-Spinner
Fidget-Spinner requested review from gvanrossum, ilevkivskyi and rhettinger as code owners October 31, 2020 12:50
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Oct 31, 2020
@bedevere-bot bedevere-bot added the awaiting review label Oct 31, 2020
gvanrossum
gvanrossum reviewed Nov 16, 2020
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

I suppose it would be even nicer if the subclass was written in C and used by builtins.callable as well? So we can write callable[[int, int], str].

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Nov 16, 2020

Copy link
Copy Markdown
Member Author

I suppose it would be even nicer if the subclass was written in C and used by builtins.callable as well? So we can write callable[[int, int], str].

There is work on that by Shantanu at this bpo and PR #22848.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Nov 16, 2020

Copy link
Copy Markdown
Member Author

For now, I'll wait till we've decided how to go about doing this on the bpo. It seems that there are a few sensible approaches here. Apologies if I don't follow-up to your review that quickly as a result.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 7 commits November 19, 2020 12:47
@Fidget-Spinner
Allow subclassing of GenericAlias, fix collections.abc.Callable's Gen…
2c4a297
…ericAlias
@Fidget-Spinner
fix typing tests, add hash and eq methods
588d421
@Fidget-Spinner
Fix pickling
050fa13
@Fidget-Spinner
whitespace
f60ea8a
@Fidget-Spinner
Add test specifically for bpo
2f3c6dc
@Fidget-Spinner
update error message
2c4508e
@Fidget-Spinner
Appease test_site
19d2973
@Fidget-Spinner
Fidget-Spinner force-pushed the abc-callable-ga branch from dd71133 to 19d2973 Compare November 19, 2020 04:48
@Fidget-Spinner

Fidget-Spinner commented Nov 19, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@gvanrossum, I gave the issue more thought and I'm probably going ahead with the Python implementation since callable genericalias seems contentious. In the future someone else can just replace the Python version with the C version if it comes to fruition.

Supporting __args__ for a Callable[[int, str], dict] as ((int, str), dict) is surprisingly rather inconvenient work. A few typing methods have to become recursive to account for nested tuples of types (mainly the ones that check for TypeVars and replacement of TypeVars). The C counterparts to those methods have to become recursive too. Update: We can represent the internal args as (Tuple[int, str], dict) which avoids the need for recursion.

Although __args__ of ((int, str), dict) is the cleanest IMO, the effort to achieve it versus just making both __args__ (int, str, dict) feels disproportionately high. Maybe this is why the original code author for typing (I'd assume Ivan) made a conscious decision to flatten the args.

Edit: Sorry about the force push, I'm not a git sensei :(.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner
Fidget-Spinner marked this pull request as draft November 19, 2020 09:35
@Fidget-Spinner
Represent Callable __args__ via [tuple[args], result]
3116c8e
@Fidget-Spinner
Fidget-Spinner marked this pull request as ready for review November 19, 2020 16:06
Fidget-Spinner added 4 commits November 20, 2020 01:02
@Fidget-Spinner
add back tests for weakref, styling nits, add news
f2b593a
@Fidget-Spinner
remove redundant tuple checks leftover from old code
93d51e4
@Fidget-Spinner
Use _PosArgs instead of tuple
327e1a5
1.  allow equality between typing._PosArgs and collections.abc._PosArgs
2.  allow equality checks between genericalias subclasses
3.  add support to genericalias subclasses for union type expressions
@Fidget-Spinner
Fix typo and news
e971ccb
@Fidget-Spinner

Fidget-Spinner commented Nov 29, 2020

Copy link
Copy Markdown
Member Author

@gvanrossum tests seem to be passing as of 327e1a5 with the _PosArgs implementation. Needs docs and whatsnew update (I'll do another PR for that if this gets accepted).

Personally, the _PosArgs approach has grown on me quite a bit: it's a nice compromise. I managed to hack things together such that typing._PosArgs[x] == collections.abc._PosArgs[x] too. So typing.Callable[[int], str].__args__ == Callable[[int], str].__args__. (Note that typing.Callable[[int], str] != Callable[[int], str])

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner Fidget-Spinner changed the title bpo-42195: Make GenericAlias subclassable and subclass for collections.abc.Callable bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing Nov 29, 2020
gvanrossum
gvanrossum reviewed Nov 30, 2020
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

Sorry for leaving so many trivial code formatting comments. There are also a few important suggestions.

In general I am still not completely comfortable with this. In particular the change to how typing.Callable changes the form of its __args__ makes me worried about backwards compatibility (that code has been the same for a long time, unlike collections.abc.Callable, which is new in 3.9.0).

I also worry that 3.9.1rc1 is already out, so the earliest we could get this in would be 3.9.2. But if we ever want to fix this I think 3.9.2 is still better than 3.10 (let alone never).

Something makes me think that we're probably better off keeping typing.Callable unchanged and changing collections.abc.Callable so that its __args__ is a flat tuple. The trickery here is quite hard to follow. (Since I procrastinated on the review I feel this all the stronger. :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Core and Builtins/2020-11-20-00-57-47.bpo-42195.HeqcpS.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Nov 30, 2020 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

[me]

Something makes me think that we're probably better off keeping typing.Callable unchanged and changing collections.abc.Callable so that its __args__ is a flat tuple. The trickery here is quite hard to follow. (Since I procrastinated on the review I feel this all the stronger. :-)

However, I also forgot that Shantanu warned in bpo-42195 that PEP 612 allows Callable[P, R] where P is a ParamSpec typevar. This could make a flat tuple ambiguous. (We should continue this discussion at the bpo issue.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 2 commits November 30, 2020 13:26
@Fidget-Spinner
Refactor C code to use less duplication
abd8b98
@Fidget-Spinner
Address most of Guido's reviews (tests failing on purpose)
3ddca06
gvanrossum
gvanrossum reviewed Dec 5, 2020
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

Very close for the Python code. For the C code I am still not sure that this is the best refactoring. Basically we have some shared code (if not tuple, tuplify), then some non-shared code (allocate object), then some more shared code (set attributes), then some non-shared code (gc-track).

I think I may have led you astray by asking to share the code between the two. Or maybe twe could change the order to

  1. allocate
  2. tuplify
  3. set attrs
  4. gc-track

and then only put (2) and (3) in the shared helper function.

What do you think?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/_collections_abc.py

@classmethod
def __getitem_type(cls, origin, args):
def __new__(cls, origin, args):

@gvanrossum gvanrossum Dec 5, 2020

Copy link
Copy Markdown
Member

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.

Okay, for 3.10 I think this is the right solution.

For 3.9, I think we need to issue a DeprecationWarning(using the warnings module) with the same message (for both TypeErrors below) and return what was returned in 3.9.0.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_typing.py

@gvanrossum gvanrossum Dec 5, 2020

Copy link
Copy Markdown
Member

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.

So in the 3.9 backport this file should be untouched.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fidget-Spinner
refactor C code, add deprecation warning for 3.9
a789620
@Fidget-Spinner

Fidget-Spinner commented Dec 6, 2020

Copy link
Copy Markdown
Member Author

I think I may have led you astray by asking to share the code between the two. Or maybe twe could change the order to

  1. allocate
  2. tuplify
  3. set attrs
  4. gc-track

and then only put (2) and (3) in the shared helper function.

What do you think?

Ok, I did just that. This is really helpful and clear feedback. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Dec 6, 2020
View reviewed changes
Comment thread Lib/_collections_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/genericaliasobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/genericaliasobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 2 commits December 6, 2020 14:08
@Fidget-Spinner
remove redundant check in C code, and try except in __new__
1890b37
@Fidget-Spinner
remove check
4e928c6
@Fidget-Spinner Fidget-Spinner mentioned this pull request Dec 8, 2020
bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing #23702
Merged
16 tasks done
@gvanrossum
gvanrossum removed request for ilevkivskyi and rhettinger December 10, 2020 19:50
@gvanrossum

gvanrossum commented Dec 10, 2020

Copy link
Copy Markdown
Member

Hi @Fidget-Spinner, this LGTM, but before I land it I have one question -- how will the PEP 612 implementation affect this? A particular thing to look into would be to see if we would run into trouble if people took this (or the original Callable implementations) and started using it with ParamSpec and Concatenate imported from typing_extensions? I fear that's not going to end well unless we remove most of the type checks. I wonder if we would have to simplify things so that Callable[X, Y] is valid for any X and Y, and falls into two cases:

  • If X is a list, then __args__ becomes `(*X, Y)
  • In all other cases, __args__ becomes (X, Y)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Dec 11, 2020

Copy link
Copy Markdown
Member Author

I wonder if we would have to simplify things so that Callable[X, Y] is valid for any X and Y, and falls into two cases:

  • If X is a list, then __args__ becomes `(*X, Y)
  • In all other cases, __args__ becomes (X, Y)

Yeah definitely agree here. I saw that ParamSpec in pyre_extensions already uses the inheriting from list hack to work with Callable.

So do you want me to do that for both typing and collections.abc, or just collections.abc's Callable?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Dec 11, 2020 via email

Copy link
Copy Markdown
Member
Let's do that for both. This will make the introduction of ParamSpec much simpler.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 2 commits December 12, 2020 00:49
@Fidget-Spinner
Loosen type checks for Callable args, cast to PyObject in genericalia…
6b11d33
…sobject.c
@Fidget-Spinner
update news to mention about removing validation in argtypes
4215c3b
gvanrossum
gvanrossum reviewed Dec 11, 2020
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

I think it's ready except for the commented-out blocks of code.

Also, I'd like to ask @pablogsal to see if he has time to review your C code (he has often caught things I missed in C code!).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner
remove commented out code
585bf19
gvanrossum
gvanrossum approved these changes Dec 12, 2020
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

Actually @pablogsal may be busy, let's see if @brandtbucher is interested.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Dec 12, 2020
@pablogsal

pablogsal commented Dec 12, 2020

Copy link
Copy Markdown
Member

Also, I'd like to ask @pablogsal to see if he has time to review your C code (he has often caught things I missed in C code!).

Sorry for the delay. I can give it a go this night :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal
pablogsal approved these changes Dec 13, 2020
View reviewed changes

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

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.

I have reviewed the C code and it looks good to me! Good job @Fidget-Spinner !

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@Fidget-Spinner

Fidget-Spinner commented Dec 13, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I have reviewed the C code and it looks good to me! Good job @Fidget-Spinner !

Thank you for taking time out of your busy schedules to review this pablo and guido :).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum
gvanrossum merged commit 463c7d3 into python:master Dec 13, 2020
@bedevere-bot bedevere-bot removed the awaiting merge label Dec 13, 2020
@gvanrossum

gvanrossum commented Dec 13, 2020

Copy link
Copy Markdown
Member

Okay, that's 3.10 down. @Fidget-Spinner, can you work on the backport to 3.9? There's one comment in typing.py indicating we need to do something different there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Dec 13, 2020

Copy link
Copy Markdown
Member Author

Okay, that's 3.10 down. @Fidget-Spinner, can you work on the backport to 3.9? There's one comment in typing.py indicating we need to do something different there.

Sure, give me some time :). I'll make another PR to update docs too after the backport lands.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner
Fidget-Spinner deleted the abc-callable-ga branch December 14, 2020 14:49
gvanrossum pushed a commit that referenced this pull request Dec 14, 2020
@Fidget-Spinner
[3.9] bpo-42195: Ensure consistency of Callable's __args__ in collect…
33b3fed
…ions.abc and typing (GH-23765)

Backport of GH-23060.
@Fidget-Spinner Fidget-Spinner mentioned this pull request Dec 27, 2020
bpo-42740: Fix get_args for PEP 585 collections.abc.Callable #23963
Merged
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
@Fidget-Spinner @adorilson
bpo-42195: Ensure consistency of Callable's __args__ in collections.a…
0db4bb8
…bc and typing (pythonGH-23060)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pablogsal pablogsal pablogsal approved these changes
@gvanrossum gvanrossum gvanrossum approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@Fidget-Spinner @gvanrossum @pablogsal @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.