Skip to content

[ty] Track non-terminal-call constraints in global scope#23245

Merged
sharkdp merged 1 commit intomainfrom
david/module-scope-returnsnever
Mar 26, 2026
Merged

[ty] Track non-terminal-call constraints in global scope#23245
sharkdp merged 1 commit intomainfrom
david/module-scope-returnsnever

Conversation

@sharkdp
Copy link
Copy Markdown
Contributor

@sharkdp sharkdp commented Feb 12, 2026

Summary

preparatory work: #24207 #24203

We previously avoided recording IsNonTerminalCall calls in global scope (non-function scopes, to be precise) because we were worried about the performance implications. However, it turns out that this does not cause any slowdowns in our real world ty benchmarks (but see #24203). There is also no dramatic change in the ecosystem timings.

closes astral-sh/ty#2480

Ecosystem

Two false positives removed.

Test Plan

Adapted previously existing test

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Feb 12, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Feb 12, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 86.46%. The percentage of expected errors that received a diagnostic held steady at 80.68%. The number of fully passing files held steady at 67/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Feb 12, 2026

Memory usage report

Summary

Project Old New Diff Outcome
prefect 711.26MB 711.44MB +0.03% (184.68kB)
sphinx 264.18MB 264.26MB +0.03% (89.87kB)
trio 117.31MB 117.39MB +0.07% (80.63kB)
flake8 47.87MB 47.88MB +0.01% (6.10kB)

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
infer_expression_types_impl 61.37MB 61.49MB +0.19% (118.55kB)
semantic_index 169.62MB 169.66MB +0.02% (37.53kB)
Expression 8.67MB 8.69MB +0.29% (25.31kB)
infer_scope_types_impl 53.71MB 53.69MB -0.04% (20.32kB)
infer_expression_type_impl 13.76MB 13.77MB +0.05% (7.58kB)
infer_definition_types 88.31MB 88.31MB +0.01% (6.42kB)
infer_deferred_types 14.61MB 14.62MB +0.03% (4.29kB)
function_known_decorators 8.07MB 8.08MB +0.04% (3.71kB)
all_narrowing_constraints_for_expression 7.10MB 7.11MB +0.01% (580.00B)
CallableType 2.02MB 2.02MB +0.02% (504.00B)
all_negative_narrowing_constraints_for_expression 2.61MB 2.61MB +0.01% (388.00B)
IntersectionType 2.30MB 2.30MB +0.01% (168.00B)

sphinx

Name Old New Diff Outcome
infer_expression_types_impl 21.35MB 21.47MB +0.54% (118.43kB)
infer_scope_types_impl 15.53MB 15.47MB -0.40% (63.54kB)
semantic_index 60.42MB 60.44MB +0.02% (15.17kB)
Expression 3.17MB 3.18MB +0.40% (13.08kB)
CallableType 1.09MB 1.09MB +0.28% (3.09kB)
infer_expression_type_impl 2.95MB 2.95MB +0.09% (2.73kB)
infer_definition_types 23.59MB 23.59MB +0.00% (648.00B)
Type<'db>::member_lookup_with_policy_ 6.51MB 6.51MB +0.00% (132.00B)
place_by_id 1.37MB 1.37MB +0.01% (120.00B)
function_known_decorators 2.46MB 2.46MB +0.00% (12.00B)
infer_deferred_types 5.61MB 5.61MB +0.00% (12.00B)

trio

Name Old New Diff Outcome
infer_expression_types_impl 7.02MB 7.07MB +0.75% (53.60kB)
CallableType 563.86kB 577.99kB +2.51% (14.13kB)
semantic_index 29.58MB 29.59MB +0.04% (13.27kB)
infer_scope_types_impl 4.78MB 4.77MB -0.27% (13.07kB)
Expression 1.41MB 1.42MB +0.60% (8.72kB)
infer_expression_type_impl 1.32MB 1.32MB +0.23% (3.05kB)
infer_definition_types 7.49MB 7.49MB +0.01% (504.00B)
BoundMethodType<'db>::into_callable_type_ 73.49kB 73.72kB +0.32% (240.00B)
function_known_decorators 921.70kB 921.80kB +0.01% (96.00B)
infer_deferred_types 2.38MB 2.38MB +0.00% (96.00B)
overloads_and_implementation_inner 326.40kB 326.41kB +0.00% (12.00B)

flake8

Name Old New Diff Outcome
infer_expression_types_impl 1.06MB 1.07MB +0.66% (7.12kB)
infer_scope_types_impl 999.89kB 996.46kB -0.34% (3.43kB)
semantic_index 13.51MB 13.51MB +0.01% (1.02kB)
Expression 365.70kB 366.54kB +0.23% (864.00B)
CallableType 167.98kB 168.26kB +0.17% (288.00B)
infer_expression_type_impl 141.81kB 141.97kB +0.11% (160.00B)
BoundMethodType<'db>::into_callable_type_ 27.63kB 27.73kB +0.34% (96.00B)
infer_definition_types 1.82MB 1.82MB +0.00% (24.00B)

@sharkdp sharkdp changed the title [ty] Track ReturnsNever constraints in global scope [ty] Track ReturnsNever constraints in global scope Feb 12, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 12, 2026

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 30 skipped benchmarks1


Comparing david/module-scope-returnsnever (761588f) with main (4704c2a)

Open in CodSpeed

Footnotes

  1. 30 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Feb 12, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-await 0 40 0
invalid-argument-type 0 2 0
invalid-return-type 0 1 0
Total 0 43 0

Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.

Raw diff:

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- benchmarks/base/run.py:114:45 error[invalid-argument-type] Argument to function `cpu_affinity_to_cpu_groups` is incorrect: Expected `str`, found `str | None`

vision (https://github.com/pytorch/vision)
- .github/process_commit.py:77:47 error[invalid-argument-type] Argument to function `get_pr_merger_and_labels` is incorrect: Expected `int`, found `int | None`

Full report with detailed diff (timing results)

@sharkdp

This comment was marked as resolved.

@sharkdp sharkdp force-pushed the david/module-scope-returnsnever branch from ea28e5e to 856c2b2 Compare March 26, 2026 10:07
@sharkdp sharkdp changed the title [ty] Track ReturnsNever constraints in global scope [ty] Track IsNonTerminalCall constraints in global scope Mar 26, 2026
sharkdp added a commit that referenced this pull request Mar 26, 2026
## Summary

This changes the `many_enum_members` benchmark from this structure:

```py
class E(Enum):
    m1 = 1
    m2 = 2
    ...

print(E.m1)
print(E.m2)
...
```

to this structure:

```py
class E(Enum):
    m1 = 1
    m2 = 2
    ...

print((E.m1, E.m2, …))
```

The idea here is to make this benchmark less susceptible to changes in
our reachability constraints (that we record for `print` calls). The
idea of the `print` statements was only to have "uses" of these enum
members. It was never the idea to be an example of 512 function calls in
sequence.

I'm certainly not trying to hide the fact that this benchmark revealed a
huge regression in #23245. But it
did so for the wrong reasons. We would have already had that regression
on `main` if those `print` calls would have been inside another function
scope. And so it seems fair to change the benchmark before we proceed
with #23245 (which shows no regressions elsewhere, and even leads to
performance improvements in real world projects).
sharkdp added a commit that referenced this pull request Mar 26, 2026
## Summary

Some mdtest changes in preparation for
#23245.
@sharkdp sharkdp force-pushed the david/module-scope-returnsnever branch from 99d7273 to 8d3e3ea Compare March 26, 2026 13:16
@astral-sh astral-sh deleted a comment from astral-sh-bot bot Mar 26, 2026
@sharkdp sharkdp force-pushed the david/module-scope-returnsnever branch 3 times, most recently from ee10221 to 3a27f34 Compare March 26, 2026 13:26
@sharkdp sharkdp changed the title [ty] Track IsNonTerminalCall constraints in global scope [ty] Track non-terminal call constraints in global scope Mar 26, 2026
@sharkdp sharkdp changed the title [ty] Track non-terminal call constraints in global scope [ty] Track non-terminal-call constraints in global scope Mar 26, 2026
@sharkdp sharkdp marked this pull request as ready for review March 26, 2026 13:33
.record_narrowing_constraint_for_all_places(constraint);
} else {
// In non-function scopes, we only record a narrowing constraint
// (no a reachability constraints). Recording reachability for
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// (no a reachability constraints). Recording reachability for
// (not a reachability constraint). Recording reachability for

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is marked as resolved but I still see the typos 😆

@sharkdp sharkdp force-pushed the david/module-scope-returnsnever branch from 3a27f34 to 761588f Compare March 26, 2026 13:47
@sharkdp sharkdp enabled auto-merge (squash) March 26, 2026 13:47
@sharkdp sharkdp merged commit 683bae5 into main Mar 26, 2026
48 checks passed
@sharkdp sharkdp deleted the david/module-scope-returnsnever branch March 26, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing type narrowing for NoReturn control flow at module scope

3 participants