Skip to content

Fix overloads with a generic self: dropped when matching a Protocol#3975

Open
jorenham wants to merge 1 commit into
facebook:mainfrom
jorenham:gh-3974
Open

Fix overloads with a generic self: dropped when matching a Protocol#3975
jorenham wants to merge 1 commit into
facebook:mainfrom
jorenham:gh-3974

Conversation

@jorenham

Copy link
Copy Markdown
Contributor

Summary

filter_overloads_by_self_type dropped any overload whose self: mentions the overload's own type params, since is_subset_eq(receiver, self) checks against a rigid, unsolvable variable. Substitute those params with Any first, so the receiver is matched against a gradual self:.
An overload whose self: mentions its own type parameters is matched gradually by substituting those parameters with Any (self: Arr[S, T] -> Arr[Any, Any]), which is exact for the realistic single-occurrence case and only over-accepts the presumably niche degenerate repeated-parameter case, (self: C[Z, Z]), same as mypy and pyright.

Fixes #3974

Test Plan

Regression tests added

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

jax (https://github.com/google/jax)
+ ERROR jax/_src/lax/linalg.py:1066:74-78: Argument `list[signedinteger[_32Bit]]` is not assignable to parameter `broadcast_dimensions` with type `Sequence[int]` in function `jax._src.lax.lax.broadcast_in_dim` [bad-argument-type]
+ ERROR jax/_src/lax/linalg.py:1068:63-67: Argument `list[signedinteger[_32Bit]]` is not assignable to parameter `broadcast_dimensions` with type `Sequence[int]` in function `jax._src.lax.lax.broadcast_in_dim` [bad-argument-type]

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ ERROR tests/sparse/test_csr.pyi:196:1-2: Unused `# pyrefly: ignore` comment for code(s): assert-type [unused-ignore]

DateType (https://github.com/glyph/DateType)
- ERROR src/datetype/test/test_datetype.py:49:38-54: `Time[None]` is not assignable to `NaiveTime` [bad-assignment]

colour (https://github.com/colour-science/colour)
- ERROR colour/temperature/cie_d.py:113:21-118:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/cie_d.py:113:21-118:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
- ERROR colour/temperature/hernandez1999.py:176:21-181:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=Unknown, args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/hernandez1999.py:176:21-181:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=Unknown, args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
- ERROR colour/temperature/kang2002.py:112:21-117:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/kang2002.py:112:21-117:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
- ERROR colour/temperature/krystek1985.py:117:21-122:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], uv: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/krystek1985.py:117:21-122:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], uv: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
- ERROR colour/temperature/mccamy1992.py:160:21-165:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=Unknown, args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/mccamy1992.py:160:21-165:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((xy: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=Unknown, args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
- ERROR colour/temperature/planck1900.py:120:21-125:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], uv: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[Any], **dict[str, str | dict[str, float]]) [no-matching-overload]
+ ERROR colour/temperature/planck1900.py:120:21-125:14: No matching overload found for function `scipy.optimize._minimize.minimize` called with arguments: ((CCT: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]], uv: ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]) -> DTypeFloat, x0=list[int], args=tuple[ndarray[tuple[Any, ...], dtype[float64 | floating[_16Bit] | floating[_32Bit]]]], **dict[str, str | dict[str, float]]) [no-matching-overload]
@jorenham

Copy link
Copy Markdown
Contributor Author
  • jax (src): two new true positives are now reported (previously false negatives). list(ndarray) now infers the precise scalar element type (via ndarray.__iter__'s generic-self overload, previously dropped), correctly flagging a list[np.int32] passed where Sequence[int] is expected.
  • scipy-stubs (src): this was indeed a false positive that is now resolved.
  • DateType (src): also looks like a resovled false positive that (they seem to be using a lot of self: Self in overloads, and there's a protocol involved here).
  • colour: all that changed here are the reported error messages; so +0

So all primer changes are good news.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment