Skip to content

Fix FRect contains error message #3536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Hato1
Copy link

@Hato1 Hato1 commented Jul 15, 2025

FRect requires a float as the left operand but the error message says it requires an int.

Before this PR:

>>> fr = pygame.FRect(10,20,30,40)
>>> 10 in fr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <pygame.rect.FRect>' requires rect style object or int as left operand

After:

>>> fr = pygame.FRect(10,20,30,40)
>>> 10 in fr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <pygame.rect.FRect>' requires rect style object or float as left operand

Resolves #3533

@Hato1 Hato1 requested a review from a team as a code owner July 15, 2025 19:50
@Starbuck5
Copy link
Member

The rect_impl template is compiled separately for Rect and FRect, it shouldn't need a runtime check inside itself to determine which type is which. I was thinking a new define could be added to rect.c to communicate the type name as a string to the implementation. Or perhaps even there's a way to treat the C type that's already communicated as a string.

@Starbuck5 Starbuck5 added the rect pygame.rect label Jul 16, 2025
@MightyJosip
Copy link
Member

Starbuck what do you think about something like this

diff --git a/src_c/rect.c b/src_c/rect.c
index 155f97faf..c9097096a 100644
--- a/src_c/rect.c
+++ b/src_c/rect.c
@@ -49,6 +49,7 @@ static int
 four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
                      float *val4);

+#define IS_RECT
 #define RectExport_init pg_rect_init
 #define RectExport_subtypeNew4 _pg_rect_subtype_new4
 #define RectExport_new pg_rect_new
@@ -165,7 +166,9 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
 #define RectOptional_Freelist_Num pg_rect_freelist_num
 #endif /* PYPY_VERSION */
 #include "rect_impl.h"
+#undef IS_RECT

+#define IS_FRECT
 #define RectExport_init pg_frect_init
 #define RectExport_subtypeNew4 _pg_frect_subtype_new4
 #define RectExport_new pg_frect_new
@@ -282,6 +285,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
 #define RectOptional_Freelist_Num pg_frect_freelist_num
 #endif /* PYPY_VERSION */
 #include "rect_impl.h"
+#undef IS_FRECT

diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h
index 9018f9522..51b474637 100644
--- a/src_c/rect_impl.h
+++ b/src_c/rect_impl.h
@@ -1997,13 +1997,16 @@ RectExport_containsSeq(RectObject *self, PyObject *arg)

     int ret = RectExport_contains_internal(self, (PyObject *const *)&arg, 1);
     if (ret < 0) {
-        const char *operand_type =
-            pgFRect_Check((PyObject *)self) ? "float" : "int";
-
+#if defined(IS_RECT)
+#define OperandType "int"
+#elif defined(IS_FRECT)
+#define OperandType "float"
+#endif
         PyErr_Format(
             PyExc_TypeError,
             "'in %s' requires rect style object or %s as left operand",
-            ObjectName, operand_type);
+            ObjectName, OperandType);
+#undef OperandType
     }
     return ret;
 }

@Starbuck5
Copy link
Member

I think it would be simpler if you defined operandtype directly on the outer layer without the ISRECT/ISFRECT indirection.

Also I'd think should be able to be compiled into the string that goes into PyErr_Format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rect pygame.rect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect contains error message in FRect
3 participants