Skip to content
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

Rect() and FRect() initialization with no arguments #2655

Merged
merged 7 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/rect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class _GenericRect(Collection[_N]):
def __init__(self, left_top: Coordinate, width_height: Coordinate) -> None: ...
@overload
def __init__(self, single_arg: RectValue) -> None: ...
@overload
def __init__(self) -> None: ...
def __len__(self) -> Literal[4]: ...
def __iter__(self) -> Iterator[_N]: ...
@overload
Expand Down
6 changes: 6 additions & 0 deletions docs/reST/ref/rect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
| :sg:`Rect(left, top, width, height) -> Rect`
| :sg:`Rect((left, top), (width, height)) -> Rect`
| :sg:`Rect(object) -> Rect`
| :sg:`Rect() -> Rect`
| :sg:`FRect(left, top, width, height) -> FRect`
| :sg:`FRect((left, top), (width, height)) -> FRect`
| :sg:`FRect(object) -> FRect`
| :sg:`FRect() -> FRect`

.. versionchanged:: 2.2 Since version 2.2 there is another class called FRect that serves the same purpose as as `Rect` but it can hold floats instead of integers.

Expand All @@ -26,6 +28,10 @@
values to construct a Rect. This makes it easier to create Rects on the fly
as arguments to functions.

If no arguments are given, a zero Rect will be created (x=0, y=0, w=0, h=0).
This will only work when using the Rect/FRect class and not with functions
that require a Rect argument.

The Rect functions that change the position or size of a Rect return a new
copy of the Rect with the affected changes. The original Rect is not
modified. Some methods have an alternate "in-place" version that returns
Expand Down
2 changes: 1 addition & 1 deletion src_c/doc/rect_doc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Auto generated file: with makeref.py . Docs go in docs/reST/ref/ . */
#define DOC_RECT "Rect(left, top, width, height) -> Rect\nRect((left, top), (width, height)) -> Rect\nRect(object) -> Rect\nFRect(left, top, width, height) -> FRect\nFRect((left, top), (width, height)) -> FRect\nFRect(object) -> FRect\npygame object for storing rectangular coordinates"
#define DOC_RECT "Rect(left, top, width, height) -> Rect\nRect((left, top), (width, height)) -> Rect\nRect(object) -> Rect\nRect() -> Rect\nFRect(left, top, width, height) -> FRect\nFRect((left, top), (width, height)) -> FRect\nFRect(object) -> FRect\nFRect() -> FRect\npygame object for storing rectangular coordinates"
#define DOC_RECT_COPY "copy() -> Rect\ncopy the rectangle"
#define DOC_RECT_MOVE "move(x, y, /) -> Rect\nmoves the rectangle"
#define DOC_RECT_MOVEIP "move_ip(x, y, /) -> None\nmoves the rectangle, in place"
Expand Down
4 changes: 4 additions & 0 deletions src_c/rect_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,10 @@ RectExport_dealloc(RectObject *self)
static int
RectExport_init(RectObject *self, PyObject *args, PyObject *kwds)
{
if (PyTuple_GET_SIZE(args) == 0) {
return 0;
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
}

InnerRect *argrect, temp;

if (!(argrect = RectFromObject(args, &temp))) {
Expand Down
21 changes: 21 additions & 0 deletions test/rect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,13 @@ def test_collection_abc(self):
self.assertTrue(isinstance(r, Collection))
self.assertFalse(isinstance(r, Sequence))

def test_construction_no_args(self):
r = Rect()
self.assertEqual(r.x, 0)
self.assertEqual(r.y, 0)
self.assertEqual(r.w, 0)
self.assertEqual(r.h, 0)

ankith26 marked this conversation as resolved.
Show resolved Hide resolved

class FRectTypeTest(RectTypeTest):
def setUp(self):
Expand Down Expand Up @@ -3291,6 +3298,13 @@ def test_frect_subscript(self):
self.assertSeqAlmostEqual5(r[3::-1], [4.8, 3.6, 2.4, 1.2])
self.assertRaises(TypeError, r.__getitem__, None)

def test_construction_no_args(self):
r = FRect()
self.assertEqual(r.x, 0.0)
self.assertEqual(r.y, 0.0)
self.assertEqual(r.w, 0.0)
self.assertEqual(r.h, 0.0)


class SubclassTest(unittest.TestCase):
class MyRect(Rect):
Expand Down Expand Up @@ -3366,6 +3380,13 @@ def test_collection_abc(self):
self.assertTrue(isinstance(mr1, Collection))
self.assertFalse(isinstance(mr1, Sequence))

def test_construction_no_args(self):
mr = self.MyRect()
self.assertEqual(mr.x, 0)
self.assertEqual(mr.y, 0)
self.assertEqual(mr.w, 0)
self.assertEqual(mr.h, 0)


if __name__ == "__main__":
unittest.main()
Loading