Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commented Apr 9, 2023

This backports the CPython PR python/cpython#26067, which was backported to Python 3.10 but no earlier in the CPython repo.

(It was initially backported to 3.9, but the backport was reverted: python/cpython#26077.)

Since we're now backporting a bugfix to <=3.9, this means that we re-implement Protocol/runtime_checkable on <=3.9, rather than on just 3.7. Doing this requires a change to _get_protocol_attrs; I also tweaked typing_extensions.runtime_checkable so that it also works on typing.Protocol, if typing.Protocol exists.

@AlexWaygoodAlexWaygood marked this pull request as draft April 9, 2023 21:34
@AlexWaygood
Copy link
MemberAuthor

Not sure why that test is failing only on 3.9; it's a bit weird. I'll try to take another look tomorrow, but any suggestions are welcome :)

@AlexWaygoodAlexWaygood marked this pull request as ready for review April 10, 2023 12:16
@AlexWaygood
Copy link
MemberAuthor

Not sure why that test is failing only on 3.9; it's a bit weird.

I figured it out -- __class_getitem__ was being counted as a protocol member on Python 3.9. All green now!

@srittau
Copy link
Collaborator

Sorry for being a pain, but could we split out the flake8 changes for a cleaner history?

@AlexWaygoodAlexWaygood mentioned this pull request Apr 12, 2023
@AlexWaygood
Copy link
MemberAuthor

Sorry for being a pain, but could we split out the flake8 changes for a cleaner history?

Copy link
Collaborator

@srittausrittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding of typing internals is limited, but this LGTM, apart from the comment about flake8 and a question below.

returnNotImplemented
ifnotgetattr(cls, '_is_runtime_protocol', False):
if_caller(depth=3) in{'abc', 'functools'}:
if_allow_reckless_class_checks():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct that the depth is changed from 3 to 4?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because by adding a new function that the code has to pass through before it gets to the sys._getframe call, the call stack becomes "another frame deep"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just have looked at _caller and figured that out myself. 🤦

" @runtime protocols")
ifnot_is_callable_members_only(cls):
if_caller(depth=3) in{'abc', 'functools'}:
if_allow_reckless_class_checks():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@srittausrittau merged commit 7e998c2 into python:mainApr 12, 2023
@AlexWaygoodAlexWaygood deleted the backport-26067 branch April 12, 2023 08:45
@AlexWaygood
Copy link
MemberAuthor

Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@AlexWaygood@srittau