The issue here is that mypy isn't able to type check this stub because of the redundant overload, i.e., trying to use the stub with mypy errors with:
out/my_native_module.pyi:47: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
Found 1 error in 1 file (checked 1 source file)
This issue seems to be related to https://github.com/pybind/pybind11/issues/4585 because there is also something funny happening related to name (which should be a property, but somehow adding a custom __str__ switches it to a method instead -- it is actually an instancemethod not a staticmethod though). Inspecting the underlying annotation produced by pybind11 at runtime:
So in a sense the root cause is the broken annotation produced by pybind11 here, but perhaps it is an easy fix on the stub generator side to omit such redundant overloads as a robustification? (The mypy stubgen just seems to ignore them, and therefore still produces type-checkable output in this case.)
The following is a minimal example involving an enum with a custom
__str__
implementation:Running the stub generator on this module produces:
The issue here is that
mypy
isn't able to type check this stub because of the redundant overload, i.e., trying to use the stub with mypy errors with:This issue seems to be related to https://github.com/pybind/pybind11/issues/4585 because there is also something funny happening related to
name
(which should be a property, but somehow adding a custom__str__
switches it to a method instead -- it is actually aninstancemethod
not astaticmethod
though). Inspecting the underlying annotation produced by pybind11 at runtime:So in a sense the root cause is the broken annotation produced by pybind11 here, but perhaps it is an easy fix on the stub generator side to omit such redundant overloads as a robustification? (The mypy stubgen just seems to ignore them, and therefore still produces type-checkable output in this case.)