Skip to content

[3.8] bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336) #17684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 23, 2019
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
6 changes: 5 additions & 1 deletion Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import sys
import stat as st

from _collections_abc import _check_methods

_names = sys.builtin_module_names

# Note: more names are added to __all__ later.
Expand Down Expand Up @@ -1070,7 +1072,9 @@ def __fspath__(self):

@classmethod
def __subclasshook__(cls, subclass):
return hasattr(subclass, '__fspath__')
if cls is PathLike:
return _check_methods(subclass, '__fspath__')
return NotImplemented


if name == 'nt':
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4017,6 +4017,14 @@ def test_bad_pathlike(self):
self.assertRaises(ZeroDivisionError, self.fspath,
FakePath(ZeroDivisionError()))

def test_pathlike_subclasshook(self):
# bpo-38878: subclasshook causes subclass checks
# true on abstract implementation.
class A(os.PathLike):
pass
self.assertFalse(issubclass(FakePath, A))
self.assertTrue(issubclass(FakePath, os.PathLike))


class TimesTests(unittest.TestCase):
def test_times(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
upon inheritence. Patch by Bar Harel.