cbrunet / python-poppler

Python binding to Poppler-cpp pdf library
GNU General Public License v2.0
95 stars 15 forks source link

Clarification and Improvement of `rect` and `rectf` Handling #82

Open 0xbe7a opened 10 months ago

0xbe7a commented 10 months ago

While using PyLance for local development, I've encountered an inconsistency in the Python bindings related to the handling of Rectangle objects (rect and rectf). Specifically, the constructor for a Rectangle can accept either four floats or four integers, creating a rect or rectf respectively. However, this distinction becomes unclear when using certain functions like Page.text() that specifically require a rectf for the bounding box.

Currently, the bindings do not clearly differentiate between a rect and a rectf from the perspective of a developer working with Rectangles. This leads to confusion, especially since there's no visible difference in the Python code.

Additionally, there's an issue with PyLance when attempting to construct a Rectangle using float values. PyLance reports an error because the default arguments for the constructor are integers, causing type conflicts.

Proposed Solutions:

To address these issues, I suggest one of the following approaches:

  1. Document and Distinguish rect and rectf Types:

    • Update the documentation to clearly differentiate between rect and rectf.
    • Overload the Rectangle constructor to include both (x: float, y: float, w: float, h: float) and (x: int, y: int, w: int, h: int). This provides explicit constructors for each type and makes it clear to the developer which type they are working with.
  2. Transparent Handling of Type Differences:

    • Modify the Rectangle constructor to handle both int and float types seamlessly. This approach would abstract the complexity from the developer, allowing for more flexible and intuitive usage of the API.