Skip to content

Commit 891d2a9

Browse files
thomas-chauchefoin-tobclaudegithub-code-quality[bot]
authored
Use pre-generated TorchScript fixture to avoid torch.jit deprecation warnings (#254)
* Use pre-generated TorchScript fixture to avoid torch.jit deprecation warnings torch.jit.script/torch.jit.save emit DeprecationWarnings on Python 3.14+ and may be removed in a future PyTorch release. Replace runtime JIT calls in test setUp with a pre-generated (smaller) fixture file, and include a standalone script (test/fixtures/generate_fixtures.py) to regenerate it with pinned versions of torch and torchvision. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Potential fix for pull request finding 'Unused import' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent 41ce7cb commit 891d2a9

4 files changed

Lines changed: 43 additions & 25 deletions

File tree

test/fixtures/generate_fixtures.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# /// script
3+
# requires-python = ">=3.10,<3.14"
4+
# dependencies = ["torch>=2.10,<3", "torchvision>=0.25,<1"]
5+
# ///
6+
"""Generate TorchScript fixture files for tests.
7+
8+
These fixtures are checked into the repository so that tests don't need
9+
to call torch.jit.script/torch.jit.save at runtime (which emits
10+
deprecation warnings on Python 3.14+ and may be removed in future
11+
PyTorch versions).
12+
13+
Usage:
14+
uv run test/fixtures/generate_fixtures.py
15+
"""
16+
17+
from pathlib import Path
18+
19+
import torch
20+
import torchvision.models as models
21+
22+
FIXTURES_DIR = Path(__file__).parent
23+
24+
25+
def main():
26+
model = models.squeezenet1_0()
27+
scripted = torch.jit.script(model)
28+
29+
out = FIXTURES_DIR / "squeezenet1_0_torchscript_v1_4.pt"
30+
torch.jit.save(scripted, out)
31+
print(f"Generated {out} ({out.stat().st_size} bytes)")
32+
33+
34+
if __name__ == "__main__":
35+
main()
4.83 MB
Binary file not shown.

test/test_polyglot.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import random
22
import string
3-
import sys
43
import tarfile
54
import tempfile
65
import unittest
@@ -15,7 +14,7 @@
1514
import fickling.polyglot as polyglot
1615
from fickling.polyglot import FileProperties
1716

18-
_lacks_torch_jit_support = sys.version_info >= (3, 14)
17+
FIXTURES_DIR = Path(__file__).parent / "fixtures"
1918

2019

2120
def _make_properties(**overrides):
@@ -79,17 +78,9 @@ def setUp(self):
7978
self.filename_legacy_pickle = tmppath / "model_legacy_pickle.pth"
8079
torch.save(model, self.filename_legacy_pickle, _use_new_zipfile_serialization=False)
8180

82-
if not _lacks_torch_jit_support:
83-
# TorchScript v1.4
84-
m = torch.jit.script(model)
85-
self.filename_torchscript = tmppath / "model_torchscript.pt"
86-
torch.jit.save(m, self.filename_torchscript)
87-
88-
# TorchScript v1.4 Dup
89-
self.filename_torchscript_dup = tmppath / "model_torchscript_dup.pt"
90-
torch.jit.save(m, self.filename_torchscript_dup)
91-
92-
self.standard_torchscript_polyglot_name = tmppath / "test_polyglot.pt"
81+
# TorchScript v1.4 (pre-generated fixtures to avoid torch.jit deprecation warnings)
82+
self.filename_torchscript = FIXTURES_DIR / "squeezenet1_0_torchscript_v1_4.pt"
83+
self.standard_torchscript_polyglot_name = tmppath / "test_polyglot.pt"
9384

9485
# PyTorch v0.1.1
9586
self.filename_legacy_tar = tmppath / "model_legacy_tar.pth"
@@ -132,7 +123,6 @@ def test_legacy_pickle(self):
132123
formats = polyglot.identify_pytorch_file_format(self.filename_legacy_pickle)
133124
self.assertEqual(formats, ["PyTorch v0.1.10"])
134125

135-
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")
136126
def test_torchscript(self):
137127
formats = polyglot.identify_pytorch_file_format(self.filename_torchscript)
138128
self.assertEqual(formats, ["TorchScript v1.4", "TorchScript v1.3", "PyTorch v1.3"])
@@ -200,7 +190,6 @@ def test_legacy_pickle_properties(self):
200190
proper_result = _make_properties(is_valid_pickle=True)
201191
self.assertEqual(properties, proper_result)
202192

203-
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")
204193
def test_torchscript_properties(self):
205194
properties = polyglot.find_file_properties(self.filename_torchscript)
206195
proper_result = _make_properties(
@@ -219,11 +208,10 @@ def test_zip_properties(self):
219208
proper_result = _make_properties(is_standard_zip=True, is_standard_not_torch=True)
220209
self.assertEqual(properties, proper_result)
221210

222-
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")
223211
def test_create_standard_torchscript_polyglot(self):
224212
polyglot.create_polyglot(
225213
self.filename_v1_3_dup,
226-
self.filename_torchscript_dup,
214+
self.filename_torchscript,
227215
self.standard_torchscript_polyglot_name,
228216
print_results=False,
229217
)

test/test_pytorch.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import tempfile
32
import unittest
43
from pathlib import Path
@@ -9,7 +8,7 @@
98
from fickling.fickle import Pickled
109
from fickling.pytorch import PyTorchModelWrapper
1110

12-
_lacks_torch_jit_support = sys.version_info >= (3, 14)
11+
FIXTURES_DIR = Path(__file__).parent / "fixtures"
1312

1413

1514
class TestPyTorchModule(unittest.TestCase):
@@ -21,10 +20,8 @@ def setUp(self):
2120
self.filename_v1_3 = tmppath / "test_model.pth"
2221
torch.save(model, self.filename_v1_3)
2322

24-
if not _lacks_torch_jit_support:
25-
m = torch.jit.script(model)
26-
self.torchscript_filename = tmppath / "test_model_torchscript.pth"
27-
torch.jit.save(m, self.torchscript_filename)
23+
# Pre-generated fixture to avoid torch.jit deprecation warnings
24+
self.torchscript_filename = FIXTURES_DIR / "squeezenet1_0_torchscript_v1_4.pt"
2825

2926
def tearDown(self):
3027
self.tmpdir.cleanup()
@@ -35,7 +32,6 @@ def test_wrapper(self):
3532
except Exception as e: # noqa
3633
self.fail(f"PyTorchModelWrapper was not able to load a PyTorch v1.3 file: {e}")
3734

38-
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")
3935
def test_torchscript_wrapper(self):
4036
try:
4137
PyTorchModelWrapper(self.torchscript_filename)
@@ -47,7 +43,6 @@ def test_pickled(self):
4743
pickled_portion = result.pickled
4844
self.assertIsInstance(pickled_portion, Pickled)
4945

50-
@unittest.skipIf(_lacks_torch_jit_support, "PyTorch 2.9.1 JIT broken with Python 3.14+")
5146
def test_torchscript_pickled(self):
5247
result = PyTorchModelWrapper(self.torchscript_filename)
5348
pickled_portion = result.pickled

0 commit comments

Comments
 (0)