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 — Backport some recent `Protocol` fixes from 3.12 by AlexWaygood · Pull Request #161 · python/typing_extensions

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented May 19, 2023

Copy link
Copy Markdown
Member

Backports these PRs:

  • gh-104555: Fix isinstance() and issubclass() for runtime-checkable protocols that use PEP 695 cpython#104556
  • gh-104555: Runtime-checkable protocols: Don't let previous calls to isinstance() influence whether issubclass() raises an exception cpython#104559
  • gh-74690: Don't set special protocol attributes on non-protocol subclasses of protocols cpython#104622
  • Improve readability of typing._ProtocolMeta.__instancecheck__ cpython#104649

Sorry, something went wrong.

Uh oh!

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

All reactions
@AlexWaygood
Backport some recent Protocol fixes from 3.12
8fd5626
AlexWaygood
AlexWaygood commented May 19, 2023
View reviewed changes
Comment thread src/test_typing_extensions.py
Comment on lines +2401 to +2438
exec(textwrap.dedent(
"""
def test_pep695_generic_protocol_callable_members(self):
@runtime_checkable
class Foo[T](Protocol):
def meth(self, x: T) -> None: ...

class Bar[T]:
def meth(self, x: T) -> None: ...

self.assertIsInstance(Bar(), Foo)
self.assertIsSubclass(Bar, Foo)

@runtime_checkable
class SupportsTrunc[T](Protocol):
def __trunc__(self) -> T: ...

self.assertIsInstance(0.0, SupportsTrunc)
self.assertIsSubclass(float, SupportsTrunc)

def test_no_weird_caching_with_issubclass_after_isinstance_pep695(self):
@runtime_checkable
class Spam[T](Protocol):
x: T

class Eggs[T]:
def __init__(self, x: T) -> None:
self.x = x

self.assertIsInstance(Eggs(42), Spam)

# gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta,
# TypeError wouldn't be raised here,
# as the cached result of the isinstance() check immediately above
# would mean the issubclass() call would short-circuit
# before we got to the "raise TypeError" line
with self.assertRaises(TypeError):
issubclass(Eggs, Spam)

@AlexWaygood AlexWaygood May 19, 2023

Copy link
Copy Markdown
Member Author

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 these pass, but had no way of checking, since typing_extensions currently fails to import on 3.12:

Traceback (most recent call last):
  File "C:\Users\alexw\coding\typing_extensions\src\test_typing_extensions.py", line 29, in <module>
    import typing_extensions
  File "C:\Users\alexw\coding\typing_extensions\src\typing_extensions.py", line 1361, in <module>
    class TypeVar(typing.TypeVar, _DefaultMixin, _root=True):
TypeError: type 'typing.TypeVar' is not an acceptable base type

Sorry, something went wrong.

Uh oh!

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

All reactions
@AlexWaygood

AlexWaygood commented May 19, 2023

Copy link
Copy Markdown
Member Author

Oh, the PyPy version of ABCMeta results in a bunch of extra attributes being present in a class's __dict__? TIL.

All reactions

Sorry, something went wrong.

Uh oh!

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

@AlexWaygood

AlexWaygood commented May 19, 2023

Copy link
Copy Markdown
Member Author

Oh, the PyPy version of ABCMeta results in a bunch of extra attributes being present in a class's __dict__? TIL.

Ugh, that means we'll probably have to mark the test in CPython as @cpython_only.

All reactions

Sorry, something went wrong.

Uh oh!

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

AlexWaygood added 2 commits May 19, 2023 23:02
@AlexWaygood
Hopefully fix PyPy tests
25e2fe4
@AlexWaygood
bugbear
71032d7
JelleZijlstra
JelleZijlstra approved these changes May 19, 2023
View reviewed changes
@JelleZijlstra
JelleZijlstra merged commit dfe4889 into python:main May 19, 2023
@AlexWaygood
AlexWaygood deleted the proto-fixes branch May 19, 2023 22:09
This was referenced May 19, 2023
Fix tests on Python 3.12 #162
Merged
gh-74690: Make a typing test more resilient python/cpython#104691
Merged
This was referenced May 23, 2023
Clarify error message for non @runtime_checkable decorated protocols #178
Merged
fix(typing): add @runtime_checkable decorator for CustomTreeNode protocol class metaopt/optree#56
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra 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.

2 participants

@AlexWaygood @JelleZijlstra
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.