Skip to content

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

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

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

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Fixes #22513

When an invariant context uses the same union as both a lower and an upper bound, join of lower bounds can add redundant members through Any compatibility. In solve_one(), if the joined bottom is a subtype of top but is a different union, and top already accepts every lower bound (including being one of them), keep top instead of the widened join.

This fixes the regression reported in #22513, where a dict value typed as an optional union of generic callables produced a spurious dict-item error.

Verification

  • Added testInferenceContextOptionalDictOfCallableUnion in check-inference-context.test (from the issue repro).
  • pytest mypy/test/testcheck.py -n0 -k "testInferenceContextOptionalDictOfCallableUnion": 1 passed.
  • pytest mypy/test/testcheck.py -n0 -k "InferenceContext or Generic or Union": 1147 passed, 4 skipped, 7241 deselected, 1 xfailed.
  • pytest mypy/test/testsolve.py mypy/test/testtypes.py -n0: 144 passed, 2 skipped.
  • ruff check mypy/solve.py and black --check mypy/solve.py: clean.

AI assistance: this change was prepared with AI assistance; I reviewed the diagnosis, fix, and verification.

@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]

cattrs (https://github.com/python-attrs/cattrs)
- src/cattrs/converters.py:250: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Callable[[Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any], str]] | tuple[Callable[[Any], Any], Callable[[Any], Any]] | tuple[Callable[[Any], Any], Callable[[Any], dict[str, Any]]] | tuple[Callable[[Any], bool], Callable[[type[Enum], BaseConverter], Callable[[Enum], Any]], str] | <7 more items>]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:292: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], Any], Callable[[Mapping[str, Any], type[T]], T]] | tuple[Callable[[Any], bool], Callable[[type[Enum], BaseConverter], Callable[[Any, type[Enum]], Enum]], str] | tuple[Callable[[Any], Any | bool], Callable[[Any], Callable[[Any, type[T]], T]], bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Callable[[Any, Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any, bool], Callable[[Any, type[T]], type[T] | None]], bool] | <14 more items>]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:470: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Any, Any, str] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:611: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Any, Any, str] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/preconf/orjson.py:90: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], bool], partial[Callable[[Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any, Any], Any], bool] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant