From a83472f49b958c55befd82c871be26afbf500306 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 12 Nov 2024 09:54:13 -0800 Subject: [PATCH] gh-126705: Make os.PathLike more like a protocol (#126706) it can now be used as a base class in other protocols --- Lib/test/test_typing.py | 4 ++++ Lib/typing.py | 1 + .../Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst | 1 + 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 244ce1e5da9..aa42beca5f9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -8,6 +8,7 @@ import gc import inspect import itertools import operator +import os import pickle import re import sys @@ -4252,6 +4253,9 @@ class ProtocolTests(BaseTestCase): class CustomProtocol(TestCase, Protocol): pass + class CustomPathLikeProtocol(os.PathLike, Protocol): + pass + class CustomContextManager(typing.ContextManager, Protocol): pass diff --git a/Lib/typing.py b/Lib/typing.py index 8e6381033fd..938e52922ae 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1944,6 +1944,7 @@ _PROTO_ALLOWLIST = { 'Reversible', 'Buffer', ], 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'], + 'os': ['PathLike'], } diff --git a/Misc/NEWS.d/next/Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst b/Misc/NEWS.d/next/Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst new file mode 100644 index 00000000000..f49c9c765d7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst @@ -0,0 +1 @@ +Allow :class:`os.PathLike` to be a base for Protocols.