Skip to content

Commit 5486d2a

Browse files
Merge pull request #121 from r1chardj0n3s/pathlib
Switch a few `str` representations of paths to `pathlib.Path`
2 parents 3eeae27 + ac0b59b commit 5486d2a

File tree

6 files changed

+38
-37
lines changed

6 files changed

+38
-37
lines changed

pip_check_reqs/common.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,19 @@ def finalise(self) -> Dict[str, FoundModule]:
124124
return result
125125

126126

127-
def pyfiles(root: str) -> Generator[str, None, None]:
128-
root_path = Path(root)
129-
if root_path.is_file():
130-
if root_path.suffix == ".py":
131-
yield str(root_path.absolute())
127+
def pyfiles(root: Path) -> Generator[str, None, None]:
128+
if root.is_file():
129+
if root.suffix == ".py":
130+
yield str(root.absolute())
132131
else:
133-
raise ValueError(f"{root_path} is not a python file or directory")
134-
elif root_path.is_dir():
135-
for item in root_path.rglob("*.py"):
132+
raise ValueError(f"{root} is not a python file or directory")
133+
elif root.is_dir():
134+
for item in root.rglob("*.py"):
136135
yield str(item.absolute())
137136

138137

139138
def find_imported_modules(
140-
paths: Iterable[str],
139+
paths: Iterable[Path],
141140
ignore_files_function: Callable[[str], bool],
142141
ignore_modules_function: Callable[[str], bool],
143142
) -> Dict[str, FoundModule]:
@@ -160,11 +159,11 @@ def find_required_modules(
160159
[Union[str, ParsedRequirement]], bool
161160
],
162161
skip_incompatible: bool,
163-
requirements_filename: str,
162+
requirements_filename: Path,
164163
) -> Set[NormalizedName]:
165164
explicit = set()
166165
for requirement in parse_requirements(
167-
requirements_filename, session=PipSession()
166+
str(requirements_filename), session=PipSession()
168167
):
169168
requirement_name = install_req_from_line(
170169
requirement.requirement,

pip_check_reqs/find_extra_reqs.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import importlib.metadata
66
import logging
77
import os
8-
import pathlib
98
import sys
9+
from pathlib import Path
1010
from typing import Callable, Iterable, List, Optional, Union
1111

1212
from packaging.utils import canonicalize_name
@@ -20,8 +20,8 @@
2020

2121

2222
def find_extra_reqs(
23-
requirements_filename: str,
24-
paths: Iterable[str],
23+
requirements_filename: Path,
24+
paths: Iterable[Path],
2525
ignore_files_function: Callable[[str], bool],
2626
ignore_modules_function: Callable[[str], bool],
2727
ignore_requirements_function: Callable[
@@ -48,8 +48,8 @@ def find_extra_reqs(
4848
package_location = package.location
4949
package_files = []
5050
for item in package.files or []:
51-
here = pathlib.Path(".").resolve()
52-
item_location_rel = pathlib.Path(package_location) / item
51+
here = Path(".").resolve()
52+
item_location_rel = Path(package_location) / item
5353
item_location = item_location_rel.resolve()
5454
try:
5555
relative_item_location = item_location.relative_to(here)
@@ -109,12 +109,13 @@ def main(arguments: Optional[List[str]] = None) -> None:
109109
"""Main entry point."""
110110
usage = "usage: %prog [options] files or directories"
111111
parser = argparse.ArgumentParser(usage)
112-
parser.add_argument("paths", nargs="*")
112+
parser.add_argument("paths", type=Path, nargs="*")
113113
parser.add_argument(
114114
"--requirements-file",
115115
dest="requirements_filename",
116+
type=Path,
116117
metavar="PATH",
117-
default="requirements.txt",
118+
default=Path("requirements.txt"),
118119
help="path to the requirements file "
119120
'(defaults to "requirements.txt")',
120121
)

pip_check_reqs/find_missing_reqs.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import importlib.metadata
66
import logging
77
import os
8-
import pathlib
98
import sys
9+
from pathlib import Path
1010
from typing import Callable, Iterable, List, Optional, Tuple
1111

1212
from packaging.utils import NormalizedName, canonicalize_name
@@ -22,8 +22,8 @@
2222

2323

2424
def find_missing_reqs(
25-
requirements_filename: str,
26-
paths: Iterable[str],
25+
requirements_filename: Path,
26+
paths: Iterable[Path],
2727
ignore_files_function: Callable[[str], bool],
2828
ignore_modules_function: Callable[[str], bool],
2929
) -> List[Tuple[NormalizedName, List[FoundModule]]]:
@@ -46,8 +46,8 @@ def find_missing_reqs(
4646
package_location = package.location
4747
package_files = []
4848
for item in package.files or []:
49-
here = pathlib.Path(".").resolve()
50-
item_location_rel = pathlib.Path(package_location) / item
49+
here = Path(".").resolve()
50+
item_location_rel = Path(package_location) / item
5151
item_location = item_location_rel.resolve()
5252
try:
5353
relative_item_location = item_location.relative_to(here)
@@ -95,7 +95,7 @@ def find_missing_reqs(
9595
# 4. compare with requirements
9696
explicit = set()
9797
for requirement in parse_requirements(
98-
requirements_filename,
98+
str(requirements_filename),
9999
session=PipSession(),
100100
):
101101
requirement_name = install_req_from_line(
@@ -113,11 +113,12 @@ def find_missing_reqs(
113113
def main(arguments: Optional[List[str]] = None) -> None:
114114
usage = "usage: %prog [options] files or directories"
115115
parser = argparse.ArgumentParser(usage)
116-
parser.add_argument("paths", nargs="*")
116+
parser.add_argument("paths", type=Path, nargs="*")
117117
parser.add_argument(
118118
"--requirements-file",
119119
dest="requirements_filename",
120120
metavar="PATH",
121+
type=Path,
121122
default="requirements.txt",
122123
help="path to the requirements file "
123124
'(defaults to "requirements.txt")',

tests/test_common.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_is_package_file(path: str, result: str) -> None:
3636
def test_found_module() -> None:
3737
found_module = common.FoundModule("spam", "ham")
3838
assert found_module.modname == "spam"
39-
assert found_module.filename == os.path.realpath("ham")
39+
assert found_module.filename == str(Path("ham").resolve())
4040
assert found_module.locations == []
4141

4242

@@ -66,15 +66,15 @@ def test_import_visitor(stmt: str, result: List[str]) -> None:
6666
def test_pyfiles_file(tmp_path: Path) -> None:
6767
python_file = tmp_path / "example.py"
6868
python_file.touch()
69-
assert list(common.pyfiles(root=str(python_file))) == [str(python_file)]
69+
assert list(common.pyfiles(root=python_file)) == [str(python_file)]
7070

7171

7272
def test_pyfiles_file_no_dice(tmp_path: Path) -> None:
7373
not_python_file = tmp_path / "example"
7474
not_python_file.touch()
7575

7676
with pytest.raises(ValueError):
77-
list(common.pyfiles(root=str(not_python_file)))
77+
list(common.pyfiles(root=not_python_file))
7878

7979

8080
def test_pyfiles_package(tmp_path: Path) -> None:
@@ -88,7 +88,7 @@ def test_pyfiles_package(tmp_path: Path) -> None:
8888

8989
not_python_file.touch()
9090

91-
assert list(common.pyfiles(root=str(tmp_path))) == [
91+
assert list(common.pyfiles(root=tmp_path)) == [
9292
str(python_file),
9393
str(nested_python_file),
9494
]
@@ -156,7 +156,7 @@ def ignore_mods(module: str) -> bool:
156156
return False
157157

158158
result = common.find_imported_modules(
159-
paths=[str(root)],
159+
paths=[root],
160160
ignore_files_function=ignore_files,
161161
ignore_modules_function=ignore_mods,
162162
)
@@ -204,7 +204,7 @@ def test_find_required_modules(tmp_path: Path) -> None:
204204
reqs = common.find_required_modules(
205205
ignore_requirements_function=common.ignorer(ignore_cfg=["barfoo"]),
206206
skip_incompatible=False,
207-
requirements_filename=str(fake_requirements_file),
207+
requirements_filename=fake_requirements_file,
208208
)
209209
assert reqs == {"foobar"}
210210

@@ -218,7 +218,7 @@ def test_find_required_modules_env_markers(tmp_path: Path) -> None:
218218
reqs = common.find_required_modules(
219219
ignore_requirements_function=common.ignorer(ignore_cfg=[]),
220220
skip_incompatible=True,
221-
requirements_filename=str(fake_requirements_file),
221+
requirements_filename=fake_requirements_file,
222222
)
223223
assert reqs == {"ham", "eggs"}
224224

@@ -241,7 +241,7 @@ def mocked_open(*args: Any, **kwargs: Any) -> Any:
241241

242242
monkeypatch.setattr(builtins, "open", mocked_open)
243243
common.find_imported_modules(
244-
paths=[str(tmp_path)],
244+
paths=[tmp_path],
245245
ignore_files_function=common.ignorer(ignore_cfg=[]),
246246
ignore_modules_function=common.ignorer(ignore_cfg=[]),
247247
)

tests/test_find_extra_reqs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_find_extra_reqs(tmp_path: Path) -> None:
4242
)
4343

4444
result = find_extra_reqs.find_extra_reqs(
45-
requirements_filename=str(fake_requirements_file),
46-
paths=[str(source_dir)],
45+
requirements_filename=fake_requirements_file,
46+
paths=[source_dir],
4747
ignore_files_function=common.ignorer(ignore_cfg=[]),
4848
ignore_modules_function=common.ignorer(ignore_cfg=[]),
4949
ignore_requirements_function=common.ignorer(ignore_cfg=[]),

tests/test_find_missing_reqs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_find_missing_reqs(tmp_path: Path) -> None:
4343
)
4444

4545
result = find_missing_reqs.find_missing_reqs(
46-
requirements_filename=str(fake_requirements_file),
47-
paths=[str(source_dir)],
46+
requirements_filename=fake_requirements_file,
47+
paths=[source_dir],
4848
ignore_files_function=common.ignorer(ignore_cfg=[]),
4949
ignore_modules_function=common.ignorer(ignore_cfg=[]),
5050
)

0 commit comments

Comments
 (0)