Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Add support for `AsyncIterator`, `io.Reader`, `io.Writer` and `os.PathLike` protocols.
- 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
Expand Down
11 changes: 9 additions & 2 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import inspect
import io
import itertools
import os
import pickle
import re
import subprocess
Expand Down Expand Up @@ -3869,9 +3870,15 @@ def test_builtin_protocol_allowlist(self):
class CustomProtocol(TestCase, Protocol):
pass

class CustomPathLikeProtocol(os.PathLike, Protocol):
pass

class CustomContextManager(typing.ContextManager, Protocol):
pass

class CustomAsyncIterator(typing.AsyncIterator, Protocol):
pass

@skip_if_py312b1
def test_typing_extensions_protocol_allowlist(self):
@runtime_checkable
Expand Down Expand Up @@ -7064,13 +7071,13 @@ def test_typing_extensions_defers_when_possible(self):
}
if sys.version_info < (3, 13):
exclude |= {
'NamedTuple', 'Protocol', 'runtime_checkable', 'Generator',
'NamedTuple', 'runtime_checkable', 'Generator',
'AsyncGenerator', 'ContextManager', 'AsyncContextManager',
'ParamSpec', 'TypeVar', 'TypeVarTuple', 'get_type_hints',
}
if sys.version_info < (3, 14):
exclude |= {
'TypeAliasType'
'TypeAliasType', 'Protocol'
}
if not typing_extensions._PEP_728_IMPLEMENTED:
exclude |= {'TypedDict', 'is_typeddict'}
Expand Down
11 changes: 8 additions & 3 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,13 @@ def __getitem__(self, params):
_PROTO_ALLOWLIST = {
'collections.abc': [
'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer',
'AsyncIterator', 'Hashable', 'Sized', 'Container', 'Collection',
'Reversible', 'Buffer',
],
'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
'io': ['Reader', 'Writer'],
'typing_extensions': ['Buffer'],
'os': ['PathLike'],
}


Expand Down Expand Up @@ -653,8 +656,10 @@ def _caller(depth=1, default='__main__'):

# `__match_args__` attribute was removed from protocol members in 3.13,
# we want to backport this change to older Python versions.
# Breakpoint: https://github.com/python/cpython/pull/110683
if sys.version_info >= (3, 13):
# 3.14 additionally added `io.Reader`, `io.Writer` and `os.PathLike` to
# the list of allowed protocol allowlist.
# https://github.com/python/cpython/issues/127647
if sys.version_info >= (3, 14):
Protocol = typing.Protocol
else:
def _allow_reckless_class_checks(depth=2):
Expand Down
Loading