[ty] Infer empty collection constructors from later uses#26389
Merged
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 94.47%. The percentage of expected errors that received a diagnostic held steady at 89.19%. The number of fully passing files held steady at 95/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
800fdc8 to
7d4af40
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
13 | 0 | 1 |
no-matching-overload |
10 | 0 | 0 |
unresolved-attribute |
1 | 0 | 2 |
unsupported-operator |
0 | 0 | 3 |
invalid-assignment |
2 | 0 | 0 |
not-iterable |
0 | 0 | 1 |
unused-type-ignore-comment |
0 | 1 | 0 |
| Total | 26 | 1 | 7 |
Raw diff (34 changes)
beartype (https://github.com/beartype/beartype)
+ beartype/bite/collection/infercollectionitems.py:288:47 error[invalid-argument-type] Argument to function `make_hint_pep484604_union` is incorrect: Expected `Sequence[TypeForm[Any]]`, found `tuple[object, ...]`
+ beartype/bite/collection/infercollectionitems.py:289:49 error[invalid-argument-type] Argument to function `make_hint_pep484604_union` is incorrect: Expected `Sequence[TypeForm[Any]]`, found `tuple[object, ...]`
core (https://github.com/home-assistant/core)
- homeassistant/components/tasmota/light.py:224:40 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.HS]` and `set[ColorMode] | None | set[Unknown]`
+ homeassistant/components/tasmota/light.py:224:40 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.HS]` and `set[ColorMode] | None`
- homeassistant/components/tasmota/light.py:228:37 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.WHITE]` and `set[ColorMode] | None | set[Unknown]`
+ homeassistant/components/tasmota/light.py:228:37 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.WHITE]` and `set[ColorMode] | None`
- homeassistant/components/tasmota/light.py:239:17 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.COLOR_TEMP]` and `set[ColorMode] | None | set[Unknown]`
+ homeassistant/components/tasmota/light.py:239:17 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal[ColorMode.COLOR_TEMP]` and `set[ColorMode] | None`
freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/optimize/analysis/lookahead.py:137:57 error[invalid-argument-type] Argument to function `LookaheadAnalysis.get_result` is incorrect: Expected `DataFrame`, found `dict[Unknown, Unknown]`
+ freqtrade/optimize/analysis/lookahead.py:137:57 error[invalid-argument-type] Argument to function `LookaheadAnalysis.get_result` is incorrect: Expected `DataFrame`, found `dict[str, DataFrame]`
manticore (https://github.com/trailofbits/manticore)
+ tests/other/test_smtlibv2.py:139:25 error[invalid-argument-type] Argument to bound method `set.add` is incorrect: Expected `<class 'Expression'> | <class 'BoolOperation'> | <class 'BitVecOperation'> | ... omitted 4 union elements`, found `type`
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/asynchronous/mongo_client.py:892:37 error[invalid-argument-type] Argument to bound method `AsyncMongoClient._init_based_on_options` is incorrect: Expected `Collection[tuple[str, int]]`, found `set[Unknown | tuple[str, int | None]] & ~AlwaysFalsy`
+ pymongo/asynchronous/mongo_client.py:971:41 error[invalid-argument-type] Argument to bound method `AsyncMongoClient._init_based_on_options` is incorrect: Expected `Collection[tuple[str, int]]`, found `set[Unknown | tuple[str, int | None]] & ~AlwaysFalsy`
+ pymongo/synchronous/mongo_client.py:893:37 error[invalid-argument-type] Argument to bound method `MongoClient._init_based_on_options` is incorrect: Expected `Collection[tuple[str, int]]`, found `set[Unknown | tuple[str, int | None]] & ~AlwaysFalsy`
+ pymongo/synchronous/mongo_client.py:972:41 error[invalid-argument-type] Argument to bound method `MongoClient._init_based_on_options` is incorrect: Expected `Collection[tuple[str, int]]`, found `set[Unknown | tuple[str, int | None]] & ~AlwaysFalsy`
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/internals/construction.py:430:21 error[invalid-argument-type] Argument to function `construct_1d_arraylike_from_scalar` is incorrect: Expected `str | bytes | date | ... omitted 10 union elements`, found `object`
+ pandas/core/internals/construction.py:434:17 error[invalid-assignment] Invalid subscript assignment with key of type `int` and value of type `ExtensionArray | ndarray[tuple[Any, ...], dtype[Any]]` on object of type `list[int | float]`
pyppeteer (https://github.com/pyppeteer/pyppeteer)
- pyppeteer/launcher.py:148:40 warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive
scikit-learn (https://github.com/scikit-learn/scikit-learn)
+ sklearn/linear_model/_logistic.py:2402:30 error[no-matching-overload] No overload of function `tile` matches arguments
- sklearn/linear_model/tests/test_logistic.py:1940:12 error[unresolved-attribute] Attribute `shape` is not defined on `list[Unknown]`, `int`, `float` in union `list[Unknown] | ndarray[tuple[Any, ...], dtype[Unknown]] | int | float`
+ sklearn/linear_model/tests/test_logistic.py:1940:12 error[unresolved-attribute] Attribute `shape` is not defined on `list[Any]`, `int`, `float` in union `list[Any] | ndarray[tuple[Any, ...], dtype[Unknown]] | int | float`
- sklearn/linear_model/tests/test_logistic.py:1941:12 error[unresolved-attribute] Attribute `shape` is not defined on `list[Unknown]`, `int`, `float` in union `list[Unknown] | ndarray[tuple[Any, ...], dtype[Unknown]] | int | float`
+ sklearn/linear_model/tests/test_logistic.py:1941:12 error[unresolved-attribute] Attribute `shape` is not defined on `list[Unknown | str | None | int | float]`, `int`, `float` in union `list[Unknown | str | None | int | float] | Unknown | ndarray[tuple[Any, ...], dtype[Any]] | int | float`
+ sklearn/linear_model/tests/test_logistic.py:1946:9 error[no-matching-overload] No overload of function `assert_allclose` matches arguments
+ sklearn/linear_model/tests/test_logistic.py:1995:5 error[no-matching-overload] No overload of function `assert_allclose` matches arguments
+ sklearn/metrics/tests/test_common.py:1305:13 error[invalid-assignment] Object of type `str` is not assignable to attribute `__name__` on type `(Unknown & Top[partial[Unknown]]) | partial[(...) -> Unknown] | (((...) -> Unknown) & Top[partial[Unknown]])`
scipy (https://github.com/scipy/scipy)
+ scipy/conftest.py:661:55 error[invalid-argument-type] Argument to function `filterwarnings` is incorrect: Expected `bool`, found `<class 'RuntimeWarning'> | str | <class 'DeprecationWarning'> | ... omitted 3 union elements`
+ scipy/conftest.py:661:55 error[invalid-argument-type] Argument to function `filterwarnings` is incorrect: Expected `int`, found `<class 'RuntimeWarning'> | str | <class 'DeprecationWarning'> | ... omitted 3 union elements`
+ scipy/conftest.py:661:55 error[invalid-argument-type] Argument to function `filterwarnings` is incorrect: Expected `str`, found `<class 'RuntimeWarning'> | str | <class 'DeprecationWarning'> | ... omitted 3 union elements`
+ scipy/conftest.py:661:55 error[invalid-argument-type] Argument to function `filterwarnings` is incorrect: Expected `str`, found `<class 'RuntimeWarning'> | str | <class 'DeprecationWarning'> | ... omitted 3 union elements`
+ scipy/conftest.py:661:55 error[invalid-argument-type] Argument to function `filterwarnings` is incorrect: Expected `type[Warning]`, found `<class 'RuntimeWarning'> | str | <class 'DeprecationWarning'> | ... omitted 3 union elements`
+ scipy/spatial/tests/test_distance.py:615:14 error[no-matching-overload] No overload of function `cdist` matches arguments
+ scipy/spatial/tests/test_distance.py:664:14 error[no-matching-overload] No overload of function `cdist` matches arguments
+ scipy/spatial/tests/test_distance.py:665:14 error[no-matching-overload] No overload of function `cdist` matches arguments
+ scipy/spatial/tests/test_distance.py:677:15 error[no-matching-overload] No overload of function `cdist` matches arguments
+ scipy/spatial/tests/test_distance.py:1394:19 error[no-matching-overload] No overload of function `pdist` matches arguments
+ scipy/spatial/tests/test_distance.py:1432:14 error[no-matching-overload] No overload of function `pdist` matches arguments
+ scipy/spatial/tests/test_distance.py:1433:14 error[no-matching-overload] No overload of function `pdist` matches arguments
sympy (https://github.com/sympy/sympy)
+ sympy/simplify/radsimp.py:206:35 error[unresolved-attribute] Attribute `base` is not defined on `Expr` in union `Expr | Unknown`
- sympy/simplify/radsimp.py:708:28 error[not-iterable] Object of type `set[Unknown] | Basic` may not be iterable
+ sympy/simplify/radsimp.py:708:28 error[not-iterable] Object of type `set[Expr] | Basic` may not be iterable2471566 to
0ed9d0c
Compare
ibraheemdev
approved these changes
Jun 26, 2026
ibraheemdev
left a comment
Member
There was a problem hiding this comment.
Cool!
I wonder if it makes sense to extend this (as a followup) to non-empty collection constructor calls, e.g., x = list((1, 2, 3)); x.append("4") for consistency with literals, but it may end up being somewhat unintuitive.
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
Unannotated collection literals participate in full-scope bidirectional inference, but empty constructor calls (
list(),set(), anddict()) did not. For example, we previously accepted this function becauseresultremainedset[Unknown]:This PR treats bare, zero-argument
list(),set(), anddict()assignment values as collection initializers in the semantic index and reuses the existing collection-use constraint inference. (For now, we don't support aliased or attribute-accessed calls.)Closes astral-sh/ty#3573.