Skip to content

Commit

Permalink
Add types and ament_mypy to rpyutils. (#12)
Browse files Browse the repository at this point in the history
* types

* system check

* add dep

* add None check

Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC authored Sep 6, 2024
1 parent 366b4ba commit 33477f0
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend>
Expand Down
12 changes: 10 additions & 2 deletions rpyutils/add_dll_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
from contextlib import contextmanager
import os
import sys
from typing import Any, Generator, List, TYPE_CHECKING

if TYPE_CHECKING:
if sys.platform == 'win32':
from os import _AddedDllDirectory
else:
_AddedDllDirectory = Any


@contextmanager
def add_dll_directories_from_env(env_name: str):
def add_dll_directories_from_env(env_name: str
) -> 'Generator[List[_AddedDllDirectory], None, None]':
"""
Add a list of directories from an environment variable to the DLL search path on Windows.
Expand All @@ -38,7 +46,7 @@ def add_dll_directories_from_env(env_name: str):
:param env_name: The name of the environment variable with DLL search paths.
:return: A list of handles to directories.
"""
dll_dir_handles = []
dll_dir_handles: 'List[_AddedDllDirectory]' = []
# This function only makes sense on Windows and if the function 'add_dll_directory' exists
if sys.platform == 'win32' and hasattr(os, 'add_dll_directory'):
env_value = os.environ.get(env_name)
Expand Down
9 changes: 6 additions & 3 deletions rpyutils/import_c_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import importlib
import os
from pathlib import Path
from types import ModuleType
from typing import Optional

from rpyutils import add_dll_directories_from_env


def import_c_library(name: str, package: Optional[str] = None):
def import_c_library(name: str, package: Optional[str] = None) -> ModuleType:
"""
Import and return a C extension library using importlib, with consistent error messaging.
Expand All @@ -41,8 +42,10 @@ def import_c_library(name: str, package: Optional[str] = None):
distro = os.environ.get('ROS_DISTRO', 'rolling')
if e.path is None:
import sysconfig
expected_path = Path(__file__).parents[1] / (
name[1:] + sysconfig.get_config_var('EXT_SUFFIX'))
config_vars = sysconfig.get_config_var('EXT_SUFFIX')
if config_vars:
expected_path = Path(__file__).parents[1] / (
name[1:] + config_vars)
assert not expected_path.is_file()
link = f'https://docs.ros.org/en/{distro}/How-To-Guides/Installation-Troubleshooting.html#import-failing-without-library-present-on-the-system' # noqa: E501
e.msg += \
Expand Down
4 changes: 3 additions & 1 deletion test/rpyutils/test_add_dll_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path
import sys

from pytest import MonkeyPatch
from rpyutils import add_dll_directories_from_env


def test_add_dll_directories_from_env(monkeypatch, tmp_path):
def test_add_dll_directories_from_env(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
# Test with empty value
monkeypatch.delenv('TEST_ENV', raising=False)
with add_dll_directories_from_env('TEST_ENV') as dlls:
Expand Down
2 changes: 1 addition & 1 deletion test/test_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

@pytest.mark.copyright
@pytest.mark.linter
def test_copyright():
def test_copyright() -> None:
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found errors'
2 changes: 1 addition & 1 deletion test/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@pytest.mark.flake8
@pytest.mark.linter
def test_flake8():
def test_flake8() -> None:
rc, errors = main_with_errors(argv=[])
assert rc == 0, \
'Found %d code style errors / warnings:\n' % len(errors) + \
Expand Down
23 changes: 23 additions & 0 deletions test/test_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ament_mypy.main import main
import pytest


@pytest.mark.mypy
@pytest.mark.linter
def test_mypy() -> None:
rc = main(argv=[])
assert rc == 0, 'Found type errors!'
2 changes: 1 addition & 1 deletion test/test_pep257.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

@pytest.mark.linter
@pytest.mark.pep257
def test_pep257():
def test_pep257() -> None:
rc = main(argv=[])
assert rc == 0, 'Found code style errors / warnings'
2 changes: 1 addition & 1 deletion test/test_xmllint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

@pytest.mark.linter
@pytest.mark.xmllint
def test_xmllint():
def test_xmllint() -> None:
rc = main(argv=[])
assert rc == 0, 'Found errors'

0 comments on commit 33477f0

Please sign in to comment.