typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Improve empty collection handling with `next`

Open max-muoto opened this issue 1 year ago • 3 comments

Fixes https://github.com/python/typeshed/issues/12064.

max-muoto avatar May 30 '24 16:05 max-muoto

This doesn't seem right; it potentially leaves the type variable unsolved.

JelleZijlstra avatar May 30 '24 16:05 JelleZijlstra

Diff from mypy_primer, showing the effect of this PR on open source code:

pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/v1/fields.py:462: error: Argument 2 to "next" has incompatible type "None"; expected "FieldInfo"  [arg-type]
+ pydantic/_internal/_decorators.py:560: error: Argument 2 to "next" has incompatible type "None"; expected "Parameter"  [arg-type]
+ pydantic/_internal/_decorators.py:678: error: Argument 2 to "next" has incompatible type "None"; expected "Parameter"  [arg-type]
+ pydantic/_internal/_decorators.py:702: error: Argument 2 to "next" has incompatible type "None"; expected "Parameter"  [arg-type]
+ pydantic/_internal/_std_types_schema.py:492: error: Argument 2 to "next" has incompatible type "None"; expected "FieldInfo"  [arg-type]
+ pydantic/_internal/_model_construction.py:418: error: Argument 2 to "next" has incompatible type "None"; expected "ModelPrivateAttr"  [arg-type]

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/enums.py:248: error: Argument 2 to "next" has incompatible type "None"; expected "Self"  [arg-type]
+ steam/media.py:73: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/generic.py:1754: error: Argument 2 to "next" has incompatible type "None"; expected "int"  [arg-type]

git-revise (https://github.com/mystor/git-revise)
+ gitrevise/merge.py:374: error: Argument 2 to "next" has incompatible type "None"; expected "bytes"  [arg-type]
+ gitrevise/merge.py:425: error: Argument 2 to "next" has incompatible type "None"; expected "bytes"  [arg-type]

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/certs.py:564: error: Argument 2 to "next" has incompatible type "None"; expected "str | tuple[str | None, Any]"  [arg-type]
+ mitmproxy/addons/tlsconfig.py:519: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]

porcupine (https://github.com/Akuli/porcupine)
+ porcupine/plugins/langserver.py:717: error: Argument 2 to "next" has incompatible type "None"; expected "LangServer"  [arg-type]
+ porcupine/plugins/filemanager.py:281: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]

sympy (https://github.com/sympy/sympy)
+ sympy/parsing/latex/lark/transformer.py:555: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]

