Skip to content

fix special-case NotImplementedType in binop signatures to match runtime semantics #1129 - #5476

Closed
asukaminato0721 wants to merge 3 commits into
meta:mainfrom
asukaminato0721:1129-focused
Closed

asukaminato0721 wants to merge 3 commits into
meta:mainfrom
asukaminato0721:1129-focused

Conversation

@asukaminato0721

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1129

continue of #2677

  • I am a human, and I fully understand the code I am submitting.

Test Plan

@github-actions

This comment has been minimized.

@meta-codesync

meta-codesync Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D128884121. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@stroxler stroxler 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.

Thanks! A fresh round of code review caught a few more concerns (not that surprising, this is a tricky change)

  • Valid SymPy operations gain errors. Bundled stubs incorrectly annotate operations such as DomainMatrix addition/subtraction and PolyElement multiplication as returning only NotImplementedType. The PR turns those declarations into unsupported-operation errors under the default preset. Confirmed absent at the base and valid at runtime. Fix the bundled declarations in a prerequisite change.

  • Same-type operands incorrectly reach __radd__. When __add__ declines, Same() + Same() can infer str from __radd__, although Python never calls it for identical runtime classes and raises TypeError. The corresponding += path has the same problem.

  • Inplace fallback uses the wrong order. After __iadd__ declines, a proper subclass’s overridden __radd__ must precede the left operand’s __add__. The current fixed ordering infers int where Python produces str. These dispatch gaps predate the PR but become exposed by its new decline handling.

@asukaminato0721

Copy link
Copy Markdown
Collaborator Author

cc @stroxler microsoft/python-type-stubs#407 after some debugging. So this pr kind of "blocked" by upstream now.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

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

============================================================
SUMMARY
============================================================
Total: +22 new errors, -10 fixed errors
By preset: +12/-1 (default), +12/-1 (strict)

Projects with changes (9):
  openlibrary: +4 -1
  manticore: +0 -1
  static-frame: +3 -4
  pandas-stubs: +8 -0
  pandas: +1 -1
  trio: +1 -1
  dd-trace-py: +1 -0
  jinja: +2 -2
  xarray: +2 -0
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

openlibrary (https://github.com/internetarchive/openlibrary)
- ERROR openlibrary/book_providers.py:596:92-155: Implicit conversion of `Acquisition | NotImplementedType | Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR openlibrary/book_providers.py:596:92-155: Implicit conversion of `Acquisition | Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR openlibrary/solr/updater/edition.py:353:16-63: Returning implicit Any from function declared to return "bool" [no-any-return-implicit]
+ ERROR openlibrary/solr/updater/edition.py:526:16-75: Returning implicit Any from function declared to return "bool" [no-any-return-implicit]
+ ERROR openlibrary/solr/updater/edition.py:530:16-78: Returning implicit Any from function declared to return "bool" [no-any-return-implicit]

manticore (https://github.com/trailofbits/manticore)
- ERROR manticore/native/manticore.py:82:24-43: `!=` is not supported between `Module[manticore.platforms]` and `Module[manticore.platforms]` [unsupported-operation]

static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/test/unit/test_index.py:527:26-37: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR static_frame/test/unit/test_index.py:527:26-37: `@` is not supported between `Index` and `Index` [unsupported-operation]
- ERROR static_frame/test/unit/test_index_hierarchy.py:3172:38-44: The type of attribute `tolist` is an explicit `Any` [explicit-any-use]
+ ERROR static_frame/test/unit/test_index_hierarchy.py:3172:27-36: `@` is not supported between `IndexHierarchy` and `IndexHierarchy` [unsupported-operation]
- ERROR static_frame/test/unit/test_index_hierarchy.py:3212:14-23: The type of `a1` is an explicit `Any` [explicit-any-use]
+ ERROR static_frame/test/unit/test_index_hierarchy.py:3212:14-23: `@` is not supported between `IndexHierarchy` and `IndexHierarchy` [unsupported-operation]
- ERROR static_frame/test/unit/test_index_hierarchy.py:3214:29-35: The type of attribute `tolist` is an explicit `Any` [explicit-any-use]

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ ERROR tests/indexes/float/test_floordiv.py:127:22-52: assert_type(Unknown, Index[float]) failed [assert-type]
+ ERROR tests/indexes/float/test_floordiv.py:127:23-32: `//` is not supported between `Index[bool]` and `Index[float]` [unsupported-operation]
+ ERROR tests/indexes/str/test_mul.py:134:22-49: assert_type(Unknown, Index[str]) failed [assert-type]
+ ERROR tests/indexes/str/test_mul.py:134:23-31: `*` is not supported between `Index[int]` and `Index[str]` [unsupported-operation]
+ ERROR tests/series/float/test_floordiv.py:228:22-53: assert_type(Unknown, Series[float]) failed [assert-type]
+ ERROR tests/series/float/test_floordiv.py:228:23-32: `//` is not supported between `Series[bool]` and `Series[float]` [unsupported-operation]
+ ERROR tests/series/str/test_mul.py:234:22-50: assert_type(Unknown, Series[str]) failed [assert-type]
+ ERROR tests/series/str/test_mul.py:234:23-31: `*` is not supported between `Series[int]` and `Series[str]` [unsupported-operation]

pandas (https://github.com/pandas-dev/pandas)
- ERROR pandas/core/arrays/_arrow_string_mixins.py:202:12-33: Implicit conversion of `int` to `bool` is not allowed [implicit-bool]
+ ERROR pandas/core/arrays/_arrow_string_mixins.py:202:12-33: Implicit conversion of `RegexFlag | int` to `bool` is not allowed [implicit-bool]

trio (https://github.com/python-trio/trio)
- ERROR src/trio/_core/_io_windows.py:685:16-21: Implicit conversion of `int` to `bool` is not allowed [implicit-bool]
+ ERROR src/trio/_core/_io_windows.py:685:16-21: Implicit conversion of `AFDPollFlags | int` to `bool` is not allowed [implicit-bool]

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ERROR ddtrace/vendor/jsonpath/parse.py:746:13-747:48: Fixpoint iteration did not converge. Inferred type `Literal[0] | RegexFlag`. Adding annotations may help. [non-convergent-recursion]

jinja (https://github.com/pallets/jinja)
- ERROR src/jinja2/filters.py:858:12-17: Implicit conversion of `list[str]` to `bool` is not allowed [implicit-bool]
+ ERROR src/jinja2/filters.py:858:12-17: Implicit conversion of `list[Markup] | list[str]` to `bool` is not allowed [implicit-bool]
- ERROR src/jinja2/filters.py:860:37-41: Implicit conversion of `str` to `bool` is not allowed [implicit-bool]
+ ERROR src/jinja2/filters.py:860:37-41: Implicit conversion of `Markup | str` to `bool` is not allowed [implicit-bool]

xarray (https://github.com/pydata/xarray)
+ ERROR xarray/core/resample_cftime.py:459:23-34: The type of `lresult` is unknown because it comes from an error [unknown-from-error]
+ ERROR xarray/tests/test_cftime_offsets.py:558:14-29: The type of `result` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]

@stroxler stroxler 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.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot added the Merged label Nov 1, 2026
@meta-codesync

meta-codesync Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in d42f807.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants