Skip to content

Commit 118e1a6

Browse files
authored
Make sure isinstance(typing_extensions.ParamSpec("P"), typing.TypeVar) is unaffected by sys.setprofile() (#407)
1 parent 910141a commit 118e1a6

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ jobs:
9292
export path_to_file=$(find dist -type f -name "typing_extensions-*.tar.gz")
9393
echo "::notice::Unpacking source distribution: $path_to_file"
9494
tar xzf $path_to_file -C dist/
95-
cd ${path_to_file%.tar.gz}
96-
python src/test_typing_extensions.py
95+
cd ${path_to_file%.tar.gz}/src
96+
python test_typing_extensions.py
9797
9898
test-sdist-installed:
9999
name: Test installed source distribution

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Unreleased
2+
3+
- Fix incorrect behaviour of `typing_extensions.ParamSpec` on Python 3.8 and
4+
3.9 that meant that
5+
`isinstance(typing_extensions.ParamSpec("P"), typing.TypeVar)` would have a
6+
different result in some situations depending on whether or not a profiling
7+
function had been set using `sys.setprofile`. Patch by Alex Waygood.
8+
19
# Release 4.12.0rc1 (May 16, 2024)
210

311
This release focuses on compatibility with the upcoming release of

src/test_typing_extensions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5020,6 +5020,48 @@ def test_eq(self):
50205020
# won't be the same.
50215021
self.assertNotEqual(hash(ParamSpec('P')), hash(P))
50225022

5023+
def test_isinstance_results_unaffected_by_presence_of_tracing_function(self):
5024+
# See https://github.com/python/typing_extensions/issues/318
5025+
5026+
code = textwrap.dedent(
5027+
"""\
5028+
import sys, typing
5029+
5030+
def trace_call(*args):
5031+
return trace_call
5032+
5033+
def run():
5034+
sys.modules.pop("typing_extensions", None)
5035+
from typing_extensions import ParamSpec
5036+
return isinstance(ParamSpec("P"), typing.TypeVar)
5037+
5038+
isinstance_result_1 = run()
5039+
sys.setprofile(trace_call)
5040+
isinstance_result_2 = run()
5041+
sys.stdout.write(f"{isinstance_result_1} {isinstance_result_2}")
5042+
"""
5043+
)
5044+
5045+
# Run this in an isolated process or it pollutes the environment
5046+
# and makes other tests fail:
5047+
try:
5048+
proc = subprocess.run(
5049+
[sys.executable, "-c", code], check=True, capture_output=True, text=True,
5050+
)
5051+
except subprocess.CalledProcessError as exc:
5052+
print("stdout", exc.stdout, sep="\n")
5053+
print("stderr", exc.stderr, sep="\n")
5054+
raise
5055+
5056+
# Sanity checks that assert the test is working as expected
5057+
self.assertIsInstance(proc.stdout, str)
5058+
result1, result2 = proc.stdout.split(" ")
5059+
self.assertIn(result1, {"True", "False"})
5060+
self.assertIn(result2, {"True", "False"})
5061+
5062+
# The actual test:
5063+
self.assertEqual(result1, result2)
5064+
50235065

50245066
class ConcatenateTests(BaseTestCase):
50255067
def test_basics(self):

src/typing_extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ def kwargs(self):
17111711

17121712
def __init__(self, name, *, bound=None, covariant=False, contravariant=False,
17131713
infer_variance=False, default=NoDefault):
1714-
super().__init__([self])
1714+
list.__init__(self, [self])
17151715
self.__name__ = name
17161716
self.__covariant__ = bool(covariant)
17171717
self.__contravariant__ = bool(contravariant)

0 commit comments

Comments
 (0)