From 1b789deb14682198d6e27dce9eb0d8434890a9b8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 1 Mar 2024 12:03:14 +0100 Subject: [PATCH] Added Literal type hint for <3.8 Python --- PythonAPI/carla/agents/tools/hints.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PythonAPI/carla/agents/tools/hints.py b/PythonAPI/carla/agents/tools/hints.py index 3e14748b1f1..475d93d3dd3 100644 --- a/PythonAPI/carla/agents/tools/hints.py +++ b/PythonAPI/carla/agents/tools/hints.py @@ -11,10 +11,10 @@ ObstacleDetectionResult = namedtuple('ObstacleDetectionResult', ['obstacle_was_found', 'obstacle', 'distance']) TrafficLightDetectionResult = namedtuple('TrafficLightDetectionResult', ['traffic_light_was_found', 'traffic_light']) else: - from typing import NamedTuple, Union + from typing import NamedTuple, Union, TYPE_CHECKING from carla import Actor, TrafficLight """ - # Python 3.6+ + # Python 3.6+, incompatible with Python 2.7 syntax class ObstacleDetectionResult(NamedTuple): obstacle_was_found : bool obstacle : Union[Actor, None] @@ -25,5 +25,10 @@ class TrafficLightDetectionResult(NamedTuple): traffic_light_was_found : bool traffic_light : Union[TrafficLight, None] """ - ObstacleDetectionResult = NamedTuple('ObstacleDetectionResult', [('obstacle_was_found', bool), ('obstacle', Union[Actor, None]), ('distance', float)]) + if TYPE_CHECKING: + from typing import Literal + ObstacleDetectionResult = NamedTuple('ObstacleDetectionResult', [('obstacle_was_found', bool), ('obstacle', Union[Actor, None]), ('distance', Union[float, Literal[-1]])]) + else: + ObstacleDetectionResult = NamedTuple('ObstacleDetectionResult', [('obstacle_was_found', bool), ('obstacle', Union[Actor, None]), ('distance', float)]) + TrafficLightDetectionResult = NamedTuple('TrafficLightDetectionResult', [('traffic_light_was_found', bool), ('traffic_light', Union[TrafficLight, None])])