Skip to content

Split Map2d.iter_data overloads by direction literal - #677

Merged
nikobockerman merged 1 commit into
mainfrom
fix-python-overload-ordering-in-map
Aug 29, 2026
Merged

Split Map2d.iter_data overloads by direction literal#677
nikobockerman merged 1 commit into
mainfrom
fix-python-overload-ordering-in-map

Conversation

@nikobockerman

Copy link
Copy Markdown
Owner

The iter_data overloads came in pairs per arity: one keyed on Literal[IterDirection.Rows] returning row-major tuples, and a general one accepting any IterDirection and returning column-major tuples, the general signature declared first.

Overload resolution picks the first match, and Literal[IterDirection.Rows] is assignable to IterDirection, so the general signature swallowed every call that named IterDirection.Rows explicitly and typed its result as column-major - the transpose of what the implementation yields. Merely declaring the literal signature first moves that unsoundness rather than removing it: the general signature then still promises column-major for an argument that may be Rows at run time, which mypy reports as an overload-overlap error.

Give each arity three signatures instead: Literal[IterDirection.Rows], Literal[IterDirection.Columns], and a general IterDirection returning the union of both shapes. The two literal signatures are disjoint, and each returns a subtype of the union the general signature promises, so no unsafe overlap is left. The union is also the honest answer for a caller whose direction is only known as an IterDirection, since iter_data branches on that value at run time.

Behaviour is unchanged and no call site needed adjusting.

The iter_data overloads came in pairs per arity: one keyed on
Literal[IterDirection.Rows] returning row-major tuples, and a general
one accepting any IterDirection and returning column-major tuples, the
general signature declared first.

Overload resolution picks the first match, and Literal[IterDirection.Rows]
is assignable to IterDirection, so the general signature swallowed every
call that named IterDirection.Rows explicitly and typed its result as
column-major - the transpose of what the implementation yields. Merely
declaring the literal signature first moves that unsoundness rather than
removing it: the general signature then still promises column-major for
an argument that may be Rows at run time, which mypy reports as an
overload-overlap error.

Give each arity three signatures instead: Literal[IterDirection.Rows],
Literal[IterDirection.Columns], and a general IterDirection returning the
union of both shapes. The two literal signatures are disjoint, and each
returns a subtype of the union the general signature promises, so no
unsafe overlap is left. The union is also the honest answer for a caller
whose direction is only known as an IterDirection, since iter_data
branches on that value at run time.

Behaviour is unchanged and no call site needed adjusting.
Copilot AI lite review requested due to automatic review settings August 29, 2026 18:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The overload restructuring matches the runtime branching behavior, removes the documented typing unsoundness, and doesn’t alter runtime logic.

Pull request overview

This PR fixes type-checking unsoundness in Map2d.iter_data overloads by making the overload set disjoint for Rows vs Columns literals and making the non-literal overload return the honest union shape that matches the runtime branch on direction.

Changes:

  • Split iter_data overloads (for each arity) into Literal[IterDirection.Rows], Literal[IterDirection.Columns], and a general IterDirection overload returning a union of both iterator shapes.
  • Ensured calls explicitly passing IterDirection.Rows are typed as row-major (matching implementation), rather than being swallowed by a broader overload.
File summaries
File Description
solvers/python/src/aoc/tooling/map.py Reworks iter_data overload signatures to avoid overload overlap and align static return types with runtime behavior.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@nikobockerman
nikobockerman enabled auto-merge (squash) August 29, 2026 18:23
@nikobockerman
nikobockerman merged commit 3529d8e into main Aug 29, 2026
54 checks passed
@nikobockerman
nikobockerman deleted the fix-python-overload-ordering-in-map branch August 29, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants