[BUG]: SupportsInt and SupportsFloat changes cause type checking failures on python classes that override these functions
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
3.0.0
Problem description
#5540 changed type hints from int and float to SupportsInt and SupportsFloat. While these may be nice for use cases where a C++ module is exposed to python, this is causing a lot of type problems for cases where a C++ function is trampolined and overridden in python. The base class is now using SupportsInt and SupportsFloat and this is more generic than the python derived classes that uses int, float so mypy issues errors like:
Argument 1 of "pdf" is incompatible with supertype "Distribution"; supertype defines the argument
type as "SupportsFloat" [override]
def pdf(self, x: float) -> float:
Am I missing something here, or is there a way to turn this feature off?
Reproducible example code
Is this a regression? Put the last known working version here if it is.
smartholder branch on 2024-08-09
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the type-hint change from #5540 and the reported mypy override error involving Distribution.pdf and SupportsFloat. Reproduce the failure with a C++ trampoline overridden by a Python class, then verify that the type hints no longer reject the narrower int or float override.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- developer-experience
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100