-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add --disallow-str-iteration flag #20577
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
base: master
Are you sure you want to change the base?
Add --disallow-str-iteration flag #20577
Conversation
for more information, see https://pre-commit.ci
| s = "hello" | ||
| for ch in s: # error: Iterating over "str" is disallowed | ||
| print(ch) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add an example with iter(s) to show that's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, on it
mypy/typeshed/stdlib/builtins.pyi
Outdated
| def __getitem__(self, i: int, /) -> _T_co: ... | ||
|
|
||
| @overload | ||
| def iter(object: str, /) -> Iterable[str]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be in a patch file, or it will be overwritten next time we sync typeshed. Look for similar patch files in the repo (there are a few related to LiteralString).
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: meson (https://github.com/mesonbuild/meson)
+ mesonbuild/compilers/cuda.py:249:34: error: No overload variant of "next" matches argument type "Iterable[str]" [call-overload]
+ mesonbuild/compilers/cuda.py:249:34: note: Possible overload variants:
+ mesonbuild/compilers/cuda.py:249:34: note: def [_T] next(SupportsNext[_T], /) -> _T
+ mesonbuild/compilers/cuda.py:249:34: note: def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
altair (https://github.com/vega/altair)
+ tools/vega_expr.py:822: error: No overload variant of "next" matches argument type "Iterable[str]" [call-overload]
+ tools/vega_expr.py:822: note: Possible overload variants:
+ tools/vega_expr.py:822: note: def [_T] next(SupportsNext[_T], /) -> _T
+ tools/vega_expr.py:822: note: def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
cki-lib (https://gitlab.com/cki-project/cki-lib)
+ cki_lib/cki_pipeline.py:537: error: No overload variant of "next" matches argument types "Iterable[str]", "None" [call-overload]
+ cki_lib/cki_pipeline.py:537: note: Possible overload variants:
+ cki_lib/cki_pipeline.py:537: note: def [_T] next(SupportsNext[_T], /) -> _T
+ cki_lib/cki_pipeline.py:537: note: def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
ibis (https://github.com/ibis-project/ibis)
+ ibis/expr/types/generic.py:1785: error: No overload variant of "next" matches argument type "Iterable[str]" [call-overload]
+ ibis/expr/types/generic.py:1785: note: Possible overload variants:
+ ibis/expr/types/generic.py:1785: note: def [_T] next(SupportsNext[_T], /) -> _T
+ ibis/expr/types/generic.py:1785: note: def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
apprise (https://github.com/caronc/apprise)
+ apprise/config/base.py:1509: note: def iter(str, /) -> Iterable[str]
|
|
|
||
| def is_str_iteration_type(self, typ: Type) -> bool: | ||
| typ = get_proper_type(typ) | ||
| if isinstance(typ, LiteralType): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wonder if we should do this for subtypes of str too and do something like an is_proper_subtype() check with str.
| "collections.abc.Collection", | ||
| "typing.Collection", | ||
| ): | ||
| # `str` doesn't conform to the `Collection` protocol, but we don't want to show that as the reason for the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the discrepancy actually on Container? So we should maybe do this for any subclass of Container.
Fixes #11001
Related to typing python/typing#256