-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
AttributeError on with super()
#129466
Comments
The reason that it's currently an error (a TypeError, it seems?) is do to how the Lines 515 to 529 in 4e47e05
Perhaps the I'm not convinced that's the best way forward... but I'd be happy to take a swing at a solution. Edit: this is the case for 3.12 and 3.13; for 3.14 after alpha one, see the next post. |
Hmmm the error actually comes from a different place in 3.14. Specifically, from the bytecode implementation of Lines 3284 to 3296 in 4e47e05
This is based on the specific verbiage of the error messages I'm seeing about missing methods: Lines 377 to 381 in 4e47e05
So again, some special-casing could occur there. |
+1, the special-cased behavior would be very intuitive and succinct |
I'm trying to understand I'd be in favor of adding that support to Those changes to I could submit an |
The docs say:
The use of So, strictly speaking, the behaviour is as expected although burried in the docs. Changing this may require a PEP but we may start by trying to provide a recipe for this (writing enter and exit calls explicitly may not be the first thing users may think of so it's better if we can write that recipe down). OTOH, we may special-case the enter/exit special methods as well as their asynchronous counterparts which suffer from the same issue I think (I don't think |
@picnixz Thank you for pointing that out in the docs - I'd agree with you that this is behaving as described, though I wonder if we can be even clearer there - perhaps adding the explicit example of context managers to the docs as in:
You're right that the error is raised with async context managers: import asyncio
class Base:
def __aenter__(self):
print("Entering Base")
def __aexit__(self):
print("Exiting Base")
class Derived(Base):
async def foo(self):
async with super():
print("Inside context manager")
asyncio.run(Derived().foo())
|
We had an opened issue that aimed to improve the error messages for those cases. Since there was no progression since then, I will have a look at this in two weeks or so. In the meantime I'm wondering whether we can decide first whether we want to support this kind of feature or not.
|
I don't think this is really a typing question, just a language question. If we add the behavior to I don't have strong feelings either way about the change. My main hesitance would be that it's not clear where we should stop; should The implementation might also get a little weird, in that I think the There could also be strange/surprising behaviors if I think it would be pretty easy to write a small library class that could wrap a |
Ah yes Carl, sorry if I used "typing" here instead of "language". I forgot the word for a moment and went for the closest one I could remember :')
This is what happens if we have mixin classes where a mixin implements
How about adding this to contextlib recipes (not necessarily implement it on our side). Orthogonally, we can also list a few more cases where |
Sure, those both seem like potentially reasonable things to do. The contextlib recipe would just need to be evaluated for how common this need is, and whether there's concern about bloating the contextlib recipes with too many very niche recipes. |
Just to be clear, but by recipes, I only meant docs recipes. I don't think we have a page for them yet though (but we should perhaps have a cookbook for more modules in general). I'll start a dpo thread next week on this matter (namely whether we should have more cookbooks for modules and how niche we should go) |
Bug report
Bug description:
Writing
with super()
raises an AttributeError. I ran into this implementing context managers inspired bycontextlib.contextmnager
but which had extra functionality. I can see no reason whywith super()
should be disallowed, and in fact,super().__enter__()
andsuper().__exit__()
work just fine.Attempting to use an
ExitStack
as a work around was unsuccessful.CPython versions tested on:
3.13, 3.12
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: