Skip to content

Commit 960098b

Browse files
committed
MOD: Change path validation to expand user dir
1 parent 49664bc commit 960098b

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This release adds support for Python 3.14.
66

77
#### Enhancements
88
- Added support for Python 3.14
9+
- Functions which accept a path as an argument now expand user directories
910

1011
## 0.67.0 - 2025-12-02
1112

databento/common/dbnstore.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from databento.common.validation import validate_enum
6161
from databento.common.validation import validate_file_write_path
6262
from databento.common.validation import validate_maybe_enum
63+
from databento.common.validation import validate_path
6364

6465

6566
logger = logging.getLogger(__name__)
@@ -138,15 +139,15 @@ class FileDataSource(DataSource):
138139
The name of the file.
139140
nbytes : int
140141
The size of the data in bytes; equal to the file size.
141-
path : PathLike[str] or str
142+
path : Path
142143
The path of the file.
143144
reader : IO[bytes]
144145
A `BufferedReader` for this file-backed data.
145146
146147
"""
147148

148-
def __init__(self, source: PathLike[str] | str):
149-
self._path = Path(source)
149+
def __init__(self, source: Path):
150+
self._path = source
150151

151152
if not self._path.is_file() or not self._path.exists():
152153
raise FileNotFoundError(source)
@@ -653,7 +654,7 @@ def from_file(cls, path: PathLike[str] | str) -> DBNStore:
653654
If an empty file is specified.
654655
655656
"""
656-
return cls(FileDataSource(path))
657+
return cls(FileDataSource(validate_path(path, "path")))
657658

658659
@classmethod
659660
def from_bytes(cls, data: BytesIO | bytes | IO[bytes]) -> DBNStore:

databento/common/symbology.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from databento_dbn import SymbolMappingMsgV1
2424

2525
from databento.common.parsing import datetime_to_unix_nanoseconds
26+
from databento.common.validation import validate_path
2627

2728

2829
class MappingInterval(NamedTuple):
@@ -49,15 +50,15 @@ def _validate_path_pair(
4950
in_file: PathLike[str] | str,
5051
out_file: PathLike[str] | str | None,
5152
) -> tuple[Path, Path]:
52-
in_file_valid = Path(in_file)
53+
in_file_valid = validate_path(in_file, "in_file")
5354

5455
if not in_file_valid.exists():
5556
raise ValueError(f"{in_file_valid} does not exist")
5657
if not in_file_valid.is_file():
5758
raise ValueError(f"{in_file_valid} is not a file")
5859

5960
if out_file is not None:
60-
out_file_valid = Path(out_file)
61+
out_file_valid = validate_path(out_file, "out_file")
6162
else:
6263
out_file_valid = in_file_valid.with_name(
6364
f"{in_file_valid.stem}_mapped{in_file_valid.suffix}",

databento/common/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime as dt
22
import logging
3-
import pathlib
43
import warnings
54
from collections.abc import Callable
65
from os import PathLike
@@ -13,6 +12,7 @@
1312
import pandas as pd
1413

1514
from databento.common.error import BentoWarning
15+
from databento.common.validation import validate_file_write_path
1616

1717

1818
logger = logging.getLogger(__name__)
@@ -110,7 +110,7 @@ def __init__(
110110
is_managed = False
111111

112112
if isinstance(stream, (str, PathLike)):
113-
stream = pathlib.Path(stream).open("xb")
113+
stream = validate_file_write_path(stream, "stream", False).open("xb")
114114
is_managed = True
115115

116116
if not hasattr(stream, "write"):

databento/common/validation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
def validate_path(value: PathLike[str] | str, param: str) -> Path:
2222
"""
23-
Validate whether the given value is a valid path.
23+
Validate whether the given value is a valid path. This also expands user
24+
directories to form valid paths.
2425
2526
Parameters
2627
----------
@@ -38,10 +39,12 @@ def validate_path(value: PathLike[str] | str, param: str) -> Path:
3839
------
3940
TypeError
4041
If value is not a valid path.
42+
RuntimeError
43+
If a user's home directory cannot be expanded.
4144
4245
"""
4346
try:
44-
return Path(value)
47+
return Path(value).expanduser()
4548
except TypeError:
4649
raise TypeError(
4750
f"The `{param}` was not a valid path type. " "Use any of [PathLike[str], str].",
@@ -72,6 +75,10 @@ def validate_file_write_path(
7275
7376
Raises
7477
------
78+
TypeError
79+
If value is not a valid path.
80+
RuntimeError
81+
If a user's home directory cannot be expanded.
7582
IsADirectoryError
7683
If path is a directory.
7784
FileExistsError

0 commit comments

Comments
 (0)