Skip to content

Commit 2dc9c3d

Browse files
authored
Remove deprecated pkg_resources (#20081)
1 parent 5822f51 commit 2dc9c3d

File tree

6 files changed

+15
-25
lines changed

6 files changed

+15
-25
lines changed

.actions/assistant.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from itertools import chain
2323
from os.path import dirname, isfile
2424
from pathlib import Path
25-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Union
25+
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple
2626

27-
from pkg_resources import Requirement, parse_requirements, yield_lines
27+
from packaging.requirements import Requirement
2828

2929
REQUIREMENT_FILES = {
3030
"pytorch": (
@@ -80,14 +80,15 @@ def adjust(self, unfreeze: str) -> str:
8080
out = str(self)
8181
if self.strict:
8282
return f"{out} {self.strict_string}"
83+
specs = [(spec.operator, spec.version) for spec in self.specifier]
8384
if unfreeze == "major":
84-
for operator, version in self.specs:
85+
for operator, version in specs:
8586
if operator in ("<", "<="):
8687
major = LooseVersion(version).version[0]
8788
# replace upper bound with major version increased by one
8889
return out.replace(f"{operator}{version}", f"<{major + 1}.0")
8990
elif unfreeze == "all":
90-
for operator, version in self.specs:
91+
for operator, version in specs:
9192
if operator in ("<", "<="):
9293
# drop upper bound
9394
return out.replace(f"{operator}{version},", "")
@@ -96,33 +97,25 @@ def adjust(self, unfreeze: str) -> str:
9697
return out
9798

9899

99-
def _parse_requirements(strs: Union[str, Iterable[str]]) -> Iterator[_RequirementWithComment]:
100+
def _parse_requirements(lines: Iterable[str]) -> Iterator[_RequirementWithComment]:
100101
"""Adapted from `pkg_resources.parse_requirements` to include comments.
101102
102103
>>> txt = ['# ignored', '', 'this # is an', '--piparg', 'example', 'foo # strict', 'thing', '-r different/file.txt']
103104
>>> [r.adjust('none') for r in _parse_requirements(txt)]
104105
['this', 'example', 'foo # strict', 'thing']
105-
>>> txt = '\\n'.join(txt)
106-
>>> [r.adjust('none') for r in _parse_requirements(txt)]
107-
['this', 'example', 'foo # strict', 'thing']
108106
109107
"""
110-
lines = yield_lines(strs)
111108
pip_argument = None
112109
for line in lines:
110+
line = line.strip()
111+
if not line or line.startswith("#"):
112+
continue
113113
# Drop comments -- a hash without a space may be in a URL.
114114
if " #" in line:
115115
comment_pos = line.find(" #")
116116
line, comment = line[:comment_pos], line[comment_pos:]
117117
else:
118118
comment = ""
119-
# If there is a line continuation, drop it, and append the next line.
120-
if line.endswith("\\"):
121-
line = line[:-2].strip()
122-
try:
123-
line += next(lines)
124-
except StopIteration:
125-
return
126119
# If there's a pip argument, save it
127120
if line.startswith("--"):
128121
pip_argument = line
@@ -148,7 +141,7 @@ def load_requirements(path_dir: str, file_name: str = "base.txt", unfreeze: str
148141
logging.warning(f"Folder {path_dir} does not have any base requirements.")
149142
return []
150143
assert path.exists(), (path_dir, file_name, path)
151-
text = path.read_text()
144+
text = path.read_text().splitlines()
152145
return [req.adjust(unfreeze) for req in _parse_requirements(text)]
153146

154147

@@ -368,7 +361,7 @@ def _prune_packages(req_file: str, packages: Sequence[str]) -> None:
368361
if not ln_ or ln_.startswith("#"):
369362
final.append(line)
370363
continue
371-
req = list(parse_requirements(ln_))[0]
364+
req = list(_parse_requirements([ln_]))[0]
372365
if req.name not in packages:
373366
final.append(line)
374367
print(final)

.actions/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
jsonargparse >=4.16.0, <4.28.0
22
requests
3+
packaging

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
requires = [
1717
"setuptools",
1818
"wheel",
19+
"packaging",
1920
]
2021

2122

src/lightning/__setup__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from types import ModuleType
66
from typing import Any, Dict
77

8-
from setuptools import find_packages
8+
from setuptools import find_namespace_packages
99

1010
_PROJECT_ROOT = "."
1111
_SOURCE_ROOT = os.path.join(_PROJECT_ROOT, "src")
@@ -87,7 +87,7 @@ def _setup_args() -> Dict[str, Any]:
8787
"url": about.__homepage__,
8888
"download_url": "https://github.com/Lightning-AI/lightning",
8989
"license": about.__license__,
90-
"packages": find_packages(where="src", include=["lightning", "lightning.*"]),
90+
"packages": find_namespace_packages(where="src", include=["lightning", "lightning.*"]),
9191
"package_dir": {"": "src"},
9292
"long_description": long_description,
9393
"long_description_content_type": "text/markdown",

src/lightning/fabric/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737

3838
__all__ = ["Fabric", "seed_everything", "is_wrapped"]
3939

40-
# for compatibility with namespace packages
41-
__import__("pkg_resources").declare_namespace(__name__)
42-
4340

4441
if os.environ.get("POSSIBLE_USER_WARNINGS", "").lower() in ("0", "off"):
4542
disable_possible_user_warnings()

src/lightning/pytorch/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
__all__ = ["Trainer", "LightningDataModule", "LightningModule", "Callback", "seed_everything"]
3535

36-
# for compatibility with namespace packages
37-
__import__("pkg_resources").declare_namespace(__name__)
3836

3937
LIGHTNING_LOGO: str = """
4038
####

0 commit comments

Comments
 (0)