From 271a77cb9928f0e9fd5f3fda297af47dcd96c0e0 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 11 Nov 2024 14:52:54 -0800 Subject: [PATCH 1/3] Make os.PathLike more like a protocol --- Lib/os.py | 2 +- Lib/test/test_typing.py | 3 +++ Lib/typing.py | 1 + .../Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-11-11-14-52-21.gh-issue-126705.0W7jFW.rst diff --git a/Lib/os.py b/Lib/os.py index 9c2258e6ccf5ba..86d16de7cf295d 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -1118,7 +1118,7 @@ def _fspath(path): fspath.__name__ = "fspath" -class PathLike(abc.ABC): +class PathLike(metaclass=abc.ABCMeta): """Abstract base class for implementing the file system path protocol.""" diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 2f1f9e86a0bce4..87ed3f2c67056c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4252,6 +4252,9 @@ def test_builtin_protocol_allowlist(self): 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 c924c767042552..922a887599425c 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1943,6 +1943,7 @@ def _allow_reckless_class_checks(depth=2): 'Hashable', 'Sized', 'Container', 'Collection', '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 00000000000000..f49c9c765d778f --- /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. From 5dbce96b4fd61411a7f53c66bc2b7b02d8a7840a Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 11 Nov 2024 15:06:11 -0800 Subject: [PATCH 2/3] missed import in test file --- Lib/test/test_typing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 87ed3f2c67056c..3c316ff3482373 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -8,6 +8,7 @@ import inspect import itertools import operator +import os import pickle import re import sys From fc4d593b9479ba055c443bef939f459715dd78f8 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 12 Nov 2024 09:22:57 -0800 Subject: [PATCH 3/3] don't change the base class --- Lib/os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index 86d16de7cf295d..9c2258e6ccf5ba 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -1118,7 +1118,7 @@ def _fspath(path): fspath.__name__ = "fspath" -class PathLike(metaclass=abc.ABCMeta): +class PathLike(abc.ABC): """Abstract base class for implementing the file system path protocol."""