Skip to content

Commit

Permalink
Fix MyPy errors in Python 3.12 (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass authored Dec 30, 2024
1 parent bb334e9 commit 1221634
Show file tree
Hide file tree
Showing 24 changed files with 55 additions and 49 deletions.
1 change: 1 addition & 0 deletions docs/examples/blackboard_activity_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
writer.foo = "foobar"
unused_result = reader.foo
print(py_trees.display.unicode_blackboard_activity_stream())
assert py_trees.blackboard.Blackboard.activity_stream is not None
py_trees.blackboard.Blackboard.activity_stream.clear()
6 changes: 3 additions & 3 deletions docs/examples/blackboard_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Foo(py_trees.behaviour.Behaviour):
"""Example behaviour that reads from and writes to the blackboard."""

def __init__(self, name):
def __init__(self, name: str) -> None:
"""Construct a new behaviour instance and sets up blackboard access."""
super().__init__(name=name)
self.blackboard = self.attach_blackboard_client(name="Foo Global")
Expand All @@ -25,11 +25,11 @@ def __init__(self, name):
"number_of_noodles", access=py_trees.common.Access.WRITE
)

def initialise(self):
def initialise(self) -> None:
"""Initialise blackboard variables based on parameters."""
self.state.number_of_noodles = self.parameters.init

def update(self):
def update(self) -> py_trees.common.Status:
"""Update blackboard variables as the behaviour ticks."""
self.state.number_of_noodles += 1
self.feedback_message = self.state.number_of_noodles
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/blackboard_disconnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import py_trees


def check_foo():
def check_foo() -> None:
"""Read the value of a blackboard variable in a different scope."""
blackboard = py_trees.blackboard.Client(name="Reader")
blackboard.register_key(key="foo", access=py_trees.common.Access.READ)
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/blackboard_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
class Nested(object):
"""Simple object that contains a few attributes."""

def __init__(self):
self.foo = None
self.bar = None
def __init__(self) -> None:
self.foo: str | None = None
self.bar: str | None = None

def __str__(self):
def __str__(self) -> str:
return str(self.__dict__)


Expand Down
2 changes: 1 addition & 1 deletion docs/examples/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import py_trees.display

if __name__ == "__main__":
root = py_trees.composites.Sequence(name="Life")
root = py_trees.composites.Sequence(name="Life", memory=False)
timeout = py_trees.decorators.Timeout(
name="Timeout", child=py_trees.behaviours.Success(name="Have a Beer!")
)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import py_trees

