Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ jobs:
allow-prereleases: true

- name: Install coverage
if: ${{ !startsWith(matrix.python-version, 'pypy') }}
run: |
# Be wary that this does not install typing_extensions in the future
pip install coverage

- name: Test typing_extensions with coverage
if: ${{ !startsWith(matrix.python-version, 'pypy') }}
run: |
# Be wary of running `pip install` here, since it becomes easy for us to
# accidentally pick up typing_extensions as installed by a dependency
Expand All @@ -82,18 +80,9 @@ jobs:
# Run tests under coverage
export COVERAGE_FILE=.coverage_${{ matrix.python-version }}
python -m coverage run -m unittest test_typing_extensions.py
- name: Test typing_extensions no coverage on pypy
if: ${{ startsWith(matrix.python-version, 'pypy') }}
run: |
# Be wary of running `pip install` here, since it becomes easy for us to
# accidentally pick up typing_extensions as installed by a dependency
cd src
python --version # just to make sure we're running the right one
python -m unittest test_typing_extensions.py

- name: Archive code coverage results
id: archive-coverage
if: ${{ !startsWith(matrix.python-version, 'pypy') }}
uses: actions/upload-artifact@v4
with:
name: .coverage_${{ matrix.python-version }}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Unreleased

- Fix incorrect behaviour on Python 3.9 and Python 3.10 that meant that
calling `isinstance` with `typing_extensions.Concatenate[...]` or
`typing_extensions.Unpack[...]` as the first argument could have a different
result in some situations depending on whether or not a profiling function had been
set using `sys.setprofile`. This affected both CPython and PyPy implementations.
Patch by Brian Schubert.
- Fix `__init_subclass__()` behavior in the presence of multiple inheritance involving
an `@deprecated`-decorated base class. Backport of CPython PR
[#138210](https://github.com/python/cpython/pull/138210) by Brian Schubert.
Expand Down
81 changes: 81 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6230,6 +6230,47 @@ def test_is_param_expr(self):
self.assertTrue(typing._is_param_expr(concat))
self.assertTrue(typing._is_param_expr(typing_concat))

def test_isinstance_results_unaffected_by_presence_of_tracing_function(self):
# See https://github.com/python/typing_extensions/issues/661

code = textwrap.dedent(
"""\
import sys, typing

def trace_call(*args):
return trace_call
Comment on lines +6240 to +6241
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curiously, if the trace function accesses f_locals, e.g.

def trace_call(frame, event, arg):
    frame.f_locals
    return trace_call

then the test fails on Python 3.12.0 (and only this version) with this odd error:

 /home/runner/work/typing_extensions/typing_extensions/src/typing_extensions.py:4303: RuntimeWarning: assigning None to unbound local 'name'
  {name: getattr(typing, name) for name in _typing_names if hasattr(typing, name)}
Traceback (most recent call last):
  File "<string>", line 13, in <module>
  File "<string>", line 9, in run
  File "/home/runner/work/typing_extensions/typing_extensions/src/typing_extensions.py", line 4303, in <module>
    {name: getattr(typing, name) for name in _typing_names if hasattr(typing, name)}
           ^^^^^^^^^^^^^^^^^^^^^
TypeError: attribute name must be string, not 'NoneType'

https://github.com/brianschubert/typing_extensions/actions/runs/17386017836/job/49352484071?pr=5

Note that it only fails on the 3.12.0 tests, not the 3.12 tests (which pass).

The same is true for the existing test in ParamSpecTests that these tests were based on.


def run():
sys.modules.pop("typing_extensions", None)
from typing_extensions import Concatenate
return isinstance(Concatenate[...], typing._GenericAlias)
isinstance_result_1 = run()
sys.setprofile(trace_call)
isinstance_result_2 = run()
sys.stdout.write(f"{isinstance_result_1} {isinstance_result_2}")
"""
)

# Run this in an isolated process or it pollutes the environment
# and makes other tests fail:
try:
proc = subprocess.run(
[sys.executable, "-c", code], check=True, capture_output=True, text=True,
)
except subprocess.CalledProcessError as exc:
print("stdout", exc.stdout, sep="\n")
print("stderr", exc.stderr, sep="\n")
raise

# Sanity checks that assert the test is working as expected
self.assertIsInstance(proc.stdout, str)
result1, result2 = proc.stdout.split(" ")
self.assertIn(result1, {"True", "False"})
self.assertIn(result2, {"True", "False"})

# The actual test:
self.assertEqual(result1, result2)

class TypeGuardTests(BaseTestCase):
def test_basics(self):
TypeGuard[int] # OK
Expand Down Expand Up @@ -6652,6 +6693,46 @@ def test_type_var_inheritance(self):
self.assertFalse(isinstance(Unpack[Ts], TypeVar))
self.assertFalse(isinstance(Unpack[Ts], typing.TypeVar))

def test_isinstance_results_unaffected_by_presence_of_tracing_function(self):
# See https://github.com/python/typing_extensions/issues/661

code = textwrap.dedent(
"""\
import sys, typing

def trace_call(*args):
return trace_call

def run():
sys.modules.pop("typing_extensions", None)
from typing_extensions import TypeVarTuple, Unpack
return isinstance(Unpack[TypeVarTuple("Ts")], typing.TypeVar)
isinstance_result_1 = run()
sys.setprofile(trace_call)
isinstance_result_2 = run()
sys.stdout.write(f"{isinstance_result_1} {isinstance_result_2}")
"""
)

# Run this in an isolated process or it pollutes the environment
# and makes other tests fail:
try:
proc = subprocess.run(
[sys.executable, "-c", code], check=True, capture_output=True, text=True,
)
except subprocess.CalledProcessError as exc:
print("stdout", exc.stdout, sep="\n")
print("stderr", exc.stderr, sep="\n")
raise

# Sanity checks that assert the test is working as expected
self.assertIsInstance(proc.stdout, str)
result1, result2 = proc.stdout.split(" ")
self.assertIn(result1, {"True", "False"})
self.assertIn(result2, {"True", "False"})

# The actual test:
self.assertEqual(result1, result2)

class TypeVarTupleTests(BaseTestCase):

Expand Down
9 changes: 7 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,9 @@ class _ConcatenateGenericAlias(list):
__class__ = typing._GenericAlias

def __init__(self, origin, args):
super().__init__(args)
# Cannot use `super().__init__` here because of the `__class__` assignment
# in the class body (https://github.com/python/typing_extensions/issues/661)
list.__init__(self, args)
self.__origin__ = origin
self.__args__ = args

Expand Down Expand Up @@ -2545,7 +2547,10 @@ def __typing_is_unpacked_typevartuple__(self):
def __getitem__(self, args):
if self.__typing_is_unpacked_typevartuple__:
return args
return super().__getitem__(args)
# Cannot use `super().__getitem__` here because of the `__class__` assignment
# in the class body on Python <=3.11
# (https://github.com/python/typing_extensions/issues/661)
return typing._GenericAlias.__getitem__(self, args)

@_UnpackSpecialForm
def Unpack(self, parameters):
Expand Down
Loading