FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-12022: Change error type for bad objects in "with" and "async with" by serhiy-storchaka · Pull Request #26809 · python/cpython · GitHub

/ cpython Public

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

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:context-manager-type-error
Jun 29, 2021
Merged

bpo-12022: Change error type for bad objects in "with" and "async with"#26809
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:context-manager-type-error

Conversation

serhiy-storchaka commented Jun 20, 2021
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

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.

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

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.

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Okay, LG then!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL