Skip to content

Commit 75fb60c

Browse files
committed
core: deprecate PathIsh = Path | str alias
since python3.12 can just use Path | str syntax directly, much cleaner
1 parent a1269c9 commit 75fb60c

16 files changed

Lines changed: 47 additions & 53 deletions

File tree

doc/MODULES.org

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Some explanations:
4141

4242
- =MY_CONFIG= is the path where you are keeping your private configuration (usually =~/.config/my/=)
4343
- [[https://docs.python.org/3/library/pathlib.html#pathlib.Path][Path]] is a standard Python object to represent paths
44-
- [[https://github.com/karlicoss/HPI/blob/5f4acfddeeeba18237e8b039c8f62bcaa62a4ac2/my/core/common.py#L9][PathIsh]] is a helper type to allow using either =str=, or a =Path=
4544
- [[https://github.com/karlicoss/HPI/blob/5f4acfddeeeba18237e8b039c8f62bcaa62a4ac2/my/core/common.py#L108][Paths]] is another helper type for paths.
4645

4746
It's 'smart', allows you to be flexible about your config:
@@ -344,7 +343,7 @@ for cls, p in modules:
344343
'''
345344
Polar config is optional, you only need it if you want to specify custom 'polar_dir'
346345
'''
347-
polar_dir: PathIsh = Path('~/.polar').expanduser()
346+
polar_dir: Path | str = Path('~/.polar').expanduser()
348347
defensive: bool = True # pass False if you want it to fail faster on errors (useful for debugging)
349348
#+end_src
350349
** [[file:../src/my/instapaper.py][my.instapaper]]
@@ -365,7 +364,7 @@ for cls, p in modules:
365364

366365
#+begin_src python
367366
class github:
368-
gdpr_dir: PathIsh # path to unpacked GDPR archive
367+
gdpr_dir: Path | str # path to unpacked GDPR archive
369368
#+end_src
370369
** [[file:../src/my/github/ghexport.py][my.github.ghexport]]
371370

@@ -381,7 +380,7 @@ for cls, p in modules:
381380

382381
# path to a cache directory
383382
# if omitted, will use /tmp
384-
cache_dir: Optional[PathIsh] = None
383+
cache_dir: Optional[Path | str] = None
385384
#+end_src
386385
** [[file:../src/my/kobo.py][my.kobo]]
387386

src/my/arbtt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import ijson # type: ignore[import-untyped]
1818

19-
from my.core import Json, PathIsh, Stats, datetime_aware, get_files, stat
19+
from my.core import Json, Stats, datetime_aware, get_files, stat
2020

2121

2222
def inputs() -> Sequence[Path]:
@@ -78,9 +78,9 @@ def active(self) -> str | None:
7878
def entries() -> Iterable[Entry]:
7979
inps = list(inputs())
8080

81-
base: list[PathIsh] = ['arbtt-dump', '--format=json']
81+
base: list[Path | str] = ['arbtt-dump', '--format=json']
8282

83-
cmds: list[list[PathIsh]]
83+
cmds: list[list[Path | str]]
8484
if len(inps) == 0:
8585
cmds = [base] # rely on default
8686
else:

src/my/coding/commits.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from subprocess import check_output
1717
from typing import cast
1818

19-
from my.core import PathIsh, make_config, make_logger
19+
from my.core import make_config, make_logger
2020
from my.core.cachew import cache_dir, mcachew
2121
from my.core.warnings import high
2222

@@ -25,7 +25,7 @@
2525

2626
@dataclass
2727
class commits_cfg(user_config):
28-
roots: Sequence[PathIsh] = field(default_factory=list)
28+
roots: Sequence[Path | str] = field(default_factory=list)
2929
emails: Sequence[str] | None = None
3030
names: Sequence[str] | None = None
3131

@@ -90,7 +90,7 @@ def fix_datetime(dt: datetime) -> datetime:
9090
return dt.replace(tzinfo=ntz)
9191

9292

93-
def _git_root(git_dir: PathIsh) -> Path:
93+
def _git_root(git_dir: Path | str) -> Path:
9494
gd = Path(git_dir)
9595
if gd.name == '.git':
9696
return gd.parent
@@ -108,7 +108,7 @@ def _repo_commits_aux(gr: git.Repo, rev: str, emitted: set[str]) -> Iterator[Com
108108
continue
109109
emitted.add(sha)
110110

111-
# todo figure out how to handle Union[str, PathLike[Any]].. should it be part of PathIsh?
111+
# todo figure out how to handle Union[str, PathLike[Any]].. should it be part of Path | str?
112112
repo = str(_git_root(gr.git_dir)) # type: ignore[arg-type] # ty: ignore[invalid-argument-type]
113113

114114
yield Commit(
@@ -123,7 +123,7 @@ def _repo_commits_aux(gr: git.Repo, rev: str, emitted: set[str]) -> Iterator[Com
123123
)
124124

125125

126-
def repo_commits(repo: PathIsh):
126+
def repo_commits(repo: Path | str):
127127
gr = git.Repo(str(repo))
128128
emitted: set[str] = set()
129129
for r in gr.references:

src/my/core/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# this file only keeps the most common & critical types/utility functions
22

33
from .cfg import make_config
4-
from .common import PathIsh, Paths, get_files
4+
from .common import Paths, get_files
55
from .error import Res, notnone, unwrap
66
from .logging import make_logger
77
from .stats import Stats, stat
@@ -17,7 +17,6 @@
1717
'__NOT_HPI_MODULE__',
1818
'Json',
1919
'Path',
20-
'PathIsh',
2120
'Paths',
2221
'Res',
2322
'Stats',
@@ -72,10 +71,14 @@ def assert_never(*args, **kwargs):
7271

7372
del deprecated
7473

75-
__all__ += [
76-
'LazyLogger',
77-
'assert_never',
78-
]
74+
# just use Path | str directly instead
75+
from .common import PathIsh
76+
77+
__all__ += [
78+
'LazyLogger',
79+
'PathIsh',
80+
'assert_never',
81+
]
7982

8083

8184
if not TYPE_CHECKING:

src/my/core/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def config_create() -> None:
128128
# hpi doctor my.reddit.rexport
129129
130130
### useful default imports
131-
from my.core import Paths, PathIsh, get_files
131+
from my.core import Paths, get_files
132132
###
133133
134134
# most of your configs will look like this:

src/my/core/_deprecated/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from pathlib import Path
12
from typing import Any
23

3-
from ..common import PathIsh
44
from ..sqlite import sqlite_connect_immutable
55

66

7-
def connect_readonly(db: PathIsh) -> Any:
7+
def connect_readonly(db: Path | str) -> Any:
88
import dataset # type: ignore[import-not-found]
99

1010
# see https://github.com/pudo/dataset/issues/136#issuecomment-128693122

src/my/core/cachew.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
from . import warnings
1717

18-
type PathIsh = str | Path # avoid circular import from .common
19-
2018

2119
def disable_cachew() -> None:
2220
try:
@@ -53,7 +51,7 @@ def _hpi_cache_dir() -> Path:
5351
_CACHE_DIR_NONE_HACK = Path('/tmp/hpi/cachew_none_hack')
5452

5553

56-
def cache_dir(suffix: PathIsh | None = None) -> Path:
54+
def cache_dir(suffix: Path | str | None = None) -> Path:
5755
from . import core_config as CC
5856

5957
cdir_ = CC.config.get_cache_dir()
@@ -119,7 +117,7 @@ def _mcachew_impl(cache_path=_cache_path_dflt, **kwargs):
119117

120118

121119
if TYPE_CHECKING:
122-
type PathProvider[**P] = PathIsh | Callable[P, PathIsh]
120+
type PathProvider[**P] = Path | str | Callable[P, Path | str]
123121
# NOTE: in cachew, HashFunction type returns str
124122
# however in practice, cachew always calls str for its result
125123
# so perhaps better to switch it to Any in cachew as well

src/my/core/common.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99

1010
from . import compat, warnings
1111

12-
# some helper functions
13-
# TODO start deprecating this? soon we'd be able to use Path | str syntax which is shorter and more explicit
14-
PathIsh = Path | str
15-
16-
Paths = Sequence[PathIsh] | PathIsh
12+
Paths = Sequence[Path | str] | Path | str
1713

1814

1915
DEFAULT_GLOB = '*'
@@ -238,6 +234,9 @@ def asdict(*args, **kwargs):
238234
)
239235

240236
tzdatetime = datetime_aware
237+
238+
# should just use union directly
239+
PathIsh = Path | str
241240
else:
242241
from typing import Never
243242

src/my/core/sqlite.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
from typing import Any, Literal, assert_never, overload
1010

1111
from . import warnings
12-
from .common import PathIsh
1312

1413

15-
def sqlite_connect_immutable(db: PathIsh) -> sqlite3.Connection:
14+
def sqlite_connect_immutable(db: Path | str) -> sqlite3.Connection:
1615
return sqlite3.connect(f'file:{db}?immutable=1', uri=True)
1716

1817

@@ -45,7 +44,7 @@ def dict_factory(cursor, row):
4544

4645
@contextmanager
4746
def sqlite_connection(
48-
db: PathIsh,
47+
db: Path | str,
4948
*,
5049
immutable: bool = False,
5150
row_factory: Factory | None = None,
@@ -97,7 +96,7 @@ def sqlite_connection(
9796

9897
# TODO come up with a better name?
9998
# NOTE: this is tested by tests/sqlite.py::test_sqlite_read_with_wal
100-
def sqlite_copy_and_open(db: PathIsh) -> sqlite3.Connection:
99+
def sqlite_copy_and_open(db: Path | str) -> sqlite3.Connection:
101100
"""
102101
'Snapshots' database and opens by making a deep copy of it including journal/WAL files
103102
"""

src/my/demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pathlib import Path
1212
from typing import Protocol
1313

14-
from my.core import Json, PathIsh, Paths, get_files
14+
from my.core import Json, Paths, get_files
1515

1616

1717
class config(Protocol):
@@ -23,7 +23,7 @@ class config(Protocol):
2323
# this is to check optional attribute handling
2424
timezone: tzinfo = UTC
2525

26-
external: PathIsh | None = None
26+
external: Path | str | None = None
2727

2828
@property
2929
def external_module(self):

0 commit comments

Comments
 (0)