Skip to content

Commit dfe43fc

Browse files
committed
drop deprecated pkg_resources
Each time we run pytest in our GitHub Actions, we get the warning: ```tests/test_mavlogdump.py:9 /home/runner/work/pymavlink/pymavlink/tests/test_mavlogdump.py:9: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. import pkg_resources ``` Use `here = pathlib.Path(__file__).parent` to replace `pkg_resources.resource_filename(__name__, xxx)`. All modified files are run by pytest in our GitHub Actions.
1 parent cc7395f commit dfe43fc

File tree

4 files changed

+25
-42
lines changed

4 files changed

+25
-42
lines changed

tests/test_mavlogdump.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"""
55
regression tests for mavlogdump.py
66
"""
7-
import unittest
87
import os
9-
import pkg_resources
10-
import sys
8+
import unittest
9+
from pathlib import Path
10+
11+
here = Path(__file__).parent
1112

1213
class MAVLogDumpTest(unittest.TestCase):
1314

@@ -21,9 +22,7 @@ def __init__(self, *args, **kwargs):
2122

2223
def test_dump_same(self):
2324
"""Test dump of file is what we expect"""
24-
test_filename = "test.BIN"
25-
test_filepath = pkg_resources.resource_filename(__name__,
26-
test_filename)
25+
test_filepath = str(here / "test.BIN")
2726
dump_filename = "tmp.dump"
2827
os.system("mavlogdump.py %s >%s" % (test_filepath, dump_filename))
2928
with open(dump_filename) as f:
@@ -33,8 +32,7 @@ def test_dump_same(self):
3332
"test.BIN.dumped"]
3433
success = False
3534
for expected in possibles:
36-
expected_filepath = pkg_resources.resource_filename(__name__,
37-
expected)
35+
expected_filepath = str(here / expected)
3836
with open(expected_filepath) as e:
3937
expected = e.read()
4038

tests/test_mavxml.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,33 @@
44
"""
55

66
import unittest
7-
import pkg_resources
7+
from pathlib import Path
88

99
from pymavlink.generator.mavparse import MAVXML
1010
from pymavlink.generator.mavparse import MAVParseError
1111

12+
here = Path(__file__).parent
13+
1214
class MAVXMLTest(unittest.TestCase):
1315
"""
1416
Class to test MAVXML
1517
"""
1618

1719
def test_fields_number(self):
1820
"""Test that a message can have at most 64 fields"""
19-
test_filename = "64-fields.xml"
20-
test_filepath = pkg_resources.resource_filename(__name__,
21-
test_filename)
21+
test_filepath = str(here / "64-fields.xml")
2222
xml = MAVXML(test_filepath)
2323
count = len(xml.message[0].fields)
2424
self.assertEqual(count, 64)
2525

26-
test_filename = "65-fields.xml"
27-
test_filepath = pkg_resources.resource_filename(__name__,
28-
test_filename)
26+
test_filepath = str(here / "65-fields.xml")
2927
with self.assertRaises(MAVParseError):
3028
_ = MAVXML(test_filepath)
3129

32-
3330
def test_wire_protocol_version(self):
3431
"""Test that an unknown MAVLink wire protocol version raises an exception"""
3532
with self.assertRaises(MAVParseError):
3633
_ = MAVXML(filename="", wire_protocol_version=42)
3734

38-
3935
if __name__ == '__main__':
4036
unittest.main()

tests/test_trim.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"""
77

88
import unittest
9-
import os
10-
import pkg_resources
11-
import sys
9+
1210
from pymavlink import mavutil
1311

1412
class PayLoadTrimZeros(unittest.TestCase):

tests/test_wp.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
regression tests for mavwp.py
55
"""
66

7-
import unittest
87
import os
9-
import pkg_resources
10-
import sys
8+
import unittest
9+
from pathlib import Path
1110

1211
os.environ["MAVLINK20"] = "1"
1312

1413
from pymavlink import mavwp
1514
from pymavlink import mavutil
1615

16+
here = Path(__file__).parent
17+
1718
class MAVWPTest(unittest.TestCase):
1819

1920
"""
@@ -42,14 +43,9 @@ def makewp(self, seq):
4243
seq, # wp.z
4344
)
4445

45-
def make_wps(self):
46-
# create waypoints
47-
count = 4
48-
waypoints = []
49-
for i in range(count):
50-
waypoints.append(self.makewp(i))
51-
52-
return waypoints
46+
def make_wps(self, count: int = 4):
47+
# create count waypoints
48+
return [self.makewp(i) for i in range(count)]
5349

5450
def test_add_remove(self):
5551
"""Test we can add/remove waypoints to/from mavwp"""
@@ -60,9 +56,9 @@ def test_add_remove(self):
6056
waypoints = self.make_wps()
6157

6258
# make sure basic addition works
63-
for i in range(len(waypoints)):
59+
for i, waypoint in enumerate(waypoints):
6460
self.assertEqual(loader.count(), i)
65-
loader.add(waypoints[i])
61+
loader.add(waypoint)
6662
self.assertEqual(loader.count(), i+1)
6763

6864
self.assertEqual(loader.wp(0).seq, 0)
@@ -76,7 +72,6 @@ def test_add_remove(self):
7672
self.assertEqual(loader.item(1).z, 1)
7773
self.assertEqual(loader.item(2).z, 2)
7874

79-
8075
# remove a middle one, make sure things get renumbered
8176
loader.remove(waypoints[0])
8277
self.assertEqual(loader.wp(0).seq, 0)
@@ -194,8 +189,6 @@ def test_wp_is_loiter(self):
194189
self.assertFalse(loader.wp_is_loiter(1))
195190
self.assertFalse(loader.wp_is_loiter(2))
196191

197-
assert True
198-
199192
def test_is_location_command(self):
200193
loader = mavwp.MAVWPLoader()
201194
self.assertFalse(loader.is_location_command(mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH))
@@ -213,13 +206,13 @@ def test_rally_load(self):
213206
self.assertTrue(loader.is_location_command(mavutil.mavlink.MAV_CMD_NAV_RALLY_POINT))
214207

215208
# test loading a QGC WPL 110 file:
216-
rally_filepath = pkg_resources.resource_filename(__name__, "rally-110.txt")
209+
rally_filepath = str(here / "rally-110.txt")
217210
loader.load(rally_filepath)
218211
self.assertEqual(loader.count(), 2)
219212
self.assertEqual(loader.wp(0).command, mavutil.mavlink.MAV_CMD_NAV_RALLY_POINT)
220213

221214
# test loading tradition "RALLY" style format:
222-
rally_filepath = pkg_resources.resource_filename(__name__, "rally.txt")
215+
rally_filepath = str(here / "rally.txt")
223216
loader.load(rally_filepath)
224217
self.assertEqual(loader.count(), 2)
225218
self.assertEqual(loader.wp(0).command, mavutil.mavlink.MAV_CMD_NAV_RALLY_POINT)
@@ -233,15 +226,13 @@ def test_fence_load(self):
233226
self.assertTrue(loader.is_location_command(mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_EXCLUSION))
234227

235228
# test loading a QGC WPL 110 file:
236-
fence_filepath = pkg_resources.resource_filename(__name__,
237-
"fence-110.txt")
229+
fence_filepath = str(here / "fence-110.txt")
238230
loader.load(fence_filepath)
239231
self.assertEqual(loader.count(), 10)
240232
self.assertEqual(loader.wp(3).command, mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_INCLUSION)
241233

242234
# test loading tradition lat/lng-pair style format:
243-
fence_filepath = pkg_resources.resource_filename(__name__,
244-
"fence.txt")
235+
fence_filepath = str(here / "fence.txt")
245236
loader.load(fence_filepath)
246237
# there are 6 lines in the file - one return point, four fence
247238
# points and a "fence closing point". We don't store the

0 commit comments

Comments
 (0)