mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336)
Quick subclasshook fix using the same method is being used in collections.abc (up to a certain degree).
This commit is contained in:
		
							parent
							
								
									f522a6ddb6
								
							
						
					
					
						commit
						eae87e3e4e
					
				
					 3 changed files with 15 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
    def __class_getitem__(cls, type):
 | 
			
		||||
        return cls
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4048,6 +4048,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))
 | 
			
		||||
 | 
			
		||||
    def test_pathlike_class_getitem(self):
 | 
			
		||||
        self.assertIs(os.PathLike[bytes], os.PathLike)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
 | 
			
		||||
upon inheritence. Patch by Bar Harel.
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue