Skip to content

typing: Add missing test case for Protocol inheritance #132597

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

Merged
merged 2 commits into from
Apr 16, 2025

Conversation

JelleZijlstra
Copy link
Member

No description provided.

@AlexWaygood
Copy link
Member

You could also test this one?

>>> from typing import *
>>> T = TypeVar("T")
>>> class Foo(Protocol, Generic[T], object):
...     x: T
...     
>>> get_protocol_attrs(Foo)
frozenset({"x"})

@JelleZijlstra
Copy link
Member Author

Done, also added a PEP 695 one

@JelleZijlstra JelleZijlstra added the needs backport to 3.13 bugs and security fixes label Apr 16, 2025
Comment on lines +2948 to +2949
class NewGeneric[T](Protocol, object):
z: T
Copy link
Member

Choose a reason for hiding this comment

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

although this one fails, interestingly:

        class NewGeneric[T](Protocol[T], object):
            z: T

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's general PEP 695 behavior, generic classes work by injecting Generic[T] as the last base and if object is already there, it fails.

>>> class X[T](object): pass
... 
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    class X[T](object): pass
  File "<python-input-2>", line 1, in <generic parameters of X>
    class X[T](object): pass
TypeError: Cannot create a consistent method resolution order (MRO) for bases object, Generic

Somewhat unfortunate but nobody has complained about it and the workaround is simple (just don't inherit from object there).

Copy link
Member

Choose a reason for hiding this comment

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

oh yeah, I understand why it is the way it is and I'm not asking for it to be changed! Happy for it to remain an untested corner case

@JelleZijlstra JelleZijlstra merged commit 72da4a4 into python:main Apr 16, 2025
44 checks passed
@miss-islington-app
Copy link

Thanks @JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 16, 2025
(cherry picked from commit 72da4a4)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Apr 16, 2025

GH-132603 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Apr 16, 2025
AlexWaygood pushed a commit that referenced this pull request Apr 16, 2025
) (#132603)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip issue skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants