Skip to content

Commit

Permalink
restore support for types with attribute value_type
Browse files Browse the repository at this point in the history
added a deprecation warning and updated the test

Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
  • Loading branch information
mikaelarguedas committed Jul 26, 2019
1 parent 1ab0b8d commit 0002388
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions rosidl_runtime_py/rosidl_runtime_py/import_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@

import importlib
from typing import Any
import warnings

from rosidl_parser.definition import NamespacedType


def import_message_from_namespaced_type(message_type: NamespacedType) -> Any:
if not isinstance(message_type, NamespacedType):
if hasattr(message_type, 'value_type'):
message_type = message_type.value_type
warnings.warn(
'Passing objects containing a NamespacedType is deprecated, '
'please pass a NamespacedType object directly',
DeprecationWarning)
module = importlib.import_module(
'.'.join(message_type.namespaces))
return getattr(module, message_type.name)
15 changes: 10 additions & 5 deletions rosidl_runtime_py/test/rosidl_runtime_py/test_import_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import warnings

from rosidl_parser.definition import AbstractNestedType
from rosidl_parser.definition import NamespacedType
from rosidl_parser.definition import UnboundedSequence

Expand All @@ -26,10 +27,14 @@ def test_import_namespaced_type():
fixture_namespaced_type = NamespacedType(['test_msgs', 'msg'], 'Empty')
imported_message = import_message_from_namespaced_type(fixture_namespaced_type)
assert type(imported_message) == type(Empty)

not_namespaced_type = UnboundedSequence(fixture_namespaced_type)
assert not isinstance(not_namespaced_type, NamespacedType)
with pytest.raises(AttributeError):
import_message_from_namespaced_type(not_namespaced_type)
imported_message2 = import_message_from_namespaced_type(
not_namespaced_type.value_type)
assert isinstance(not_namespaced_type, AbstractNestedType)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
imported_message2 = import_message_from_namespaced_type(not_namespaced_type)
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert type(imported_message2) == type(Empty)

0 comments on commit 0002388

Please sign in to comment.