Skip to content

Comments

Fix identity comparisons with literals and mutable default arguments#1350

Open
dhruvildarji wants to merge 1 commit intoAtsushiSakai:masterfrom
dhruvildarji:fix/identity-comparisons-mutable-defaults
Open

Fix identity comparisons with literals and mutable default arguments#1350
dhruvildarji wants to merge 1 commit intoAtsushiSakai:masterfrom
dhruvildarji:fix/identity-comparisons-mutable-defaults

Conversation

@dhruvildarji
Copy link

Summary

  • Replace is/is not comparisons with non-None literals to use ==/!=/not instead, fixing SyntaxWarning in Python 3.12+ (these will become SyntaxError in a future Python version)
  • Replace mutable default arguments (lists) with None or tuples to prevent shared-state bugs

Changes

Identity comparisons fixed (9 instances across 6 files)

Using is/is not to compare with integers, booleans, or other non-singleton literals is deprecated in Python 3.12+ and emits SyntaxWarning. This is relevant to #1291 (Python 3.14 support).

File Change
NLinkArm.py, arm_obstacle_navigation.py, arm_obstacle_navigation_2.py i is not self.n_links -> i != self.n_links (integer comparison)
n_joint_arm_to_point_control.py state is WAIT_FOR_NEW_GOAL / state is MOVING_TO_GOAL -> == (integer comparison)
probabilistic_road_map.py path_found is False -> not path_found
grid_map_lib.py flag is inside -> flag == inside (boolean comparison)

Mutable default arguments fixed (4 files)

Mutable default arguments are a well-known Python pitfall -- the default object is shared across all calls, so mutations persist unexpectedly.

File Change
arm_obstacle_navigation.py, arm_obstacle_navigation_2.py obstacles=[] -> obstacles=None with guard
TrajectoryGenerator.py start_vel=[0,0,0] etc. -> tuples (0,0,0) (immutable, only indexed)
GridWithDynamicObstacles.py obstacle_avoid_points=[] -> None with guard; removed mutable class-level list defaults

Test plan

  • All existing tests continue to pass (no behavioral changes, only correctness fixes)
  • No new warnings with -Werror flag used in CI test runner

Replace `is`/`is not` comparisons with non-None literals (`==`/`!=`/`not`)
to fix SyntaxWarning in Python 3.12+ (related to AtsushiSakai#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant