Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jul 31, 2025

Summary

This PR refactors the generic fallback match arm in TypeInferenceBuilder::infer_subscript_expression_types into a closure that can be called from multiple places. This refactor has several advantages:

  • It's marginally less code, and it leads to cleaner code in many places: we need to have fewer .expect("Checked in branch arm") calls peppered across the function
  • It gives a (small, but very consistent) 1% speedup on many of the benchmarks. I think this is mostly because we no longer need to eagerly call .slice_literal(db) at the top of the function; instead, we only do it in the branch arms for Type variants that we know could plausibly represent a slice instance type
  • It makes further changes that I'd like to do (relating to support namedtuple and typing.NamedTuple ty#545) easier

Test Plan

All existing tests pass

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

github-actions bot commented Jul 31, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@AlexWaygood AlexWaygood force-pushed the alex/binary-method-fallbacks branch from 93cb638 to a96f92c Compare July 31, 2025 11:34
@github-actions
Copy link
Contributor

github-actions bot commented Jul 31, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@AlexWaygood AlexWaygood force-pushed the alex/binary-method-fallbacks branch from a96f92c to 9e58d47 Compare July 31, 2025 11:43
@AlexWaygood AlexWaygood force-pushed the alex/binary-method-fallbacks branch from 9e58d47 to 1b17d74 Compare July 31, 2025 11:44
@AlexWaygood AlexWaygood marked this pull request as ready for review July 31, 2025 12:01
@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Jul 31, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) July 31, 2025 13:36
@AlexWaygood AlexWaygood merged commit a3f28ba into main Jul 31, 2025
37 checks passed
@AlexWaygood AlexWaygood deleted the alex/binary-method-fallbacks branch July 31, 2025 13:38
dcreager added a commit that referenced this pull request Aug 1, 2025
* main: (39 commits)
  [ty] Initial test suite for `TypedDict` (#19686)
  [ty] Improve debuggability of protocol types (#19662)
  [ty] Simplify lifetime requirements for `PySlice` trait (#19687)
  [ty] Improve `isinstance()` truthiness analysis for generic types (#19668)
  [`refurb`] Make example error out-of-the-box (`FURB164`) (#19673)
  Fix link: unused_import.rs (#19648)
  [ty] Remove `Specialization::display` (full) (#19682)
  [ty] Remove `KnownModule::is_enum` (#19681)
  [ty] Support `__setitem__` and improve `__getitem__` related diagnostics (#19578)
  [ty] Sync vendored typeshed stubs (#19676)
  [`flake8-use-pathlib`] Expand `PTH201` to check all `PurePath` subclasses (#19440)
  [`refurb`] Make example error out-of-the-box (`FURB180`) (#19672)
  [`pyupgrade`] Prevent infinite loop with `I002` (`UP010`, `UP035`) (#19413)
  [ty] Improve the `Display` for generic `type[]` types (#19667)
  [ty] Refactor `TypeInferenceBuilder::infer_subscript_expression_types` (#19658)
  Fix tests on 32-bit architectures (#19652)
  [ty] Move `pandas-stubs` to bad.txt (#19659)
  [ty] Remove special casing for string-literal-in-tuple `__contains__` (#19642)
  Update pre-commit's `ruff` id (#19654)
  Update salsa (#19449)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants