-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Bound instance methods fail to satisfy runtime_checkable
Protocol
#127059
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
Thanks @AlexWaygood. I am happy to help with this if I am able, but I imagine the first step is deciding what (if anything) to do about it! |
My first question here was "So why doesn't the >>> class Foo: pass
...
>>> Foo.__getattribute__ is object.__getattribute__
True
>>> import types
>>> types.MethodType.__getattribute__ is object.__getattribute__
False My next question was, "Why does >>> class Foo:
... def f(self): pass
...
>>> Foo.f.bar = 42
>>>
>>> Foo.f.__dict__ is Foo().f.__dict__
True
>>> Foo().f.bar
42
I think the choice to switch to I'm not sure we should change the docs here. We already have a note in "What's new" outlining that the change we made might cause problems like this. I'm not sure this specific case should be called out there, as it seems like something of an edge case that not many users are likely to run into. Python 3.12 has now been released for over a year, and this is the first time anybody has raised this issue. Even for "What's new", it's important to keep the document focussed and streamlined. So, to summarize: I'm really sorry that this change broke your code! But, I'm not sure there's anything to be done here :( |
This all makes sense. I was actually part way through refactoring my code for compliance with strict pyright (which does not allow setting arbitrary attributes on functions) when I discovered it doesn't run on 3.12, so this is something that will go away soon in my case. I mainly thought it was interesting and wondered if it was a bug. Hopefully this issue will now appear in searches and your excellent summary above will provide the documentation of this behaviour. Thanks for your help! |
Bug report
Bug description:
#103034 added some protections around
isinstance
checks ofruntime_checkable
Protocols
usinginspect.getattr_static
. This usesinspect._check_instance
which usesobject.__getattribute__(obj, "__dict__")
, which fails for bound instance methods (althoughobj.__dict__
does work), so they cannot fulfillruntime_checkable
Protocols
anymore.Is this an intended/known change? Could it be noted in the changelog here as an example of case that will no longer work?
Created from #103034 (comment)
cc @AlexWaygood
CPython versions tested on:
3.12
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: