Skip to content

Conversation

@AlexWaygood
Copy link
Member

Summary

#18987 only partly implemented the special-casing required to make tuple instantiations sound. While that PR made instantiations of tuple itself sound, it did not make instantiations of tuple subclasses sound, as the way in which that PR added the special casing did not mean that the synthesized constructor signatures would be automatically inherited by subclasses. This PR fixes that by moving the special casing to a lower level, inside ClassType::own_class_member.

Test Plan

Mdtests added. Some existing mdtests also had to be slightly modified because they were unsoundly constructing instances of tuple subclasses.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jul 21, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 21, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@carljm
Copy link
Contributor

carljm commented Jul 21, 2025

The new diagnostics look weird, though, because I would expect <class 'tuple'> to be assignable to (Unknown, /) -> Unknown.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jul 21, 2025

The new diagnostics look weird, though, because I would expect <class 'tuple'> to be assignable to (Unknown, /) -> Unknown.

yeah, that's why it's still in draft :P I'm looking into it!

@AlexWaygood
Copy link
Member Author

Minimal repro: this still passes on this branch:

from typing import Callable, Any

def f(x: Callable[..., Any]): ...

f(tuple)

but this no longer does:

from typing import Callable, Any

def f(x: Callable[[Any], Any]): ...

f(tuple)

@AlexWaygood
Copy link
Member Author

The reason seems to be that this .into_callable(db) call is now returning Callable[[], tuple[Unknown, ...]] for <class 'tuple'>:

(_, Type::Callable(_)) => self
.into_callable(db)
.is_some_and(|callable| callable.has_relation_to(db, target, relation)),

@AlexWaygood
Copy link
Member Author

Oh, I think the bug is just astral-sh/ty#760 again, but for __new__ this time rather than __init__

@AlexWaygood
Copy link
Member Author

Great, 3838203 fixed it! This now has a clean primer report.

@AlexWaygood AlexWaygood marked this pull request as ready for review July 21, 2025 19:34
@AlexWaygood AlexWaygood force-pushed the alex/tuple-sub-constructors branch from 3838203 to 4e3acfc Compare July 21, 2025 21:15
@AlexWaygood AlexWaygood force-pushed the alex/tuple-sub-constructors branch from 4e3acfc to 0577d51 Compare July 21, 2025 21:21
@AlexWaygood AlexWaygood enabled auto-merge (squash) July 21, 2025 21:22
@AlexWaygood AlexWaygood merged commit cb5a9ff into main Jul 21, 2025
36 checks passed
@AlexWaygood AlexWaygood deleted the alex/tuple-sub-constructors branch July 21, 2025 21:25
dcreager added a commit that referenced this pull request Jul 22, 2025
* main: (76 commits)
  Move fix suggestion to subdiagnostic (#19464)
  [ty] Implement non-stdlib stub mapping for classes and functions (#19471)
  [ty] Disallow illegal uses of `ClassVar` (#19483)
  [ty] Disallow `Final` in function parameter/return-type annotations (#19480)
  [ty] Extend `Final` test suite (#19476)
  [ty] Minor change to diagnostic message for invalid Literal uses (#19482)
  [ty] Detect illegal non-enum attribute accesses in Literal annotation (#19477)
  [ty] Reduce size of `TypeInference` (#19435)
  Run MD tests for Markdown-only changes (#19479)
  Revert "[ty] Detect illegal non-enum attribute accesses in Literal annotation"
  [ty] Detect illegal non-enum attribute accesses in Literal annotation
  [ty] Added semantic token support for more identifiers (#19473)
  [ty] Make tuple subclass constructors sound (#19469)
  [ty] Pass down specialization to generic dataclass bases (#19472)
  [ty] Garbage-collect reachability constraints (#19414)
  [ty] Implicit instance attributes declared `Final` (#19462)
  [ty] Expansion of enums into unions of literals (#19382)
  [ty] Avoid rechecking the entire project when changing the opened files (#19463)
  [ty] Add warning for unknown `TY_MEMORY_REPORT` value (#19465)
  [ty] Sync vendored typeshed stubs (#19461)
  ...
dcreager added a commit that referenced this pull request Jul 22, 2025
* main:
  [ty] Use `ThinVec` for sub segments in `PlaceExpr` (#19470)
  [ty] Splat variadic arguments into parameter list (#18996)
  [`flake8-pyi`] Skip fix if all `Union` members are `None` (`PYI016`)  (#19416)
  Skip notebook with errors in ecosystem check (#19491)
  [ty] Consistent use of American english (in rules) (#19488)
  [ty] Support iterating over enums (#19486)
  Fix panic for illegal `Literal[…]` annotations with inner subscript expressions (#19489)
  Move fix suggestion to subdiagnostic (#19464)
  [ty] Implement non-stdlib stub mapping for classes and functions (#19471)
  [ty] Disallow illegal uses of `ClassVar` (#19483)
  [ty] Disallow `Final` in function parameter/return-type annotations (#19480)
  [ty] Extend `Final` test suite (#19476)
  [ty] Minor change to diagnostic message for invalid Literal uses (#19482)
  [ty] Detect illegal non-enum attribute accesses in Literal annotation (#19477)
  [ty] Reduce size of `TypeInference` (#19435)
  Run MD tests for Markdown-only changes (#19479)
  Revert "[ty] Detect illegal non-enum attribute accesses in Literal annotation"
  [ty] Detect illegal non-enum attribute accesses in Literal annotation
  [ty] Added semantic token support for more identifiers (#19473)
  [ty] Make tuple subclass constructors sound (#19469)
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.

4 participants