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

Commit

Permalink
Fix path types for <3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
seandstewart committed Feb 28, 2024
1 parent 6b91b6b commit 5af3d14
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions src/typical/types/path.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import pathlib
import sys
from typing import TYPE_CHECKING

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

Expand All @@ -14,33 +16,62 @@ class FilePathError(ValueError):
pass


class FilePath(PathType): # type: ignore
"""A path object pointing to a file.
class DirectoryPathError(ValueError):
"""Generic error raised when the given value is not a Directory-Path."""

See Also:
- :py:class:`pathlib.Path`
"""
pass

def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)
if not self.is_file():
raise FilePathError(f"{self} is not a valid file-path") from None

if TYPE_CHECKING:

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

pass
class DirectoryPath(pathlib.Path): ...

else:

class FilePath(PathType): # type: ignore
"""A path object pointing to a file.
See Also:
- :py:class:`pathlib.Path`
"""

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

def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)
if not self.is_file():
raise FilePathError(f"{self} is not a valid file-path") from None

else:

def __init__(self, *pathsegments: str):
super().__init__()
if not self.is_file():
raise FilePathError(f"{self} is not a valid file-path") from None

class DirectoryPath(PathType): # type: ignore
"""A path object pointing to a directory.
See Also:
- :py:class:`pathlib.Path`
"""

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

class DirectoryPath(PathType): # type: ignore
"""A path object pointing to a directory.
def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)
if not self.is_dir():
raise DirectoryPathError(
f"{self} is not a valid directory-path"
) from None

See Also:
- :py:class:`pathlib.Path`
"""
else:

def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)
if not self.is_dir():
raise DirectoryPathError(f"{self} is not a valid directory-path") from None
def __init__(self, *pathsegments: str):
super().__init__()
if not self.is_dir():
raise DirectoryPathError(
f"{self} is not a valid directory-path"
) from None

0 comments on commit 5af3d14

Please sign in to comment.