Skip to content

Avoid NoReturn analysis for Any or Unknown calls#11419

Open
bschnurr wants to merge 3 commits intomicrosoft:mainfrom
bschnurr:perf/no-return-unknown-guard
Open

Avoid NoReturn analysis for Any or Unknown calls#11419
bschnurr wants to merge 3 commits intomicrosoft:mainfrom
bschnurr:perf/no-return-unknown-guard

Conversation

@bschnurr
Copy link
Copy Markdown
Member

@bschnurr bschnurr commented May 6, 2026

Summary

Fixes NoReturn reachability analysis for call targets that include Any or Unknown.

A call target like Unknown | Callable[..., NoReturn] cannot establish a guaranteed NoReturn result. Previously, the callable subtype could be counted as NoReturn while the Unknown subtype did not force a negative result, so code after the call could be incorrectly marked unreachable.

This change makes isCallNoReturn return false as soon as any call-target subtype is Any or Unknown.

This also tightens related typeUtils subtype helper usage so paths that are checking “any subtype” or “all subtypes” can short-circuit correctly and are covered by direct unit tests.

Details

isCallNoReturn now uses an explicit subtype loop so it can fail fast while analyzing the call target:

const callSubtypes = isUnion(callType) ? callType.priv.subtypes : [callType];
for (let callSubtype of callSubtypes) {
    subtypeCount++;

    if (isAnyOrUnknown(callSubtype)) {
        return false;
    }

    ...
}

The previous callback-local return for custom metaclass __call__ handling is preserved as continue in the explicit loop.

The typeUtils follow-up includes:

  • Fix allSubtypes so the union branch returns the callback result from .every.
  • Use allSubtypes in isTypeVarSame.
  • Use someSubtypes in derivesFromAnyOrUnknown, checking each subtype rather than the original union.
  • Return early in getLiteralTypeClassName and reject mixed literal classes as documented.
  • Return early in combineSameSizedTuples when a subtype cannot participate.

Tests

Added regression cases for mixed Unknown/Any plus Callable[..., NoReturn] call targets, and added direct typeUtils unit coverage for:

  • allSubtypes
  • someSubtypes
  • derivesFromAnyOrUnknown
  • getLiteralTypeClassName
  • combineSameSizedTuples

Validation run for the latest commit:

packages/pyright-internal: npm run build
packages/pyright-internal: npm run test:norebuild -- --runTestsByPath src/tests/typeUtils.test.ts --forceExit

Earlier validation on the NoReturn regression path:

packages/pyright-internal: npx jest typeEvaluator1.test --runInBand --forceExit
packages/pyright-internal: npm run test:norebuild

Earlier results:

typeEvaluator1.test: 157 passed
full test suite: 57 suites passed, 2431 tests passed

Benchmark

I used a benchmark-branch-only generated analyzer benchmark, noreturn_heavy, to stress this specific path with repeated mixed Unknown/Any + Callable[..., NoReturn] call-target cases.

Initial focused analyzer benchmark results:

Baseline:
median: 6230.78ms
avg:    6202.06ms
NoReturn queries:        39837
NoReturn call type eval: 273.29ms
NoReturn subtype scan:   37.54ms
false Any/Unknown exits: 0

This change:
median: 6046.67ms
avg:    6146.16ms
NoReturn queries:        39837
NoReturn call type eval: 266.25ms
NoReturn subtype scan:   11.65ms
false Any/Unknown exits: 4803
only Any/Unknown exits:  803

Delta on the focused benchmark:

median:       -184.11ms, about 3.0% faster
subtype scan: -25.89ms, about 69% lower

After the typeUtils follow-up, I updated the benchmark worktree with the same helper fixes and reran the full analyzer benchmark. New result file:

src/tests/benchmarks/.generated/benchmark-results/analyzer/analyzer-benchmark-2026-05-06T23-24-52-295Z.json

Full analyzer benchmark medians from that run:

large_stdlib              1293.16ms
fstring_heavy              969.80ms
comment_heavy              957.00ms
large_class               1086.20ms
import_heavy              1135.31ms
union_heavy               1043.25ms
repetitive_identifiers     957.23ms
noreturn_heavy            5984.83ms

Compared with the prior noreturn_heavy-only run:

previous median: 6094.10ms
current median:  5984.83ms
delta:           -109.27ms (-1.79%)

I would characterize the performance impact as a targeted improvement for NoReturn-heavy mixed Any/Unknown call-target cases. The latest full benchmark still points in the same direction for noreturn_heavy, but the additional delta is small enough to treat as noise-level unless repeated runs keep showing the same shape.

Full NumPy CLI benchmarks were noisy and effectively neutral. Diagnostics and file counts matched across runs:

8501 errors, 1109 warnings, 0 informations
Total files parsed and bound: 1044
Total files checked: 700

@bschnurr bschnurr requested a review from erictraut May 6, 2026 20:41
@github-actions

This comment has been minimized.

Comment thread packages/pyright-internal/src/analyzer/codeFlowEngine.ts
Comment thread packages/pyright-internal/src/tests/samples/unreachable1.py
@github-actions

This comment has been minimized.

Update subtype helper paths to short-circuit where the result is known. Fix allSubtypes to return the callback result from every, use allSubtypes in isTypeVarSame, use someSubtypes in derivesFromAnyOrUnknown, and return early in getLiteralTypeClassName and combineSameSizedTuples.

Add direct typeUtils unit tests for allSubtypes, someSubtypes, derivesFromAnyOrUnknown, getLiteralTypeClassName, and combineSameSizedTuples.

Validation: npm run build; npm run test:norebuild -- --runTestsByPath src/tests/typeUtils.test.ts --forceExit.

Benchmark note: reran the benchmark worktree analyzer benchmark after porting these typeUtils fixes there. Full run passed and wrote analyzer-benchmark-2026-05-06T23-24-52-295Z.json. noreturn_heavy median was 5984.83ms vs 6094.10ms in the previous no-return-only run (-1.79%), which is small enough to treat as noise-level unless repeated.
if (isUnion(type)) {
return type.priv.subtypes.every((subtype) => {
callback(subtype);
return callback(subtype);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

every expects the return result from callback

const subtypeLiteralTypeName = getLiteralTypeClassName(subtype);
if (!subtypeLiteralTypeName) {
foundMismatch = true;
return undefined;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

use early exit

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

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_solvers.py:1214:18 - error: Operator "+" not supported for types "Basic" and "Expr" (reportOperatorIssue)
-   .../projects/sympy/sympy/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "NaN"
-     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
-   .../projects/sympy/sympy/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "ComplexInfinity"
-     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:315:25 - error: "Set" is not iterable
+     "__iter__" method not defined (reportGeneralTypeIssues)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:315:25 - error: "ConditionSet" is not iterable
+     "__iter__" method not defined (reportGeneralTypeIssues)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2024:20 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2036:20 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2049:20 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2070:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2071:20 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2252:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2333:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2346:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2352:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2519:16 - error: Argument of type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | FiniteSet | Set | Intersection | Union | Complement | Any | ConditionSet" is not assignable to type "Sized"
+       "Set" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:2522:20 - error: Argument of type "Unknown | Basic | Any" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+     Type "Unknown | Basic | Any" is not assignable to type "Sized"
+       "Basic" is incompatible with protocol "Sized"
+         "__len__" is not present (reportArgumentType)
-   .../projects/sympy/sympy/stats/crv_types.py:2544: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 | Zero | Expr"
-       Type "ComplexInfinity | NaN | Rational | Unknown | Zero | Expr" is not assignable to type "None"
-         "Expr" is not assignable to "None" (reportIncompatibleMethodOverride)
-   .../projects/sympy/sympy/stats/crv_types.py:2723: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 | One | NegativeOne | Zero | Integer"
-       Type "Expr | NaN | ComplexInfinity | Rational | Unknown | One | NegativeOne | Zero | Integer" is not assignable to type "None"
-         "Expr" is not assignable to "None" (reportIncompatibleMethodOverride)
-   .../projects/sympy/sympy/stats/drv.py:269:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | 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:269:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
-     "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
+     "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
-       Type parameter "_T_co@Iterable" is covariant, but "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | 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 | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
-         Type "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+         Type "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
-           Type "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+           Type "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
-   .../projects/sympy/sympy/stats/drv_types.py:293:16 - error: Operator "*" not supported for types "Expr" and "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic"
+   .../projects/sympy/sympy/stats/drv_types.py:293:16 - error: Operator "*" not supported for types "Expr" and "tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic"
-   .../projects/sympy/sympy/stats/frv_types.py:139: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:576: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__"
+   .../projects/sympy/sympy/stats/joint_rv_types.py:576:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | 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 | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
+     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"

... (truncated 754 lines) ...

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.

2 participants