pybind / pybind/pybind11

[BUG]: SupportsInt and SupportsFloat changes cause type checking failures on python classes that override these functions

Open
#5,767 28 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.