Skip to content

Conversation

@michaelm-openai
Copy link

Fixes #11001

Related to typing python/typing#256

s = "hello"
for ch in s: # error: Iterating over "str" is disallowed
print(ch)
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

This is not done

Copy link
Author

Choose a reason for hiding this comment

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

oops, on it

def __getitem__(self, i: int, /) -> _T_co: ...

@overload
def iter(object: str, /) -> Iterable[str]: ...
Copy link
Member

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).

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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):
Copy link
Member

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.
Copy link
Member

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.

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.

Allow mypy to flag str matching against Sequence[str] as an error

2 participants