Skip to content

Fix spurious error on optional dict of generic callable union - #22520

Closed
zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/optional-dict-callable-union-22513
Closed

zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/optional-dict-callable-union-22513

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #22513

In solve_one(), an invariant context can use the same union as both the lower and the upper bound. When the join of the lower bounds gains a redundant member through Any compatibility, the inferred value type is widened, which produces two competing dict candidates for an optional dict of callable unions and reports a spurious dict-item error.

Fix: when both bounds are unions that differ, top is itself one of the lower bounds, and every lower bound is already a subtype of top, keep the original union instead of using the join result.

Added testInferenceContextOptionalDictOfCallableUnion (the exact repro from the issue) to check-inference-context.test; it fails before this change and passes after. Related suites pass (1178 passed, 5 skipped, 1 xfailed across testcheck.py -k "InferenceContext or Generic or Union", testsolve.py, testtypes.py).

Fixes python#22513

In solve_one, an invariant context could set the same union as both a
lower and an upper bound. The join of the lower bounds could then add
redundant members when compatibility depended on Any, widening the
inferred value type and leaving the outer dict with two competing dict
alternatives. Keep the original union when it already accepts every
lower bound.
@github-actions

Copy link
Copy Markdown
Contributor

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

spark (https://github.com/apache/spark)
- python/pyspark/pandas/groupby.py:3544: error: Argument 2 to "zip" has incompatible type "list[Hashable | Any] | Hashable | list[Any | tuple[Any, ...]]"; expected "Iterable[Hashable | Any]"  [arg-type]
+ python/pyspark/pandas/groupby.py:3544: error: Argument 2 to "zip" has incompatible type "list[Any | tuple[Any, ...]] | Hashable"; expected "Iterable[Any | tuple[Any, ...]]"  [arg-type]

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.

[regression] Error on optional dict where value type is generic union of callables

2 participants