Skip to content

Commit b7b298d

Browse files
authored
Merge pull request #273 from aatle/pymunk7
Make most properties use conventional syntax to fix static typing
2 parents a7afc5b + 875c3f7 commit b7b298d

File tree

7 files changed

+367
-482
lines changed

7 files changed

+367
-482
lines changed

pymunk/arbiter.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ def __init__(self, _arbiter: ffi.CData, space: "Space") -> None:
3838
self._arbiter = _arbiter
3939
self._space = space
4040

41-
def _get_contact_point_set(self) -> ContactPointSet:
41+
@property
42+
def contact_point_set(self) -> ContactPointSet:
43+
"""Contact point sets make getting contact information from the
44+
Arbiter simpler.
45+
46+
Return `ContactPointSet`"""
4247
_set = lib.cpArbiterGetContactPointSet(self._arbiter)
4348
return ContactPointSet._from_cp(_set)
4449

45-
def _set_contact_point_set(self, point_set: ContactPointSet) -> None:
50+
@contact_point_set.setter
51+
def contact_point_set(self, point_set: ContactPointSet) -> None:
4652
# This has to be done by fetching a new Chipmunk point set, update it
4753
# according to whats passed in and the pass that back to chipmunk due
4854
# to the fact that ContactPointSet doesnt contain a reference to the
@@ -63,15 +69,6 @@ def _set_contact_point_set(self, point_set: ContactPointSet) -> None:
6369

6470
lib.cpArbiterSetContactPointSet(self._arbiter, ffi.addressof(_set))
6571

66-
contact_point_set = property(
67-
_get_contact_point_set,
68-
_set_contact_point_set,
69-
doc="""Contact point sets make getting contact information from the
70-
Arbiter simpler.
71-
72-
Return `ContactPointSet`""",
73-
)
74-
7572
@property
7673
def shapes(self) -> Tuple["Shape", "Shape"]:
7774
"""Get the shapes in the order that they were defined in the
@@ -87,46 +84,40 @@ def shapes(self) -> Tuple["Shape", "Shape"]:
8784
assert b is not None
8885
return a, b
8986

90-
def _get_restitution(self) -> float:
91-
return lib.cpArbiterGetRestitution(self._arbiter)
92-
93-
def _set_restitution(self, restitution: float) -> None:
94-
lib.cpArbiterSetRestitution(self._arbiter, restitution)
95-
96-
restitution = property(
97-
_get_restitution,
98-
_set_restitution,
99-
doc="""The calculated restitution (elasticity) for this collision
87+
@property
88+
def restitution(self) -> float:
89+
"""The calculated restitution (elasticity) for this collision
10090
pair.
101-
91+
10292
Setting the value in a pre_solve() callback will override the value
10393
calculated by the space. The default calculation multiplies the
10494
elasticity of the two shapes together.
105-
""",
106-
)
95+
"""
96+
return lib.cpArbiterGetRestitution(self._arbiter)
10797

108-
def _get_friction(self) -> float:
109-
return lib.cpArbiterGetFriction(self._arbiter)
98+
@restitution.setter
99+
def restitution(self, restitution: float) -> None:
100+
lib.cpArbiterSetRestitution(self._arbiter, restitution)
110101

111-
def _set_friction(self, friction: float) -> None:
112-
lib.cpArbiterSetFriction(self._arbiter, friction)
102+
@property
103+
def friction(self) -> float:
104+
"""The calculated friction for this collision pair.
113105
114-
friction = property(
115-
_get_friction,
116-
_set_friction,
117-
doc="""The calculated friction for this collision pair.
118-
119106
Setting the value in a pre_solve() callback will override the value
120107
calculated by the space. The default calculation multiplies the
121108
friction of the two shapes together.
122-
""",
123-
)
109+
"""
110+
return lib.cpArbiterGetFriction(self._arbiter)
111+
112+
@friction.setter
113+
def friction(self, friction: float) -> None:
114+
lib.cpArbiterSetFriction(self._arbiter, friction)
124115

125116
def _get_surface_velocity(self) -> Vec2d:
126117
v = lib.cpArbiterGetSurfaceVelocity(self._arbiter)
127118
return Vec2d(v.x, v.y)
128119

129-
def _set_surface_velocity(self, velocity: Vec2d) -> None:
120+
def _set_surface_velocity(self, velocity: Tuple[float, float]) -> None:
130121
lib.cpArbiterSetSurfaceVelocity(self._arbiter, velocity)
131122

132123
surface_velocity = property(

pymunk/body.py

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -252,30 +252,28 @@ def __repr__(self) -> str:
252252
else:
253253
return "Body(Body.STATIC)"
254254

255-
def _set_mass(self, mass: float) -> None:
256-
lib.cpBodySetMass(self._body, mass)
257-
258-
def _get_mass(self) -> float:
255+
@property
256+
def mass(self) -> float:
257+
"""Mass of the body."""
259258
return lib.cpBodyGetMass(self._body)
260259

261-
mass = property(_get_mass, _set_mass, doc="""Mass of the body.""")
260+
@mass.setter
261+
def mass(self, mass: float) -> None:
262+
lib.cpBodySetMass(self._body, mass)
262263

263-
def _set_moment(self, moment: float) -> None:
264-
lib.cpBodySetMoment(self._body, moment)
264+
@property
265+
def moment(self) -> float:
266+
"""Moment of inertia (MoI or sometimes just moment) of the body.
265267
266-
def _get_moment(self) -> float:
268+
The moment is like the rotational mass of a body.
269+
"""
267270
return lib.cpBodyGetMoment(self._body)
268271

269-
moment = property(
270-
_get_moment,
271-
_set_moment,
272-
doc="""Moment of inertia (MoI or sometimes just moment) of the body.
273-
274-
The moment is like the rotational mass of a body.
275-
""",
276-
)
272+
@moment.setter
273+
def moment(self, moment: float) -> None:
274+
lib.cpBodySetMoment(self._body, moment)
277275

278-
def _set_position(self, pos: Union[Vec2d, Tuple[float, float]]) -> None:
276+
def _set_position(self, pos: Tuple[float, float]) -> None:
279277
assert len(pos) == 2
280278
lib.cpBodySetPosition(self._body, pos)
281279

@@ -344,16 +342,9 @@ def _get_force(self) -> Vec2d:
344342
force applied manually from the apply force functions.""",
345343
)
346344