if __name__ == "__main__":
sequence = py_trees.composites.Sequence("Sequence")
sequence = py_trees.composites.Sequence("Sequence", memory=False)
guard = py_trees.behaviours.Success(name="Guard")
a1 = py_trees.behaviours.Success(name="Action 1")
a2 = py_trees.behaviours.Success(name="Action 2")
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
b2 = py_trees.behaviours.Success(name="B2")
b3 = py_trees.behaviours.Success(name="B3")
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnSelected(
synchronise=True, children=[b1, b2]
)
),
)
root.add_children([b1, b2, b3])
py_trees.display.render_dot_tree(
Expand Down
7 changes: 5 additions & 2 deletions docs/examples/pickup_where_you_left_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
eventually=py_trees.common.Status.SUCCESS,
)
high_priority_interrupt = py_trees.decorators.RunningIsFailure(
child=py_trees.behaviours.Periodic(name="High Priority", n=3)
name="High Priority Interrupt",
child=py_trees.behaviours.Periodic(name="High Priority", n=3),
)
piwylo = py_trees.idioms.pick_up_where_you_left_off(
name="Tasks", tasks=[task_one, task_two]
)
root = py_trees.composites.Selector(name="Pick Up\nWhere You\nLeft Off")
root = py_trees.composites.Selector(
name="Pick Up\nWhere You\nLeft Off", memory=False
)
root.add_children([high_priority_interrupt, piwylo])
py_trees.display.render_dot_tree(
root, py_trees.common.string_to_visibility_level("all")
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import py_trees

if __name__ == "__main__":
root = py_trees.composites.Selector("Selector")
root = py_trees.composites.Selector("Selector", memory=False)
high = py_trees.behaviours.Success(name="High Priority")
med = py_trees.behaviours.Success(name="Med Priority")
low = py_trees.behaviours.Success(name="Low Priority")
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import py_trees

if __name__ == "__main__":
root = py_trees.composites.Sequence("Sequence")
root = py_trees.composites.Sequence("Sequence", memory=False)
guard = py_trees.behaviours.Success(name="Guard")
a1 = py_trees.behaviours.Success(name="Action 1")
a2 = py_trees.behaviours.Success(name="Action 2")
Expand Down
11 changes: 6 additions & 5 deletions docs/examples/skeleton_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"""Example showing how to create a skeleton behaviour."""

import random
import typing

import py_trees


class Foo(py_trees.behaviour.Behaviour):
"""A skeleton behaviour that inherits from the PyTrees Behaviour class."""

def __init__(self, name):
def __init__(self, name: str) -> None:
"""
Minimal one-time initialisation.
Expand All @@ -22,7 +23,7 @@ def __init__(self, name):
"""
super(Foo, self).__init__(name)

def setup(self):
def setup(self, **kwargs: typing.Any) -> None:
"""
Minimal setup implementation.
Expand Down Expand Up @@ -54,7 +55,7 @@ def setup(self):
"""
self.logger.debug(" %s [Foo::setup()]" % self.name)

def initialise(self):
def initialise(self) -> None:
"""
Minimal initialisation implementation.
Expand All @@ -68,7 +69,7 @@ def initialise(self):
"""
self.logger.debug(" %s [Foo::initialise()]" % self.name)

def update(self):
def update(self) -> py_trees.common.Status:
"""
Minimal update implementation.
Expand All @@ -92,7 +93,7 @@ def update(self):
self.feedback_message = "Uh oh"
return py_trees.common.Status.FAILURE

def terminate(self, new_status):
def terminate(self, new_status: py_trees.common.Status) -> None:
"""
Minimal termination implementation.
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/skeleton_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import py_trees

if __name__ == "__main__":
root = py_trees.composites.Selector("Selector")
root = py_trees.composites.Selector("Selector", memory=False)
high = py_trees.behaviours.Success(name="High Priority")
med = py_trees.behaviours.Success(name="Med Priority")
low = py_trees.behaviours.Success(name="Low Priority")
Expand All @@ -15,7 +15,7 @@
print(py_trees.display.unicode_tree(root=root))
behaviour_tree.setup(timeout=15)

def print_tree(tree):
def print_tree(tree: py_trees.trees.BehaviourTree) -> None:
"""Print the behaviour tree and its current status."""
print(py_trees.display.unicode_tree(root=tree.root, show_status=True))

Expand Down
2 changes: 1 addition & 1 deletion py_trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def visited_setup() -> None:
signal.signal(
_SIGNAL,
functools.partial(
signal_handler, original_signal_handler=original_signal_handler
signal_handler, original_signal_handler=original_signal_handler # type: ignore[arg-type]
),
)
try:
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class ImportTest(unittest.TestCase):
def test_import(self):
def test_import(self) -> None:
"""
This test serves to make the buildfarm happy in Python 3.12 and later.
See https://github.com/colcon/colcon-core/issues/678 for more information.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blackboard_behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def test_set_blackboard_variable() -> None:
)
assert nested_set_foo.status == py_trees.common.Status.FAILURE

class Count:
class Count: # type: ignore[unreachable]
value: int = 0

def generator() -> int:
Expand Down
14 changes: 7 additions & 7 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_repeat_until_success() -> None:
print("child.status == py_trees.common.Status.SUCCESS")
assert child.status == py_trees.common.Status.SUCCESS

decorator.tick_once()
decorator.tick_once() # type: ignore[unreachable]
print("\n--------- Tick 3 ---------\n")
print("decorator.status == py_trees.common.Status.SUCCESS")
assert decorator.status == py_trees.common.Status.SUCCESS
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_repeat_interrupting_failure() -> None:
print("child.status == py_trees.common.Status.SUCCESS")
assert child.status == py_trees.common.Status.SUCCESS

decorator.tick_once()
decorator.tick_once() # type: ignore[unreachable]
print("\n--------- Tick 3 ---------\n")
print("decorator.status == py_trees.common.Status.FAILURE")
assert decorator.status == py_trees.common.Status.FAILURE
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_retry_until_success() -> None:
print("child.status == py_trees.common.Status.FAILURE")
assert child.status == py_trees.common.Status.FAILURE

decorator.tick_once()
decorator.tick_once() # type: ignore[unreachable]
print("\n--------- Tick 3 ---------\n")
print("decorator.status == py_trees.common.Status.SUCCESS")
assert decorator.status == py_trees.common.Status.SUCCESS
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_retry_eventual_failure() -> None:
print("child.status == py_trees.common.Status.FAILURE")
assert child.status == py_trees.common.Status.FAILURE

decorator.tick_once()
decorator.tick_once() # type: ignore[unreachable]
print("\n--------- Tick 3 ---------\n")
print("decorator.status == py_trees.common.Status.FAILURE")
assert decorator.status == py_trees.common.Status.FAILURE
Expand Down Expand Up @@ -465,7 +465,7 @@ def test_timeout() -> None:
print("\n--------- Assertions ---------\n")
print("timeout.status == py_trees.common.Status.FAILURE")
assert timeout.status == py_trees.common.Status.FAILURE
print("running.status == py_trees.common.Status.INVALID")
print("running.status == py_trees.common.Status.INVALID") # type: ignore[unreachable]
assert running.status == py_trees.common.Status.INVALID

# test that it passes on success
Expand All @@ -490,7 +490,7 @@ def test_timeout() -> None:
print("\n--------- Assertions ---------\n")
print("timeout.status == py_trees.common.Status.SUCCESS")
assert timeout.status == py_trees.common.Status.SUCCESS
print("count.status == py_trees.common.Status.SUCCESS")
print("count.status == py_trees.common.Status.SUCCESS") # type: ignore[unreachable]
assert count.status == py_trees.common.Status.SUCCESS

# test that it passes on failure
Expand Down Expand Up @@ -570,7 +570,7 @@ def test_condition() -> None:
print("\n--------- Assertions ---------\n")
print("child.status == py_trees.common.Status.SUCCESS")
assert child.status == py_trees.common.Status.SUCCESS
print("condition.status == py_trees.common.Status.SUCCESS")
print("condition.status == py_trees.common.Status.SUCCESS") # type: ignore[unreachable]
assert condition.status == py_trees.common.Status.SUCCESS


Expand Down
2 changes: 1 addition & 1 deletion tests/test_either_or.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_basic_workflow() -> None:
result=task_one.status,
)
assert task_one.status == py_trees.common.Status.RUNNING
root.tick_once()
root.tick_once() # type: ignore[unreachable]
root.tick_once()
py_trees.tests.print_assert_details(
text="Tick 6 - task one finished",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_eternal_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def impl_eternal_guard_checks(
)
assert eternal_guard.status == py_trees.common.Status.RUNNING

py_trees.tests.tick_tree(root, 3, 3, print_snapshot=True)
py_trees.tests.tick_tree(root, 3, 3, print_snapshot=True) # type: ignore[unreachable]
py_trees.tests.tick_tree(root, 4, 4, print_snapshot=True)
py_trees.tests.tick_tree(root, 5, 5, print_snapshot=True)
print(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ def untest_oneshot_with_subtrees_and_interrupt() -> None:
+ " [{}]".format(py_trees.common.Status.INVALID)
)
assert oneshot.status == py_trees.common.Status.SUCCESS
assert worker_subtree.status == py_trees.common.Status.INVALID
assert worker_subtree.status == py_trees.common.Status.INVALID # type: ignore[unreachable]
6 changes: 3 additions & 3 deletions tests/test_parallels.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_parallel_running() -> None:
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
print("success_after_1.status == py_trees.common.Status.SUCCESS")
print("success_after_1.status == py_trees.common.Status.SUCCESS") # type: ignore[unreachable]
assert success_after_1.status == py_trees.common.Status.SUCCESS
print("running.status == py_trees.common.Status.RUNNING")
assert running.status == py_trees.common.Status.RUNNING
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_parallel_success_on_selected() -> None:
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("running1.status == py_trees.common.Status.INVALID")
print("running1.status == py_trees.common.Status.INVALID") # type: ignore[unreachable]
assert running1.status == py_trees.common.Status.INVALID
print("success1.status == py_trees.common.Status.SUCCESS")
assert success1.status == py_trees.common.Status.SUCCESS
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_parallel_synchronisation() -> None:
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("success.status == py_trees.common.Status.SUCCESS")
print("success.status == py_trees.common.Status.SUCCESS") # type: ignore[unreachable]
assert success.status == py_trees.common.Status.SUCCESS
print("success_every_second.status == py_trees.common.Status.SUCCESS")
assert success_every_second.status == py_trees.common.Status.SUCCESS
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pickup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_high_priority_interrupt() -> None:
print("\n--------- Assertions ---------\n")
print("high_priority_interrupt.status == py_trees.common.Status.SUCCESS")
assert high_priority_interrupt.status == py_trees.common.Status.SUCCESS
print("piwylo.status == py_trees.common.Status.INVALID")
print("piwylo.status == py_trees.common.Status.INVALID") # type: ignore[unreachable]
assert piwylo.status == py_trees.common.Status.INVALID
print("task_one.status == py_trees.common.Status.INVALID")
assert task_one.status == py_trees.common.Status.INVALID
Expand Down
4 changes: 2 additions & 2 deletions tests/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_tick_running_with_no_memory() -> None:
"2::Selector Status", py_trees.common.Status.SUCCESS, root.status
)
assert root.status == py_trees.common.Status.SUCCESS
py_trees.tests.print_assert_details(
py_trees.tests.print_assert_details( # type: ignore[unreachable]
"2::Child 1 Status", py_trees.common.Status.SUCCESS, child_1.status
)
assert child_1.status == py_trees.common.Status.SUCCESS
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_with_memory_priority_handling() -> None:
"1::Selector Status", py_trees.common.Status.SUCCESS, root.status
)
assert root.status == py_trees.common.Status.SUCCESS
py_trees.tests.print_assert_details(
py_trees.tests.print_assert_details( # type: ignore[unreachable]
"2::Child 1 Status", py_trees.common.Status.INVALID, child_1.status
)
assert child_1.status == py_trees.common.Status.INVALID
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_running_with_no_memory_children_do_not_reset() -> None:
"2::Selector Status", py_trees.common.Status.SUCCESS, root.status
)
assert root.status == py_trees.common.Status.SUCCESS
py_trees.tests.print_assert_details(
py_trees.tests.print_assert_details( # type: ignore[unreachable]
"2::Child 1 Status", py_trees.common.Status.SUCCESS, child_1.status
)
assert child_1.status == py_trees.common.Status.SUCCESS
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_running_with_no_memory_invalidate_dangling_runners() -> None:
"2::Child 1 Status", py_trees.common.Status.RUNNING, child_1.status
)
assert child_1.status == py_trees.common.Status.RUNNING
py_trees.tests.print_assert_details(
py_trees.tests.print_assert_details( # type: ignore[unreachable]
"2::Child 2 Status", py_trees.common.Status.INVALID, child_2.status
)
assert child_2.status == py_trees.common.Status.INVALID
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_running_with_memory_proceeds() -> None:
"2::Child 2 Status", py_trees.common.Status.SUCCESS, child_2.status
)
assert child_2.status == py_trees.common.Status.SUCCESS
py_trees.tests.print_assert_details(
py_trees.tests.print_assert_details( # type: ignore[unreachable]
"3::Child 3 Status", py_trees.common.Status.RUNNING, child_3.status
)
assert child_3.status == py_trees.common.Status.RUNNING
Expand Down
Loading

0 comments on commit 1221634

Please sign in to comment.