Skip to content

Add type hints to minimal publisher rclpy example#439

Open
andrearichichi wants to merge 3 commits intoros2:rollingfrom
andrearichichi:add-type-hints-minimal-publisher
Open

Add type hints to minimal publisher rclpy example#439
andrearichichi wants to merge 3 commits intoros2:rollingfrom
andrearichichi:add-type-hints-minimal-publisher

Conversation

@andrearichichi
Copy link

@andrearichichi andrearichichi commented Mar 18, 2026

This PR adds Python type hints to the minimal publisher rclpy example.
It annotates function return types and key variables (publisher, timer, and message).
No logic or behavior has been changed.
The diff is intentionally minimal and limited to typing improvements only.

Signed-off-by: Andrea Richichi <andrea.richichi@studenti.unitn.it>
@InvincibleRMC InvincibleRMC self-requested a review March 19, 2026 17:03
def __init__(self) -> None:
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 10)
self.publisher_: Publisher[String] = self.create_publisher(String, 'topic', 10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.publisher_: Publisher[String] = self.create_publisher(String, 'topic', 10)
self.publisher_= self.create_publisher(String, 'topic', 10)

# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List, Optional

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use builtin list

self.publisher_: Publisher[String] = self.create_publisher(String, 'topic', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.timer: Timer = self.create_timer(timer_period, self.timer_callback)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.timer: Timer = self.create_timer(timer_period, self.timer_callback)
self.timer = self.create_timer(timer_period, self.timer_callback)

def timer_callback(self):
msg = String()
def timer_callback(self) -> None:
msg: String = String()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg: String = String()
msg = String()

Signed-off-by: Andrea Richichi <andrea.richichi@studenti.unitn.it>
Copy link

@InvincibleRMC InvincibleRMC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need type hint on function args like main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants