Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 5af3d14

Browse files
committed
Fix path types for <3.12
1 parent 6b91b6b commit 5af3d14

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

src/typical/types/path.py

+52-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

33
import pathlib
4+
import sys
5+
from typing import TYPE_CHECKING
46

57
__all__ = ("FilePathError", "FilePath", "DirectoryPathError", "DirectoryPath")
68

@@ -14,33 +16,62 @@ class FilePathError(ValueError):
1416
pass
1517

1618

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."""
1921

20-
See Also:
21-
- :py:class:`pathlib.Path`
22-
"""
22+
pass
2323

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
2824

25+
if TYPE_CHECKING:
2926

30-
class DirectoryPathError(ValueError):
31-
"""Generic error raised when the given value is not a Directory-Path."""
27+
class FilePath(pathlib.Path): ...
3228

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+
"""
3460

61+
if sys.version_info >= (3, 12):
3562

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
3869

39-
See Also:
40-
- :py:class:`pathlib.Path`
41-
"""
70+
else:
4271

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

Comments
 (0)