1
1
from __future__ import annotations
2
2
3
3
import pathlib
4
+ import sys
5
+ from typing import TYPE_CHECKING
4
6
5
7
__all__ = ("FilePathError" , "FilePath" , "DirectoryPathError" , "DirectoryPath" )
6
8
@@ -14,33 +16,62 @@ class FilePathError(ValueError):
14
16
pass
15
17
16
18
17
- class FilePath ( PathType ): # type: ignore
18
- """A path object pointing to a file.
19
+ class DirectoryPathError ( ValueError ):
20
+ """Generic error raised when the given value is not a Directory-Path."""
19
21
20
- See Also:
21
- - :py:class:`pathlib.Path`
22
- """
22
+ pass
23
23
24
- def __init__ (self , * pathsegments : str ):
25
- super ().__init__ (* pathsegments )
26
- if not self .is_file ():
27
- raise FilePathError (f"{ self } is not a valid file-path" ) from None
28
24
25
+ if TYPE_CHECKING :
29
26
30
- class DirectoryPathError (ValueError ):
31
- """Generic error raised when the given value is not a Directory-Path."""
27
+ class FilePath (pathlib .Path ): ...
32
28
33
- pass
29
+ class DirectoryPath (pathlib .Path ): ...
30
+
31
+ else :
32
+
33
+ class FilePath (PathType ): # type: ignore
34
+ """A path object pointing to a file.
35
+
36
+ See Also:
37
+ - :py:class:`pathlib.Path`
38
+ """
39
+
40
+ if sys .version_info >= (3 , 12 ):
41
+
42
+ def __init__ (self , * pathsegments : str ):
43
+ super ().__init__ (* pathsegments )
44
+ if not self .is_file ():
45
+ raise FilePathError (f"{ self } is not a valid file-path" ) from None
46
+
47
+ else :
48
+
49
+ def __init__ (self , * pathsegments : str ):
50
+ super ().__init__ ()
51
+ if not self .is_file ():
52
+ raise FilePathError (f"{ self } is not a valid file-path" ) from None
53
+
54
+ class DirectoryPath (PathType ): # type: ignore
55
+ """A path object pointing to a directory.
56
+
57
+ See Also:
58
+ - :py:class:`pathlib.Path`
59
+ """
34
60
61
+ if sys .version_info >= (3 , 12 ):
35
62
36
- class DirectoryPath (PathType ): # type: ignore
37
- """A path object pointing to a directory.
63
+ def __init__ (self , * pathsegments : str ):
64
+ super ().__init__ (* pathsegments )
65
+ if not self .is_dir ():
66
+ raise DirectoryPathError (
67
+ f"{ self } is not a valid directory-path"
68
+ ) from None
38
69
39
- See Also:
40
- - :py:class:`pathlib.Path`
41
- """
70
+ else :
42
71
43
- def __init__ (self , * pathsegments : str ):
44
- super ().__init__ (* pathsegments )
45
- if not self .is_dir ():
46
- raise DirectoryPathError (f"{ self } is not a valid directory-path" ) from None
72
+ def __init__ (self , * pathsegments : str ):
73
+ super ().__init__ ()
74
+ if not self .is_dir ():
75
+ raise DirectoryPathError (
76
+ f"{ self } is not a valid directory-path"
77
+ ) from None
0 commit comments