Skip to content

Commit 2abd683

Browse files
authored
[ty] Short circuit ReachabilityConstraints::analyze_single for dynamic types (#19867)
1 parent dc84645 commit 2abd683

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

crates/ty_python_semantic/resources/mdtest/function/return_type.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,16 @@ class Abstract(Protocol):
505505
class Concrete(Abstract):
506506
def method(self) -> str: ... # error: [invalid-return-type]
507507
```
508+
509+
## Diagnostics for `invalid-return-type` on dynamic type
510+
511+
```toml
512+
environment.python-version = "3.12"
513+
```
514+
515+
```py
516+
from typing import Never, Any
517+
518+
def f(func: Any) -> Never: # error: [invalid-return-type]
519+
func()
520+
```

crates/ty_python_semantic/src/semantic_index/reachability_constraints.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,17 @@ impl ReachabilityConstraints {
815815
// very large in number, since we add them on all statement level function calls.
816816
let ty = infer_expression_type(db, callable);
817817

818+
// Short-circuit for well known types that are known not to return `Never` when called.
819+
// Without the short-circuit, we've seen that threads keep blocking each other
820+
// because they all try to acquire Salsa's `CallableType` lock that ensures each type
821+
// is only interned once. The lock is so heavily congested because there are only
822+
// very few dynamic types, in which case Salsa's sharding the locks by value
823+
// doesn't help much.
824+
// See <https://github.com/astral-sh/ty/issues/968>.
825+
if matches!(ty, Type::Dynamic(_)) {
826+
return Truthiness::AlwaysFalse.negate_if(!predicate.is_positive);
827+
}
828+
818829
let overloads_iterator =
819830
if let Some(Type::Callable(callable)) = ty.into_callable(db) {
820831
callable.signatures(db).overloads.iter()

0 commit comments

Comments
 (0)