347-
def _set_angle(self, angle: float) -> None:
348-
lib.cpBodySetAngle(self._body, angle)
349-
350-
def _get_angle(self) -> float:
351-
return lib.cpBodyGetAngle(self._body)
352-
353-
angle = property(
354-
_get_angle,
355-
_set_angle,
356-
doc="""Rotation of the body in radians.
345+
@property
346+
def angle(self) -> float:
347+
"""Rotation of the body in radians.
357348
358349
When changing the rotation you may also want to call
359350
:py:func:`Space.reindex_shapes_for_body` to update the collision
@@ -365,43 +356,39 @@ def _get_angle(self) -> float:
365356
If you get small/no changes to the angle when for example a
366357
ball is "rolling" down a slope it might be because the Circle shape
367358
attached to the body or the slope shape does not have any friction
368-
set.""",
369-
)
359+
set."""
360+
return lib.cpBodyGetAngle(self._body)
370361

371-
def _set_angular_velocity(self, w: float) -> None:
372-
lib.cpBodySetAngularVelocity(self._body, w)
362+
@angle.setter
363+
def angle(self, angle: float) -> None:
364+
lib.cpBodySetAngle(self._body, angle)
373365

374-
def _get_angular_velocity(self) -> float:
366+
@property
367+
def angular_velocity(self) -> float:
368+
"""The angular velocity of the body in radians per second."""
375369
return lib.cpBodyGetAngularVelocity(self._body)
376370

377-
angular_velocity = property(
378-
_get_angular_velocity,
379-
_set_angular_velocity,
380-
doc="""The angular velocity of the body in radians per second.""",
381-
)
371+
@angular_velocity.setter
372+
def angular_velocity(self, w: float) -> None:
373+
lib.cpBodySetAngularVelocity(self._body, w)
382374

383-
def _set_torque(self, t: float) -> None:
384-
lib.cpBodySetTorque(self._body, t)
375+
@property
376+
def torque(self) -> float:
377+
"""The torque applied to the body.
385378
386-
def _get_torque(self) -> float:
379+
This value is reset for every time step."""
387380
return lib.cpBodyGetTorque(self._body)
388381

389-
torque = property(
390-
_get_torque,
391-
_set_torque,
392-
doc="""The torque applied to the body.
393-
394-
This value is reset for every time step.""",
395-
)
382+
@torque.setter
383+
def torque(self, t: float) -> None:
384+
lib.cpBodySetTorque(self._body, t)
396385

397-
def _get_rotation_vector(self) -> Vec2d:
386+
@property
387+
def rotation_vector(self) -> Vec2d:
388+
"""The rotation vector for the body."""
398389
v = lib.cpBodyGetRotation(self._body)
399390
return Vec2d(v.x, v.y)
400391

401-
rotation_vector = property(
402-
_get_rotation_vector, doc="""The rotation vector for the body."""
403-
)
404-
405392
@property
406393
def space(self) -> Optional["Space"]:
407394
"""Get the :py:class:`Space` that the body has been added to (or
@@ -609,7 +596,20 @@ def is_sleeping(self) -> bool:
609596
"""Returns true if the body is sleeping."""
610597
return bool(lib.cpBodyIsSleeping(self._body))
611598

612-
def _set_type(self, body_type: _BodyType) -> None:
599+
@property
600+
def body_type(self) -> _BodyType:
601+
"""The type of a body (:py:const:`Body.DYNAMIC`,
602+
:py:const:`Body.KINEMATIC` or :py:const:`Body.STATIC`).
603+
604+
When changing an body to a dynamic body, the mass and moment of
605+
inertia are recalculated from the shapes added to the body. Custom
606+
calculated moments of inertia are not preserved when changing types.
607+
This function cannot be called directly in a collision callback.
608+
"""
609+
return lib.cpBodyGetType(self._body)
610+
611+
@body_type.setter
612+
def body_type(self, body_type: _BodyType) -> None:
613613
if body_type != Body.DYNAMIC:
614614
for c in self.constraints:
615615
assert (c.a != self and c.b.body_type == Body.DYNAMIC) or (
@@ -618,22 +618,6 @@ def _set_type(self, body_type: _BodyType) -> None:
618618

619619
lib.cpBodySetType(self._body, body_type)
620620

621-
def _get_type(self) -> _BodyType:
622-
return lib.cpBodyGetType(self._body)
623-
624-
body_type = property(
625-
_get_type,
626-
_set_type,
627-
doc="""The type of a body (:py:const:`Body.DYNAMIC`,
628-
:py:const:`Body.KINEMATIC` or :py:const:`Body.STATIC`).
629-
630-
When changing an body to a dynamic body, the mass and moment of
631-
inertia are recalculated from the shapes added to the body. Custom
632-
calculated moments of inertia are not preserved when changing types.
633-
This function cannot be called directly in a collision callback.
634-
""",
635-
)
636-
637621
def each_arbiter(
638622
self,
639623
func: Callable[..., None], # TODO: Fix me once PEP 612 is ready

pymunk/collision_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def data(self) -> Dict[Any, Any]:
7272
"""
7373
return self._data
7474

75-
def _set_begin(self, func: Callable[[Arbiter, "Space", Any], bool]) -> None:
75+
def _set_begin(self, func: _CollisionCallbackBool) -> None:
7676
self._begin = func
7777
self._handler.beginFunc = lib.ext_cpCollisionBeginFunc
7878

@@ -98,7 +98,7 @@ def _set_pre_solve(self, func: _CollisionCallbackBool) -> None:
9898
self._pre_solve = func
9999
self._handler.preSolveFunc = lib.ext_cpCollisionPreSolveFunc
100100

101-
def _get_pre_solve(self) -> Optional[Callable[[Arbiter, "Space", Any], bool]]:
101+
def _get_pre_solve(self) -> Optional[_CollisionCallbackBool]:
102102
return self._pre_solve
103103

104104
pre_solve = property(

0 commit comments

Comments
 (0)