Skip to content

Commit d345923

Browse files
Copilotcomfuture
andcommitted
Fix Python 3.9 compatibility issues in test_examples.py
Co-authored-by: comfuture <[email protected]>
1 parent 482f667 commit d345923

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ classifiers = [
1616
"Development Status :: 3 - Alpha",
1717
"Programming Language :: Python :: 3.9",
1818
]
19+
dependencies = [
20+
"typing-extensions>=4.12.2; python_version<'3.11'"
21+
]
1922

2023
[project.urls]
2124
Homepage = "https://github.com/comfuture/function-schema"

test/test_examples.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import sys
88
from pathlib import Path
9+
import pytest
910

1011

1112
def test_examples_syntax():
@@ -32,14 +33,22 @@ def test_examples_importable():
3233

3334
# Test that the file can be imported (checks imports)
3435
module_name = example_file.stem
35-
spec = __import__('importlib.util', fromlist=['spec_from_file_location']).spec_from_file_location(module_name, example_file)
36+
import importlib.util
37+
spec = importlib.util.spec_from_file_location(module_name, example_file)
3638
try:
37-
module = __import__('importlib.util', fromlist=['module_from_spec']).module_from_spec(spec)
38-
# Don't execute the module, just load it to check imports
39+
module = importlib.util.module_from_spec(spec)
40+
# Execute the module to check imports (but not __main__ block)
41+
old_name = module.__name__
42+
module.__name__ = "__not_main__" # Prevent __main__ block execution
43+
spec.loader.exec_module(module)
44+
module.__name__ = old_name
3945
except ImportError as e:
4046
# Only fail if it's importing function_schema - other imports are optional
4147
if "function_schema" in str(e):
4248
pytest.fail(f"Import error in {example_file}: {e}")
49+
except Exception as e:
50+
# Other exceptions during module execution are not import issues
51+
pass
4352

4453

4554
def test_cli_example_functions():

0 commit comments

Comments
 (0)