Added reportUnreachable diagnostic check. If enabled, it emits a di…#10581
Merged
Added reportUnreachable diagnostic check. If enabled, it emits a di…#10581
reportUnreachable diagnostic check. If enabled, it emits a di…#10581Conversation
…agnostic for code that is determined to be structurally unreachable or unreachable via type analysis. It does not report code that is within a code block that is gated by a conditional that is statically determined to be false, such as `TYPE_CHECKING` and version checks. This diagnostic check is off by default even in strict mode because there are legitimate reasons for unreachable code to exist in Python code. This change also modifies the messages that accompany tagged hints (the mechanism by which unreachable or unanalyzed code is displayed as "dimmed" in an editor). The message now distinguishes between code that is structurally unreachable (e.g. after a `return` or `raise` statement) and code that is not analyzed because it is in a conditional block that is statically evaluated as false. This addresses #10284.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
+ .../projects/sympy/sympy/solvers/tests/test_solveset.py:444:30 - error: Cannot access attribute "limit_denominator" for class "NaN"
+ Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/tests/test_solveset.py:444:30 - error: Cannot access attribute "limit_denominator" for class "ComplexInfinity"
+ Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/stats/crv_types.py:2542:9 - error: Method "_cdf" overrides class "SingleContinuousDistribution" in an incompatible manner
+ Return type mismatch: base method returns type "None", override returns type "ComplexInfinity | NaN | Rational | Unknown"
+ Type "ComplexInfinity | NaN | Rational | Unknown" is not assignable to type "None"
+ "ComplexInfinity" is not assignable to "None" (reportIncompatibleMethodOverride)
+ .../projects/sympy/sympy/stats/crv_types.py:2721:9 - error: Method "_cdf" overrides class "SingleContinuousDistribution" in an incompatible manner
+ Return type mismatch: base method returns type "None", override returns type "Expr | NaN | ComplexInfinity | Rational | Unknown"
+ Type "Expr | NaN | ComplexInfinity | Rational | Unknown" is not assignable to type "None"
+ "Expr" is not assignable to "None" (reportIncompatibleMethodOverride)
- .../projects/sympy/sympy/stats/drv.py:268:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
+ .../projects/sympy/sympy/stats/drv.py:268:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum* | Expr | Any | Add | Zero | NaN | Piecewise | Basic | Sum | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
- "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
+ "Generator[tuple[Unknown, ...] | Unknown | Sum* | Expr | Any | Add | Zero | NaN | Piecewise | Basic | Sum | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
- Type parameter "_T_co@Iterable" is covariant, but "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
+ Type parameter "_T_co@Iterable" is covariant, but "tuple[Unknown, ...] | Unknown | Sum* | Expr | Any | Add | Zero | NaN | Piecewise | Basic | Sum | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
- Type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+ Type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Any | Add | Zero | NaN | Piecewise | Basic | Sum | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
- Type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+ Type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Any | Add | Zero | NaN | Piecewise | Basic | Sum | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+ .../projects/sympy/sympy/stats/frv_types.py:138:17 - error: Argument of type "NaN | ComplexInfinity | Rational | Unknown | Expr" cannot be assigned to parameter "value" of type "int" in function "__setitem__"
+ Type "NaN | ComplexInfinity | Rational | Unknown | Expr" is not assignable to type "int"
+ "Expr" is not assignable to "int" (reportArgumentType)
- .../projects/sympy/sympy/stats/joint_rv_types.py:575:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
+ .../projects/sympy/sympy/stats/joint_rv_types.py:575:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Number | Expr" is not assignable to type "SupportsIndex"
+ Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
- .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | Any | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Number | Infinity | NegativeInfinity | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+ .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum* | Add | Piecewise | Basic | Sum | Any | NaN | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
- Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | Any | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Number | Infinity | NegativeInfinity | int" is not assignable to type "Expr | complex"
+ Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum* | Add | Piecewise | Basic | Sum | Any | NaN | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" is not assignable to type "Expr | complex"
- .../projects/sympy/sympy/stats/stochastic_process_types.py:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Piecewise | Basic | Sum | Any | Zero | NaN | Integral | int | None" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+ .../projects/sympy/sympy/stats/stochastic_process_types.py:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Add | Piecewise | Basic | Sum | Any | Zero | NaN | Integral | int | None" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
- Type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Piecewise | Basic | Sum | Any | Zero | NaN | Integral | int | None" is not assignable to type "Expr | complex"
+ Type "tuple[Unknown, ...] | Unknown | Sum* | Expr | Add | Piecewise | Basic | Sum | Any | Zero | NaN | Integral | int | None" is not assignable to type "Expr | complex"
- Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Piecewise | Sum | Any | Zero | NaN | Integral | Self@Expectation | Literal[0] | None"
+ Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Add | Piecewise | Sum | Any | Zero | NaN | Integral | Self@Expectation | Literal[0] | None"
- Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Piecewise | Sum | Any | Zero | NaN | Integral | Expectation* | Literal[0] | None" is not assignable to type "Basic"
+ Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Add | Piecewise | Sum | Any | Zero | NaN | Integral | Expectation* | Literal[0] | None" is not assignable to type "Basic"
- Return type mismatch: base method returns type "Basic", override returns type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | NaN | Piecewise | Basic | ComplexInfinity | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0] | None"
+ Return type mismatch: base method returns type "Basic", override returns type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum* | Expr | Add | NaN | Piecewise | Basic | Sum | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0] | None"
- Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | NaN | Piecewise | Basic | ComplexInfinity | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0] | None" is not assignable to type "Basic"
+ Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum* | Expr | Add | NaN | Piecewise | Basic | Sum | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0] | None" is not assignable to type "Basic"
- Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Piecewise | Sum | Any | Zero | NaN | Integral | Self@Expectation | Literal[0] | None"
+ Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum* | Add | Piecewise | Sum | Any | Zero | NaN | Integral | Self@Expectation | Literal[0] | None"
... (truncated 1357 lines) ...
|
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.
…agnostic for code that is determined to be structurally unreachable or unreachable via type analysis. It does not report code that is within a code block that is gated by a conditional that is statically determined to be false, such as
TYPE_CHECKINGand version checks. This diagnostic check is off by default even in strict mode because there are legitimate reasons for unreachable code to exist in Python code.This change also modifies the messages that accompany tagged hints (the mechanism by which unreachable or unanalyzed code is displayed as "dimmed" in an editor). The message now distinguishes between code that is structurally unreachable (e.g. after a
returnorraisestatement) and code that is not analyzed because it is in a conditional block that is statically evaluated as false.This addresses #10284.