pylint (https://github.com/pycqa/pylint)
+ pylint/lint/message_state_handler.py:295: error: Argument 2 to "next" has incompatible type "tuple[None, None]"; expected "tuple[int, bool]"  [arg-type]
+ pylint/lint/message_state_handler.py:295: error: Argument 2 to "next" has incompatible type "tuple[None, None]"; expected "tuple[Any, bool]"  [arg-type]
+ pylint/lint/run.py:143: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ pylint/checkers/base/basic_checker.py:401: error: Argument 2 to "next" has incompatible type "None"; expected "bool"  [arg-type]

openlibrary (https://github.com/internetarchive/openlibrary)
+ openlibrary/catalog/marc/marc_base.py: note: In member "get_control" of class "MarcBase":
+ openlibrary/catalog/marc/marc_base.py:74: error: Argument 2 to "next" has incompatible type "tuple[str, None]"; expected "tuple[str, str | MarcFieldBase]"  [arg-type]
+ openlibrary/catalog/marc/marc_base.py:74: error: Argument 2 to "next" has incompatible type "tuple[str, None]"; expected "tuple[Any, str | MarcFieldBase]"  [arg-type]
+ openlibrary/catalog/add_book/load_book.py: note: In function "remove_author_honorifics":
+ openlibrary/catalog/add_book/load_book.py:241: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]
+ openlibrary/plugins/worksearch/subjects.py: note: In member "get_meta" of class "SubjectEngine":
+ openlibrary/plugins/worksearch/subjects.py:356: error: Argument 2 to "next" has incompatible type "None"; expected "SubjectMeta"  [arg-type]
+ openlibrary/plugins/worksearch/subjects.py: note: In member "facet_wrapper" of class "SubjectEngine":
+ openlibrary/plugins/worksearch/subjects.py:380: error: Argument 2 to "next" has incompatible type "None"; expected "SubjectMeta"  [arg-type]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/domains/std/__init__.py: note: In member "process_doc" of class "StandardDomain":
+ sphinx/domains/std/__init__.py:745:68: error: Argument 2 to "next" has incompatible type "None"; expected "toctree"  [arg-type]
+ sphinx/builders/latex/__init__.py: note: In member "write" of class "LaTeXBuilder":
+ sphinx/builders/latex/__init__.py:298:67: error: Argument 2 to "next" has incompatible type "None"; expected "toctree"  [arg-type]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/http_parser.py:502:83: error: Argument 2 to "next" has incompatible type "None"; expected "istr"  [arg-type]

mypy (https://github.com/python/mypy)
+ mypy/stubtest.py:558: error: Argument 2 to "next" has incompatible type "Missing"; expected "Optional[SymbolNode]"  [arg-type]
+ mypy/inspections.py:556: error: Argument 2 to "next" has incompatible type "None"; expected "State"  [arg-type]
+ mypy/inspections.py:560: error: If condition is always false  [redundant-expr]
+ mypy/inspections.py:560: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-redundant-expr for more info

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/junitxml.py:583: error: Argument 2 to "next" has incompatible type "None"; expected "TestReport"  [arg-type]

spack (https://github.com/spack/spack)
+ lib/spack/spack/compilers/__init__.py:1033: error: Argument 2 to "next" has incompatible type "None"; expected "dict[Any, Any]"  [arg-type]
+ lib/spack/spack/oci/opener.py:218: error: Argument 2 to "next" has incompatible type "None"; expected "Challenge"  [arg-type]
+ lib/spack/spack/oci/opener.py:224: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]
+ lib/spack/spack/oci/opener.py:225: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]
+ lib/spack/spack/oci/opener.py:226: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_core/_fileio.py:202: error: Argument 1 to "run_sync" has incompatible type overloaded function; expected "Callable[[Iterator[PathLike[str]], None], PathLike[str]]"  [arg-type]

boostedblob (https://github.com/hauntsaninja/boostedblob)
+ boostedblob/_recover.py:137: error: Argument 2 to "next" has incompatible type "None"; expected "int"  [arg-type]
+ boostedblob/_recover.py:140: error: Left operand of "and" is always true  [redundant-expr]

werkzeug (https://github.com/pallets/werkzeug)
- tests/test_test.py:757: note:     def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
+ tests/test_test.py:757: note:     def [_T, _VT] next(SupportsNext[_T], _VT | _T, /) -> _T | _VT

kopf (https://github.com/nolar/kopf)
+ kopf/_core/actions/throttlers.py:61: error: Argument 2 to "next" has incompatible type "float | None"; expected "float"  [arg-type]

poetry (https://github.com/python-poetry/poetry)
- src/poetry/mixology/version_solver.py:510: error: Returning Any from function declared to return "str | None"  [no-any-return]
+ src/poetry/mixology/version_solver.py:500: error: Argument 2 to "next" has incompatible type "None"; expected "DependencyPackage"  [arg-type]

SinbadCogs (https://github.com/mikeshardmind/SinbadCogs)
+ quotetools/quotetools.py:39: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]

Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/_internal/__init__.py:243: error: Argument 2 to "next" has incompatible type "None"; expected "Parameter"  [arg-type]
+ tanjun/conversion.py:1003: error: Argument 2 to "next" has incompatible type "None"; expected "Match[str]"  [arg-type]
+ tanjun/conversion.py:1279: error: Argument 2 to "next" has incompatible type "None"; expected "Match[str]"  [arg-type]
+ tanjun/parsing.py:579: error: Non-overlapping identity check (left operand type: "Optional[str]", right operand type: "ellipsis")  [comparison-overlap]
+ tanjun/parsing.py:579: error: Argument 2 to "next" has incompatible type "ellipsis"; expected "Optional[str]"  [arg-type]
+ tanjun/parsing.py:580: error: Non-overlapping identity check (left operand type: "Optional[str]", right operand type: "ellipsis")  [comparison-overlap]
+ tanjun/parsing.py:580: error: Argument 2 to "next" has incompatible type "ellipsis"; expected "Optional[str]"  [arg-type]

operator (https://github.com/canonical/operator)
+ ops/framework.py:1030: error: Argument 2 to "next" has incompatible type "None"; expected "tuple[str, str, str]"  [arg-type]

mkosi (https://github.com/systemd/mkosi)
+ mkosi/user.py:94:96: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/installer/rpm.py:27:79: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/installer/rpm.py:30:78: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/distributions/rhel.py:52:70: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/distributions/rhel.py:54:60: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/distributions/rhel.py:67:93: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ mkosi/distributions/rhel.py:69:91: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]

aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/connection.py:1497: error: Argument 2 to "next" has incompatible type "None"; expected "BaseException"  [arg-type]
+ aioredis/connection.py:1666: error: Argument 2 to "next" has incompatible type "None"; expected "BaseException"  [arg-type]

black (https://github.com/psf/black)
+ src/black/files.py:294:81: error: Argument 2 to "next" has incompatible type "None"; expected "Path"  [arg-type]
+ src/black/files.py:298:5: error: Statement is unreachable  [unreachable]

ibis (https://github.com/ibis-project/ibis)
+ ibis/util.py:326: error: Argument 2 to "next" has incompatible type "None"; expected "T"  [arg-type]
- ibis/expr/operations/udf.py:41: note:     def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
+ ibis/expr/operations/udf.py:41: note:     def [_T, _VT] next(SupportsNext[_T], _VT | _T, /) -> _T | _VT

jax (https://github.com/google/jax)
+ jax/_src/util.py:158: error: Argument 2 to "next" has incompatible type "object"; expected "T1"  [arg-type]
+ jax/_src/util.py:158: error: Argument 2 to "next" has incompatible type "object"; expected "T2"  [arg-type]
+ jax/_src/util.py:167: error: Argument 2 to "next" has incompatible type "object"; expected "T"  [arg-type]
+ jax/_src/util.py:179: error: Argument 2 to "next" has incompatible type "object"; expected "T"  [arg-type]
+ jax/_src/util.py:202: error: Argument 2 to "next" has incompatible type "object"; expected "T"  [arg-type]
+ jax/_src/clusters/cluster.py:53: error: Argument 2 to "next" has incompatible type "None"; expected "type[ClusterEnv]"  [arg-type]
+ jax/_src/clusters/slurm_cluster.py:50: error: Argument 2 to "next" has incompatible type "None"; expected "int"  [arg-type]
+ jax/_src/interpreters/partial_eval.py:566: error: Argument 2 to "next" has incompatible type "object"; expected "AbstractValue"  [arg-type]
+ jax/_src/interpreters/partial_eval.py:2615: error: Argument 2 to "next" has incompatible type "None"; expected "DynamicJaxprTracer"  [arg-type]
+ jax/_src/state/discharge.py:186: error: Argument 2 to "next" has incompatible type "None"; expected "int"  [arg-type]

flake8-pyi (https://github.com/PyCQA/flake8-pyi)
+ pyi.py:75: error: Argument 2 to "next" has incompatible type "bool"; expected "tuple[object, Iterator[object]]"  [arg-type]
+ pyi.py:76: error: Argument 2 to "next" has incompatible type "bool"; expected "tuple[object, Iterator[object]]"  [arg-type]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/app_commands/tree.py:1255: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]
+ discord/app_commands/transformers.py:174: error: Argument 2 to "next" has incompatible type "None"; expected "Choice[str | int | float]"  [arg-type]

schema_salad (https://github.com/common-workflow-language/schema_salad)
+ schema_salad/tests/test_schema.py: note: In function "test_extend_and_specialize_enums":
+ schema_salad/tests/test_schema.py:17:87: error: Argument 2 to "next" has incompatible type "None"; expected "dict[str, Any]"  [arg-type]

urllib3 (https://github.com/urllib3/urllib3)
+ test/contrib/test_socks.py:719: error: Argument 2 to "next" has incompatible type "None"; expected "tuple[bytes | str, int]"  [arg-type]

streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/runtime/app_session.py:425:13: error: Argument 2 to "next" has incompatible type "None"; expected "str"  [arg-type]
+ lib/streamlit/runtime/app_session.py:432:9: error: Statement is unreachable  [unreachable]

github-actions[bot] avatar May 30 '24 16:05 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

werkzeug (https://github.com/pallets/werkzeug)
- tests/test_test.py:757: note:     def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
+ tests/test_test.py:757: note:     def [_T] next(SupportsNext[_T], _T, /) -> _T

operator (https://github.com/canonical/operator)
- ops/main.py:194: error: Argument 2 to "next" has incompatible type "None"; expected "Storage"  [arg-type]
+ ops/main.py:194: error: Incompatible types in assignment (expression has type "Storage | None", variable has type "Storage")  [assignment]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/operations/udf.py:41: note:     def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
+ ibis/expr/operations/udf.py:41: note:     def [_T] next(SupportsNext[_T], _T, /) -> _T

streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/runtime/app_session.py:424:20: error: Argument 1 to "filter" has incompatible type "Callable[[Any], bool]"; expected "Callable[[str], TypeGuard[Optional[str]]]"  [arg-type]
+ lib/streamlit/runtime/app_session.py:424:36: error: Invalid index type "Optional[str]" for "Dict[str, Dict[str, str]]"; expected type "str"  [index]

github-actions[bot] avatar May 30 '24 16:05 github-actions[bot]