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

Fix bug with in operator used with a union of Container and Iterable #14384

Merged

Conversation

koogoro
Copy link
Collaborator

@koogoro koogoro commented Jan 3, 2023

Fixes #4954.

Modifies analysis of in comparison expressions. Previously, mypy would check the right operand of an in expression to see if it was a union of Containers, and then if it was a union of Iterables, but would fail on unions of both Containers and Iterables.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good, just one nit.

We allow `in` for a union of containers and iterables as long as at least one of them matches the
type of the left operand, as the operation will simply return False if the union's container/iterator
type doesn't match the left operand.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: We use # comments also for block comments.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL JukkaL merged commit 1d247ea into python:master Jan 30, 2023
hauntsaninja pushed a commit that referenced this pull request Feb 18, 2023
…mode (#14727)

Fixes a bug introduced in #14384
wherein a union that includes `None` is no longer treated as a valid
right-hand type for the `in` operator in `no-strict-optional` mode. (The
reported error is `error: "None" has no attribute "__iter__" (not
iterable) [attr-defined]`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'in' operator rejected with union right operand
2 participants