[ty] Improve equality-based narrowing#25788
Draft
charliermarsh wants to merge 8 commits into
Draft
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 92.23%. The percentage of expected errors that received a diagnostic held steady at 87.42%. The number of fully passing files held steady at 92/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownflake8
trio
sphinx
prefect
|
Merging this PR will not alter performance
Comparing Footnotes
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unsupported-operator |
0 | 0 | 12 |
invalid-argument-type |
0 | 1 | 8 |
type-assertion-failure |
0 | 0 | 1 |
| Total | 0 | 1 | 21 |
Raw diff (22 changes)
core (https://github.com/home-assistant/core)
- homeassistant/components/lutron/light.py:91:50 error[invalid-argument-type] Argument to function `to_lutron_level` is incorrect: Expected `int`, found `Any | (int & ~Literal[0]) | None`
+ homeassistant/components/lutron/light.py:91:50 error[invalid-argument-type] Argument to function `to_lutron_level` is incorrect: Expected `int`, found `Any | (int & ~Literal[0] & ~Literal[False]) | None`
hydpy (https://github.com/hydpy-dev/hydpy)
- hydpy/core/modeltools.py:3163:25 error[type-assertion-failure] Type `Any & ~Literal[0] & ~Literal[1]` is not equivalent to `Never`
+ hydpy/core/modeltools.py:3163:25 error[type-assertion-failure] Type `Any & ~Literal[0] & ~Literal[False] & ~Literal[1] & ~Literal[True]` is not equivalent to `Never`
paasta (https://github.com/yelp/paasta)
- paasta_tools/kubernetes_tools.py:951:13 error[invalid-argument-type] Argument to bound method `KubernetesDeploymentConfig.get_autoscaling_scaling_policy` is incorrect: Expected `int`, found `(int & ~Literal[0]) | None`
+ paasta_tools/kubernetes_tools.py:951:13 error[invalid-argument-type] Argument to bound method `KubernetesDeploymentConfig.get_autoscaling_scaling_policy` is incorrect: Expected `int`, found `(int & ~Literal[0] & ~Literal[False]) | None`
pandas (https://github.com/pandas-dev/pandas)
- pandas/core/arrays/datetimes.py:3233:28 error[unsupported-operator] Operator `-` is not supported between objects of type `(int & ~Literal[1]) | None` and `Literal[1]`
+ pandas/core/arrays/datetimes.py:3233:28 error[unsupported-operator] Operator `-` is not supported between objects of type `(int & ~Literal[1] & ~Literal[True]) | None` and `Literal[1]`
- pandas/core/arrays/datetimes.py:3242:28 error[unsupported-operator] Operator `-` is not supported between objects of type `(int & ~Literal[1]) | None` and `Literal[1]`
+ pandas/core/arrays/datetimes.py:3242:28 error[unsupported-operator] Operator `-` is not supported between objects of type `(int & ~Literal[1] & ~Literal[True]) | None` and `Literal[1]`
- pandas/core/frame.py:13859:42 error[invalid-argument-type] Argument to bound method `DataFrame.shift` is incorrect: Expected `int | Sequence[int]`, found `(int & ~Literal[0]) | integer[Any]`
+ pandas/core/frame.py:13859:42 error[invalid-argument-type] Argument to bound method `DataFrame.shift` is incorrect: Expected `int | Sequence[int]`, found `(int & ~Literal[0] & ~Literal[False]) | integer[Any]`
- pandas/core/reshape/tile.py:435:31 error[invalid-argument-type] Argument to function `abs` is incorrect: Expected `SupportsAbs[Unknown]`, found `~Literal[0]`
+ pandas/core/reshape/tile.py:435:31 error[invalid-argument-type] Argument to function `abs` is incorrect: Expected `SupportsAbs[Unknown]`, found `~Literal[0] & ~Literal[False]`
- pandas/core/reshape/tile.py:436:31 error[invalid-argument-type] Argument to function `abs` is incorrect: Expected `SupportsAbs[Unknown]`, found `~Literal[0]`
+ pandas/core/reshape/tile.py:436:31 error[invalid-argument-type] Argument to function `abs` is incorrect: Expected `SupportsAbs[Unknown]`, found `~Literal[0] & ~Literal[False]`
pwndbg (https://github.com/pwndbg/pwndbg)
- pwndbg/aglib/onegadget.py:366:104 error[invalid-argument-type] Argument to function `string` is incorrect: Expected `int`, found `(int & ~Literal[0]) | None`
+ pwndbg/aglib/onegadget.py:366:104 error[invalid-argument-type] Argument to function `string` is incorrect: Expected `int`, found `(int & ~Literal[0] & ~Literal[False]) | None`
scikit-learn (https://github.com/scikit-learn/scikit-learn)
- sklearn/preprocessing/_polynomial.py:533:31 error[unsupported-operator] Operator `+` is not supported between objects of type `~Literal[0]` and `Literal[1]`
+ sklearn/preprocessing/_polynomial.py:533:31 error[unsupported-operator] Operator `+` is not supported between objects of type `~Literal[0] & ~Literal[False]` and `Literal[1]`
scipy (https://github.com/scipy/scipy)
- scipy/interpolate/_polyint.py:109:33 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0]) | None`
+ scipy/interpolate/_polyint.py:109:33 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0] & ~Literal[False]) | None`
- scipy/interpolate/_polyint.py:110:49 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0]) | None`
+ scipy/interpolate/_polyint.py:110:49 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0] & ~Literal[False]) | None`
- scipy/interpolate/_polyint.py:193:41 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0]) | None`
+ scipy/interpolate/_polyint.py:193:41 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0] & ~Literal[False]) | None`
- scipy/interpolate/_polyint.py:195:29 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0]) | None`
+ scipy/interpolate/_polyint.py:195:29 error[unsupported-operator] Operator `+` is not supported between objects of type `int` and `(Unknown & ~Literal[0] & ~Literal[False]) | None`
- scipy/stats/_distribution_infrastructure.py:1914:12 error[unsupported-operator] Operator `>` is not supported between objects of type `generic[Any] | (int & ~Literal[1]) | float | ... omitted 4 union elements` and `Literal[1]`
+ scipy/stats/_distribution_infrastructure.py:1914:12 error[unsupported-operator] Operator `>` is not supported between objects of type `generic[Any] | (int & ~Literal[1] & ~Literal[True]) | float | ... omitted 4 union elements` and `Literal[1]`
- subprojects/array_api_extra/src/array_api_extra/_lib/_funcs.py:614:34 error[invalid-argument-type] Argument to function `log2` is incorrect: Expected `SupportsFloat | SupportsIndex`, found `(int & ~Literal[0]) | None`
+ subprojects/array_api_extra/src/array_api_extra/_lib/_funcs.py:614:34 error[invalid-argument-type] Argument to function `log2` is incorrect: Expected `SupportsFloat | SupportsIndex`, found `(int & ~Literal[0] & ~Literal[False]) | None`
sympy (https://github.com/sympy/sympy)
- sympy/combinatorics/prufer.py:291:13 error[unsupported-operator] Operator `-=` is not supported between objects of type `None` and `None | (Unknown & ~Literal[0])`
+ sympy/combinatorics/prufer.py:291:13 error[unsupported-operator] Operator `-=` is not supported between objects of type `None` and `None | (Unknown & ~Literal[0] & ~Literal[False])`
- sympy/ntheory/tests/test_factor_.py:64:16 error[unsupported-operator] Operator `%` is not supported between objects of type `~AlwaysFalsy & ~Literal[1]` and `Literal[2]`
+ sympy/ntheory/tests/test_factor_.py:64:16 error[unsupported-operator] Operator `%` is not supported between objects of type `~AlwaysFalsy & ~Literal[1] & ~Literal[True]` and `Literal[2]`
- sympy/ntheory/tests/test_factor_.py:66:33 error[unsupported-operator] Operator `//` is not supported between objects of type `~AlwaysFalsy & ~Literal[1]` and `Literal[2]`
+ sympy/ntheory/tests/test_factor_.py:66:33 error[unsupported-operator] Operator `//` is not supported between objects of type `~AlwaysFalsy & ~Literal[1] & ~Literal[True]` and `Literal[2]`
- sympy/polys/polytools.py:4988:25 error[invalid-argument-type] Argument to bound method `Poly.degree` is incorrect: Expected `int`, found `Expr`
- sympy/solvers/diophantine/diophantine.py:3833:43 error[invalid-argument-type] Argument to function `_can_do_sum_of_squares` is incorrect: Expected `int`, found `(int & ~Literal[0]) | MPZ`
+ sympy/solvers/diophantine/diophantine.py:3833:43 error[invalid-argument-type] Argument to function `_can_do_sum_of_squares` is incorrect: Expected `int`, found `(int & ~Literal[0] & ~Literal[False]) | MPZ`
xarray (https://github.com/pydata/xarray)
- xarray/plot/utils.py:305:49 error[unsupported-operator] Operator `-` is not supported between objects of type `Hashable & ~Literal[1]` and `Literal[1]`
+ xarray/plot/utils.py:305:49 error[unsupported-operator] Operator `-` is not supported between objects of type `Hashable & ~Literal[1] & ~Literal[True]` and `Literal[1]`35f160f to
b0d6ec8
Compare
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
This revives the equality-narrowing work from #22799 and gives
==and!=separate evaluators. Python can define__eq__and__ne__independently, so deriving one operator from the other can make reachable branches appear impossible.The evaluator handles the comparison domains where ty knows the runtime semantics, including primitive literals, enums that inherit builtin comparison methods, and identity-based singleton values. It remains conservative for arbitrary user-defined comparison methods, preserving the existing behavior on
mainwhere the comparison result cannot be proven.This improves narrowing for comparisons such as:
Value-pattern reachability now uses the same equality semantics instead of type equivalence. This recognizes that a
StrEnummember and a string literal with the same value compare equal in amatchstatement.Closes astral-sh/ty#2732.
Closes astral-sh/ty#1454.