Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve error message when usingin for object that is not container #95144

Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@Mariatta

Description

@Mariatta

Feature or enhancement

When usingin to test containment, for exampleif "a" in b:, if b does not support thein operator, then it would raise an error message:TypeError: Argument of type b is not iterable.

To the reader/debugger of this code, the error message seems to suggest that you need to pass in an iterable object (an object that implements__iter__), but in reality, you can also pass in a container object (an object that implements__contains__).

It would be great if the error message can be improved and be more accurate and helpful.

Pitch

Thein keyword can be used in different ways:

for loop

for item in [1,2,3]:

You can do for loop when you gave an iterable/sequences, like lists, dicts, strings, objects that implements__iter__

testing for containment

if "a" in "abcd":

To make a class into a container, you just need to implement the__contains__ method.

class Blabla:    def __contains__(self, item):        return Falseb = Blabla()>>> "a" in bFalse

If somehow we made a bad refactoring on this container class and removed/renamed the__contains__, and tried to use the same existing code"a" in b, it would raise an error saying that b is not iterable.

If the object was not an iterable to begin with, this error message is confusing to the person debugging this, and they would not realize that this is due to the missing__contains__ method.

I think it would be great if the error message when testing containmentif a in b can be different than the error message when doing for loopfor a in b.
Providing more accurate error message will be helpful to the user.

Example message:
TypeError: Argument of type '%.200s' is not a container

(and that this is only raised when doingif a in b)

I tried to look into the CPython code, and it seems like the error message is coming from this line:

type_error("argument of type '%.200s' is not iterable",seq);

Which was introduced in#20537

Regarding the term container, it is used in this doc:https://docs.python.org/3/library/collections.abc.html#collections.abc.Container

Previous discussion

I don't know if you'd count Twitter thread as previous discussions, but here are some links:
Start of thread

Supporting message that the error message can be improved:here andhere

Comment about Python's terminology of container and iterable

Comment about Python oddity

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp