Do not iterate via __getitem__ when __iter__ is explicitly set - #11645
Open
Henry Su (hsusul) wants to merge 1 commit into
Open
Do not iterate via __getitem__ when __iter__ is explicitly set#11645Henry Su (hsusul) wants to merge 1 commit into
Henry Su (hsusul) wants to merge 1 commit into
Conversation
CPython raises TypeError when __iter__ is None even if __getitem__ exists; the legacy sequence protocol applies only when __iter__ is missing.
Collaborator
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
Rich Chiodo (rchiodo)
approved these changes
Aug 17, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Rich Chiodo (rchiodo)
approved these changes
Aug 17, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Heejae Chang (heejaechang)
approved these changes
Aug 17, 2026
Heejae Chang (heejaechang)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
- .../projects/sympy/sympy/solvers/diophantine/diophantine.py:1460:50 - error: Cannot access attribute "as_coeff_Mul" for class "Basic*"
- Attribute "as_coeff_Mul" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/diophantine/diophantine.py:1460:50 - error: Cannot access attribute "as_coeff_Mul" for class "Relational*"
- Attribute "as_coeff_Mul" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/lie_group.py:619:61 - error: Operator "-" not supported for type "Unknown | Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:225:45 - error: Cannot access attribute "has" for class "tuple[Expr, int]"
- Attribute "has" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:223:22 - error: No overloads for "__new__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:223:25 - error: Argument of type "Unknown | One | NegativeOne | Zero | Integer | tuple[Expr, int]" cannot be assigned to parameter "arg" of type "Expr" in function "__new__"
+ Type "Unknown | One | NegativeOne | Zero | Integer | tuple[Expr, int]" is not assignable to type "Expr"
+ "tuple[Expr, int]" is not assignable to "Expr" (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:238:40 - error: No overloads for "__new__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:238:50 - error: Argument of type "Unknown | One | NegativeOne | Zero | Integer | tuple[Expr, int]" cannot be assigned to parameter "arg" of type "Expr" in function "__new__"
+ Type "Unknown | One | NegativeOne | Zero | Integer | tuple[Expr, int]" is not assignable to type "Expr"
+ "tuple[Expr, int]" is not assignable to "Expr" (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/ode.py:1433:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
+ .../projects/sympy/sympy/solvers/ode/ode.py:1433:17 - error: Argument of type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "Zero"
+ Type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr" is not assignable to type "Zero"
- .../projects/sympy/sympy/solvers/ode/ode.py:2961:17 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
+ .../projects/sympy/sympy/solvers/ode/ode.py:2961:17 - error: Argument of type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "Zero"
+ Type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr" is not assignable to type "Zero"
+ .../projects/sympy/sympy/solvers/ode/riccati.py:275:12 - error: No overloads for "__init__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/riccati.py:275:17 - error: Argument of type "zip[Unknown]" cannot be assigned to parameter "iterable" of type "Iterable[list[bytes]]" in function "__init__"
+ "zip[Unknown]" is incompatible with protocol "Iterable[list[bytes]]"
+ "__iter__" is an incompatible type
+ Type "() -> zip[Unknown]" is not assignable to type "() -> Iterator[_T_co@Iterable]"
+ Function return type "zip[Unknown]" is incompatible with type "Iterator[_T_co@Iterable]"
+ "zip[Unknown]" is incompatible with protocol "Iterator[_T_co@Iterable]" (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/riccati.py:874:51 - error: Cannot access attribute "diff" for class "Basic*"
- Attribute "diff" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/single.py:867:9 - error: Expression with type "tuple[ComplexInfinity | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown]] | tuple[ComplexInfinity | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" cannot be assigned to target tuple
+ Type "tuple[ComplexInfinity | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" is incompatible with target tuple
+ Tuple size mismatch; expected 2 but received 3 (reportAssignmentType)
- .../projects/sympy/sympy/solvers/ode/single.py:2646:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Unknown | int" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
+ .../projects/sympy/sympy/solvers/ode/single.py:2646:27 - error: Argument of type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr | NaN | NegativeInfinity | int" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Unknown | int" is not assignable to type "SupportsIndex"
+ Type "One | NegativeOne | Zero | Integer | Unknown | Rational | Number | Expr | NaN | NegativeInfinity | int" is not assignable to type "SupportsIndex"
- .../projects/sympy/sympy/solvers/ode/subscheck.py:270:32 - error: Operator "-" not supported for types "Basic | Unknown | Zero | Self@Basic" and "Basic | Unknown | Zero | Self@Basic"
+ .../projects/sympy/sympy/solvers/ode/subscheck.py:270:32 - error: Operator "-" not supported for types "Basic | Unknown | Zero" and "Basic | Unknown | Zero"
- Operator "-" not supported for types "Basic" and "Basic"
- Operator "-" not supported for types "Basic" and "Basic*"
- Operator "-" not supported for types "Basic*" and "Basic"
- Operator "-" not supported for types "Basic*" and "Basic*" (reportOperatorIssue)
+ Operator "-" not supported for types "Basic" and "Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/systems.py:1167:37 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1167:37 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "Literal[2]" and "Basic*"
- .../projects/sympy/sympy/solvers/ode/systems.py:1167:37 - error: Operator "*" not supported for types "Unknown | int | Expr" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1167:37 - error: Operator "*" not supported for types "Unknown | Expr | int" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "int" and "Basic*"
- Operator "*" not supported for types "int" and "Basic"
- Operator "*" not supported for types "Expr" and "Basic*"
+ Operator "*" not supported for types "Expr" and "Basic"
- Operator "*" not supported for types "Expr" and "Basic" (reportOperatorIssue)
+ Operator "*" not supported for types "int" and "Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/systems.py:1169:30 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1169:30 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "Literal[2]" and "Basic*"
- .../projects/sympy/sympy/solvers/ode/systems.py:1169:30 - error: Operator "*" not supported for types "Unknown | int | Expr" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1169:30 - error: Operator "*" not supported for types "Unknown | Expr | int" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "int" and "Basic*"
- Operator "*" not supported for types "int" and "Basic"
- Operator "*" not supported for types "Expr" and "Basic*"
+ Operator "*" not supported for types "Expr" and "Basic"
+ Operator "*" not supported for types "int" and "Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/systems.py:1169:49 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1169:49 - error: Operator "*" not supported for types "Literal[2]" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "Literal[2]" and "Basic*"
- .../projects/sympy/sympy/solvers/ode/systems.py:1169:49 - error: Operator "*" not supported for types "Unknown | int | Expr" and "Unknown | Self@Basic | bool | Expr | Basic"
+ .../projects/sympy/sympy/solvers/ode/systems.py:1169:49 - error: Operator "*" not supported for types "Unknown | Expr | int" and "Unknown | Expr | Basic | bool"
- Operator "*" not supported for types "int" and "Basic*"
- Operator "*" not supported for types "int" and "Basic"
- Operator "*" not supported for types "Expr" and "Basic*"
+ Operator "*" not supported for types "Expr" and "Basic"
... (truncated 1777 lines) ...
|
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
__iter__ = Noneand__getitem__should not be iterable. #11220.__iter__is present but not callable (__iter__ = None), even if__getitem__exists. Pyright still used the legacy sequence protocol and acceptedfor x in Foo().__getitem__fallback now applies only when__iter__is missing.Test plan
npx jest typeEvaluator3.test.ts -t ForLoop --forceExit(frompackages/pyright-internal)for _ in Foo()with__iter__ = NoneraisesTypeError; a__getitem__-only class still iteratesforloop over such a class reports a not-iterable error in the language server