Skip to content

Commit 7b5c54d

Browse files
committed
Add regression tests
1 parent 71b62ca commit 7b5c54d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/test_typing_extensions.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6211,6 +6211,47 @@ def test_is_param_expr(self):
62116211
self.assertTrue(typing._is_param_expr(concat))
62126212
self.assertTrue(typing._is_param_expr(typing_concat))
62136213

6214+
def test_isinstance_results_unaffected_by_presence_of_tracing_function(self):
6215+
# See https://github.com/python/typing_extensions/issues/661
6216+
6217+
code = textwrap.dedent(
6218+
"""\
6219+
import sys, typing
6220+
6221+
def trace_call(*args):
6222+
return trace_call
6223+
6224+
def run():
6225+
sys.modules.pop("typing_extensions", None)
6226+
from typing_extensions import Concatenate
6227+
return isinstance(Concatenate[...], typing._GenericAlias)
6228+
isinstance_result_1 = run()
6229+
sys.setprofile(trace_call)
6230+
isinstance_result_2 = run()
6231+
sys.stdout.write(f"{isinstance_result_1} {isinstance_result_2}")
6232+
"""
6233+
)
6234+
6235+
# Run this in an isolated process or it pollutes the environment
6236+
# and makes other tests fail:
6237+
try:
6238+
proc = subprocess.run(
6239+
[sys.executable, "-c", code], check=True, capture_output=True, text=True,
6240+
)
6241+
except subprocess.CalledProcessError as exc:
6242+
print("stdout", exc.stdout, sep="\n")
6243+
print("stderr", exc.stderr, sep="\n")
6244+
raise
6245+
6246+
# Sanity checks that assert the test is working as expected
6247+
self.assertIsInstance(proc.stdout, str)
6248+
result1, result2 = proc.stdout.split(" ")
6249+
self.assertIn(result1, {"True", "False"})
6250+
self.assertIn(result2, {"True", "False"})
6251+
6252+
# The actual test:
6253+
self.assertEqual(result1, result2)
6254+
62146255
class TypeGuardTests(BaseTestCase):
62156256
def test_basics(self):
62166257
TypeGuard[int] # OK
@@ -6633,6 +6674,46 @@ def test_type_var_inheritance(self):
66336674
self.assertFalse(isinstance(Unpack[Ts], TypeVar))
66346675
self.assertFalse(isinstance(Unpack[Ts], typing.TypeVar))
66356676

6677+
def test_isinstance_results_unaffected_by_presence_of_tracing_function(self):
6678+
# See https://github.com/python/typing_extensions/issues/661
6679+
6680+
code = textwrap.dedent(
6681+
"""\
6682+
import sys, typing
6683+
6684+
def trace_call(*args):
6685+
return trace_call
6686+
6687+
def run():
6688+
sys.modules.pop("typing_extensions", None)
6689+
from typing_extensions import TypeVarTuple, Unpack
6690+
return isinstance(Unpack[TypeVarTuple("Ts")], typing.TypeVar)
6691+
isinstance_result_1 = run()
6692+
sys.setprofile(trace_call)
6693+
isinstance_result_2 = run()
6694+
sys.stdout.write(f"{isinstance_result_1} {isinstance_result_2}")
6695+
"""
6696+
)
6697+
6698+
# Run this in an isolated process or it pollutes the environment
6699+
# and makes other tests fail:
6700+
try:
6701+
proc = subprocess.run(
6702+
[sys.executable, "-c", code], check=True, capture_output=True, text=True,
6703+
)
6704+
except subprocess.CalledProcessError as exc:
6705+
print("stdout", exc.stdout, sep="\n")
6706+
print("stderr", exc.stderr, sep="\n")
6707+
raise
6708+
6709+
# Sanity checks that assert the test is working as expected
6710+
self.assertIsInstance(proc.stdout, str)
6711+
result1, result2 = proc.stdout.split(" ")
6712+
self.assertIn(result1, {"True", "False"})
6713+
self.assertIn(result2, {"True", "False"})
6714+
6715+
# The actual test:
6716+
self.assertEqual(result1, result2)
66366717

66376718
class TypeVarTupleTests(BaseTestCase):
66386719

0 commit comments

Comments
 (0)