Created
February 7, 2021 22:23
-
-
Save mawillcockson/ea18ae66aa15075ddc24d11ddf91d298 to your computer and use it in GitHub Desktop.
[Python] subclass pathlib.Path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| example of subclassing pathlib.Path | |
| the current downside is repr() shows a PosixPath or WindowsPath | |
| mypy --strict is satisfied by the call to cast(), and as long as the __new__() | |
| method returns an object of type pathlib.Path, everything else should work, | |
| since the created object will be the same as if it were created with pathlib.Path | |
| mypy will complain, as it does not like the Path type on the last line | |
| this solution is the same as: | |
| https://stackoverflow.com/a/29880095 | |
| """ | |
| from pathlib import Path | |
| from typing import Union, cast | |
| PathType = Union[str, Path] | |
| class ExistingPath(Path): | |
| "a Path that is guaranteed to exist" | |
| def __new__(cls, value: PathType) -> "ExistingPath": | |
| # pylint: disable=arguments-differ | |
| """ | |
| create a new instance of ExistingPath | |
| overrides Path.__new__() and applies stricter requirements | |
| this function should return a value of type Path | |
| """ | |
| if not value: | |
| raise ValueError("'{value}' must be a path") | |
| path = Path(value).resolve() | |
| if not path.exists(): | |
| raise ValueError(f"'{path}' does not exist") | |
| return cast(ExistingPath, path) | |
| existing_path = ExistingPath("./") | |
| normal_path = Path("./").resolve(strict=True) | |
| TEST_FILE_NAME = "test.txt" | |
| existing_test_file = existing_path / TEST_FILE_NAME | |
| normal_test_file = normal_path / TEST_FILE_NAME | |
| assert existing_test_file == normal_test_file | |
| assert isinstance(existing_test_file, Path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment