Visitar URL original
bpo-12022: Change error type for bad objects in "with" and "async with" by serhiy-storchaka · Pull Request #26809 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223175729/https://github.com/python/cpython/pull/26809
Skip to content
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

bpo-12022: Change error type for bad objects in "with" and "async with" #26809

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jun 20, 2021 •

A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.

https://bugs.python.org/issue12022

A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.
Copy link
Member

@gvanrossum gvanrossum left a comment

I support the idea.

For the code, why not pass the message format string to special_lookup() instead of repeating the same code four times?

Also I think it would be useful to know which attribute is missing.

@serhiy-storchaka
Copy link
Member Author

serhiy-storchaka commented Jun 21, 2021

For the code, why not pass the message format string to special_lookup() instead of repeating the same code four times?

It would save 3 lines in every of 4 caller places, but the helper function adds 14 lines. Also, I think that in future we can add slots for these methods, this will make the common code so simple that there will be nothing to share.

Also I think it would be useful to know which attribute is missing.

I think that in most cases the error is in using wrong object rather than missing of some dunder methods in the definition of user class. Traditional error messages:

>>> len(1)
TypeError: object of type 'int' has no len()
>>> list(1)
TypeError: 'int' object is not iterable

It does not say "requires an object with __iter__ method".

But it may be worth to add a hint in case of the object that has __enter__ but not __exit__. It is definitely an error in the user class definition.

Copy link
Member

@gvanrossum gvanrossum left a comment

Okay, LG then!

@serhiy-storchaka serhiy-storchaka merged commit 20a8800 into python:main Jun 29, 2021
10 of 13 checks passed
@serhiy-storchaka serhiy-storchaka deleted the context-manager-type-error branch Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants