Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Make overload public type reachability-aware#25171

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/reachability-overload
May 15, 2026
Merged

[ty] Make overload public type reachability-aware#25171
charliermarsh merged 1 commit into
mainfrom
charlie/reachability-overload

Conversation

@charliermarsh

@charliermarsh charliermarsh commented May 14, 2026

Copy link
Copy Markdown
Member

Summary

This PR makes public type construction for overloaded functions respect reachability.

Before this change, we treated an overloaded functio followed by any non-overload function as an overload set followed by its implementation. That works for ordinary overloads:

@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
def f(x: int | str) -> int | str: ...

But, it's wrong if those definitions are behind control-flow:

from module import f

if flag():
    g = f
else:
    def g(x: bytes) -> bytes:
        return x

Here, the fallback implementation in the else is not guaranteed to replace the imported overload set. It is just the public value of g on one branch -- we need to use the union of the two definitions. (Previously, we would silently drop the overloaded branch and expose only the fallback function.)

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 14, 2026
@astral-sh-bot

astral-sh-bot Bot commented May 14, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134.

@astral-sh-bot

astral-sh-bot Bot commented May 14, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@charliermarsh charliermarsh changed the base branch from main to charlie/cont May 14, 2026 23:20
@astral-sh-bot

astral-sh-bot Bot commented May 14, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
unsupported-operator 2 0 0
Total 2 0 0

Raw diff:

scikit-learn (https://github.com/scikit-learn/scikit-learn)
+ sklearn/manifold/_spectral_embedding.py:421:9 error[unsupported-operator] Operator `-=` is not supported between objects of type `csr_array[Any, tuple[int, int]]` and `dia_array[float64] | Unknown`
+ sklearn/manifold/_spectral_embedding.py:421:9 error[unsupported-operator] Operator `-=` is not supported between objects of type `csr_matrix[Any]` and `dia_array[float64] | Unknown`

Full report with detailed diff (timing results)

Base automatically changed from charlie/cont to main May 14, 2026 23:22
@charliermarsh charliermarsh force-pushed the charlie/reachability-overload branch from b0092f4 to 353324c Compare May 14, 2026 23:22
def g(x: bytes) -> bytes:
return x

reveal_type(g) # revealed: (Overload[(x: int) -> int, (x: str) -> str]) | (def g(x: bytes) -> bytes)

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.

On main, this reveals def g(x: bytes) -> bytes.

@charliermarsh charliermarsh marked this pull request as ready for review May 14, 2026 23:30
@astral-sh-bot astral-sh-bot Bot requested a review from dhruvmanila May 14, 2026 23:30
@dhruvmanila

Copy link
Copy Markdown
Member

Did you look at the ecosystem diff? There are two new diagnostics

@charliermarsh

Copy link
Copy Markdown
Member Author

Yeah, it's correct! Before, we saw:

if not SCIPY_VERSION_BELOW_1_12:
    _sparse_eye_array = scipy.sparse.eye_array

else:

    def _sparse_eye_array(m, n=None, *, k=0, dtype=float, format=None):
        A = scipy.sparse.eye(m, n, k=k, dtype=dtype)
        return scipy.sparse.dia_array(A).asformat(format)

And ignored the first _sparse_eye_array = scipy.sparse.eye_array; so we only looked at _sparse_eye_array, which returns Unknown.

@charliermarsh charliermarsh merged commit 6bd3a84 into main May 15, 2026
57 of 59 checks passed
@charliermarsh charliermarsh deleted the charlie/reachability-overload branch May 15, 2026 10:51
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
## Summary

This PR makes public type construction for overloaded functions respect
reachability.

Before this change, we treated an overloaded functio followed by any
non-overload function as an overload set followed by its implementation.
That works for ordinary overloads:

```python
@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
def f(x: int | str) -> int | str: ...
```

But, it's wrong if those definitions are behind control-flow:

```python
from module import f

if flag():
    g = f
else:
    def g(x: bytes) -> bytes:
        return x
```

Here, the fallback implementation in the `else` is _not_ guaranteed to
replace the imported overload set. It is just the public value of `g` on
one branch -- we need to use the _union_ of the two definitions.
(Previously, we would silently drop the overloaded branch and expose
only the fallback function.)
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
## Summary

This PR makes public type construction for overloaded functions respect
reachability.

Before this change, we treated an overloaded functio followed by any
non-overload function as an overload set followed by its implementation.
That works for ordinary overloads:

```python
@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
def f(x: int | str) -> int | str: ...
```

But, it's wrong if those definitions are behind control-flow:

```python
from module import f

if flag():
    g = f
else:
    def g(x: bytes) -> bytes:
        return x
```

Here, the fallback implementation in the `else` is _not_ guaranteed to
replace the imported overload set. It is just the public value of `g` on
one branch -- we need to use the _union_ of the two definitions.
(Previously, we would silently drop the overloaded branch and expose
only the fallback function.)
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.

2 participants