Skip to content

Commit c988cd9

Browse files
committed
Fix identity comparisons with literals and mutable default arguments
Replace `is`/`is not` comparisons with non-None literals (`==`/`!=`/`not`) to fix SyntaxWarning in Python 3.12+ (related to #1291). Also replace mutable default arguments (lists) with None or tuples to prevent shared state bugs. Identity comparisons fixed: - `is not self.n_links` (integer) in 3 arm navigation files - `state is WAIT_FOR_NEW_GOAL` (integer) in n_joint_arm_to_point_control.py - `path_found is False` (bool literal) in probabilistic_road_map.py - `flag is inside` (bool) in grid_map_lib.py Mutable default arguments fixed: - `obstacles=[]` -> `obstacles=None` in arm_obstacle_navigation.py and _2.py - `start_vel=[0,0,0]` etc. -> tuples in TrajectoryGenerator.py - `obstacle_avoid_points=[]` -> `None` in GridWithDynamicObstacles.py - Removed mutable class-level list defaults in Grid class
1 parent f66868a commit c988cd9

File tree

8 files changed

+22
-15
lines changed

8 files changed

+22
-15
lines changed

AerialNavigation/drone_3d_trajectory_following/TrajectoryGenerator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
class TrajectoryGenerator():
10-
def __init__(self, start_pos, des_pos, T, start_vel=[0,0,0], des_vel=[0,0,0], start_acc=[0,0,0], des_acc=[0,0,0]):
10+
def __init__(self, start_pos, des_pos, T, start_vel=(0,0,0), des_vel=(0,0,0), start_acc=(0,0,0), des_acc=(0,0,0)):
1111
self.start_x = start_pos[0]
1212
self.start_y = start_pos[1]
1313
self.start_z = start_pos[2]

ArmNavigation/arm_obstacle_navigation/arm_obstacle_navigation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,18 @@ def update_points(self):
244244

245245
self.end_effector = np.array(self.points[self.n_links]).T
246246

247-
def plot(self, obstacles=[]): # pragma: no cover
247+
def plot(self, obstacles=None): # pragma: no cover
248248
plt.cla()
249+
if obstacles is None:
250+
obstacles = []
249251

250252
for obstacle in obstacles:
251253
circle = plt.Circle(
252254
(obstacle[0], obstacle[1]), radius=0.5 * obstacle[2], fc='k')
253255
plt.gca().add_patch(circle)
254256

255257
for i in range(self.n_links + 1):
256-
if i is not self.n_links:
258+
if i != self.n_links:
257259
plt.plot([self.points[i][0], self.points[i + 1][0]],
258260
[self.points[i][1], self.points[i + 1][1]], 'r-')
259261
plt.plot(self.points[i][0], self.points[i][1], 'k.')

ArmNavigation/arm_obstacle_navigation/arm_obstacle_navigation_2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,18 @@ def update_points(self):
275275

276276
self.end_effector = np.array(self.points[self.n_links]).T
277277

278-
def plot_arm(self, myplt, obstacles=[]): # pragma: no cover
278+
def plot_arm(self, myplt, obstacles=None): # pragma: no cover
279279
myplt.cla()
280+
if obstacles is None:
281+
obstacles = []
280282

281283
for obstacle in obstacles:
282284
circle = myplt.Circle(
283285
(obstacle[0], obstacle[1]), radius=0.5 * obstacle[2], fc='k')
284286
myplt.gca().add_patch(circle)
285287

286288
for i in range(self.n_links + 1):
287-
if i is not self.n_links:
289+
if i != self.n_links:
288290
myplt.plot([self.points[i][0], self.points[i + 1][0]],
289291
[self.points[i][1], self.points[i + 1][1]], 'r-')
290292
myplt.plot(self.points[i][0], self.points[i][1], 'k.')

ArmNavigation/n_joint_arm_to_point_control/NLinkArm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def plot(self): # pragma: no cover
5656
lambda event: [exit(0) if event.key == 'escape' else None])
5757

5858
for i in range(self.n_links + 1):
59-
if i is not self.n_links:
59+
if i != self.n_links:
6060
plt.plot([self.points[i][0], self.points[i + 1][0]],
6161
[self.points[i][1], self.points[i + 1][1]], 'r-')
6262
plt.plot(self.points[i][0], self.points[i][1], 'ko')

ArmNavigation/n_joint_arm_to_point_control/n_joint_arm_to_point_control.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def main(): # pragma: no cover
4444
errors, distance = distance_to_goal(end_effector, goal_pos)
4545

4646
# State machine to allow changing of goal before current goal has been reached
47-
if state is WAIT_FOR_NEW_GOAL:
47+
if state == WAIT_FOR_NEW_GOAL:
4848
if distance > 0.1 and not solution_found:
4949
joint_goal_angles, solution_found = inverse_kinematics(
5050
link_lengths, joint_angles, goal_pos)
@@ -54,7 +54,7 @@ def main(): # pragma: no cover
5454
arm.goal = end_effector
5555
elif solution_found:
5656
state = MOVING_TO_GOAL
57-
elif state is MOVING_TO_GOAL:
57+
elif state == MOVING_TO_GOAL:
5858
if distance > 0.1 and all(old_goal == goal_pos):
5959
joint_angles = joint_angles + Kp * \
6060
ang_diff(joint_goal_angles, joint_angles) * dt
@@ -103,7 +103,7 @@ def animation():
103103
errors, distance = distance_to_goal(end_effector, goal_pos)
104104

105105
# State machine to allow changing of goal before current goal has been reached
106-
if state is WAIT_FOR_NEW_GOAL:
106+
if state == WAIT_FOR_NEW_GOAL:
107107

108108
if distance > 0.1 and not solution_found:
109109
joint_goal_angles, solution_found = inverse_kinematics(
@@ -114,7 +114,7 @@ def animation():
114114
arm.goal = get_random_goal()
115115
elif solution_found:
116116
state = MOVING_TO_GOAL
117-
elif state is MOVING_TO_GOAL:
117+
elif state == MOVING_TO_GOAL:
118118
if distance > 0.1 and all(old_goal == goal_pos):
119119
joint_angles = joint_angles + Kp * \
120120
ang_diff(joint_goal_angles, joint_angles) * dt

Mapping/grid_map_lib/grid_map_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def set_value_from_polygon(self, pol_x, pol_y, val, inside=True):
152152

153153
flag = self.check_inside_polygon(x_pos, y_pos, pol_x, pol_y)
154154

155-
if flag is inside:
155+
if flag == inside:
156156
self.set_value_from_xy_index(x_ind, y_ind, val)
157157

158158
def calc_grid_index_from_xy_index(self, x_ind, y_ind):

PathPlanning/ProbabilisticRoadMap/probabilistic_road_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def dijkstra_planning(sx, sy, gx, gy, road_map, sample_x, sample_y):
205205
else:
206206
open_set[n_id] = node
207207

208-
if path_found is False:
208+
if not path_found:
209209
return [], []
210210

211211
# generate final course

PathPlanning/TimeBasedPathPlanning/GridWithDynamicObstacles.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Grid:
3535
# Set in constructor
3636
grid_size: np.ndarray
3737
reservation_matrix: np.ndarray
38-
obstacle_paths: list[list[Position]] = []
38+
obstacle_paths: list[list[Position]]
3939
# Obstacles will never occupy these points. Useful to avoid impossible scenarios
40-
obstacle_avoid_points: list[Position] = []
40+
obstacle_avoid_points: list[Position]
4141

4242
# Number of time steps in the simulation
4343
time_limit: int
@@ -49,15 +49,18 @@ def __init__(
4949
self,
5050
grid_size: np.ndarray,
5151
num_obstacles: int = 40,
52-
obstacle_avoid_points: list[Position] = [],
52+
obstacle_avoid_points: list[Position] | None = None,
5353
obstacle_arrangement: ObstacleArrangement = ObstacleArrangement.RANDOM,
5454
time_limit: int = 100,
5555
):
56+
if obstacle_avoid_points is None:
57+
obstacle_avoid_points = []
5658
self.obstacle_avoid_points = obstacle_avoid_points
5759
self.time_limit = time_limit
5860
self.grid_size = grid_size
5961
self.reservation_matrix = np.zeros((grid_size[0], grid_size[1], self.time_limit))
6062

63+
self.obstacle_paths = []
6164
if num_obstacles > self.grid_size[0] * self.grid_size[1]:
6265
raise Exception("Number of obstacles is greater than grid size!")
6366

0 commit comments

Comments
 (0)