Skip to content

Commit

Permalink
Remove deprecated pkg_resources (#20081)
Browse files Browse the repository at this point in the history
  • Loading branch information
awaelchli authored Jul 15, 2024
1 parent 5822f51 commit 2dc9c3d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
29 changes: 11 additions & 18 deletions .actions/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from itertools import chain
from os.path import dirname, isfile
from pathlib import Path
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple

from pkg_resources import Requirement, parse_requirements, yield_lines
from packaging.requirements import Requirement

REQUIREMENT_FILES = {
"pytorch": (
Expand Down Expand Up @@ -80,14 +80,15 @@ def adjust(self, unfreeze: str) -> str:
out = str(self)
if self.strict:
return f"{out} {self.strict_string}"
specs = [(spec.operator, spec.version) for spec in self.specifier]
if unfreeze == "major":
for operator, version in self.specs:
for operator, version in specs:
if operator in ("<", "<="):
major = LooseVersion(version).version[0]
# replace upper bound with major version increased by one
return out.replace(f"{operator}{version}", f"<{major + 1}.0")
elif unfreeze == "all":
for operator, version in self.specs:
for operator, version in specs:
if operator in ("<", "<="):
# drop upper bound
return out.replace(f"{operator}{version},", "")
Expand All @@ -96,33 +97,25 @@ def adjust(self, unfreeze: str) -> str:
return out


def _parse_requirements(strs: Union[str, Iterable[str]]) -> Iterator[_RequirementWithComment]:
def _parse_requirements(lines: Iterable[str]) -> Iterator[_RequirementWithComment]:
"""Adapted from `pkg_resources.parse_requirements` to include comments.
>>> txt = ['# ignored', '', 'this # is an', '--piparg', 'example', 'foo # strict', 'thing', '-r different/file.txt']
>>> [r.adjust('none') for r in _parse_requirements(txt)]
['this', 'example', 'foo # strict', 'thing']
>>> txt = '\\n'.join(txt)
>>> [r.adjust('none') for r in _parse_requirements(txt)]
['this', 'example', 'foo # strict', 'thing']
"""
lines = yield_lines(strs)
pip_argument = None
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
# Drop comments -- a hash without a space may be in a URL.
if " #" in line:
comment_pos = line.find(" #")
line, comment = line[:comment_pos], line[comment_pos:]
else:
comment = ""
# If there is a line continuation, drop it, and append the next line.
if line.endswith("\\"):
line = line[:-2].strip()
try:
line += next(lines)
except StopIteration:
return
# If there's a pip argument, save it
if line.startswith("--"):
pip_argument = line
Expand All @@ -148,7 +141,7 @@ def load_requirements(path_dir: str, file_name: str = "base.txt", unfreeze: str
logging.warning(f"Folder {path_dir} does not have any base requirements.")
return []
assert path.exists(), (path_dir, file_name, path)
text = path.read_text()
text = path.read_text().splitlines()
return [req.adjust(unfreeze) for req in _parse_requirements(text)]


Expand Down Expand Up @@ -368,7 +361,7 @@ def _prune_packages(req_file: str, packages: Sequence[str]) -> None:
if not ln_ or ln_.startswith("#"):
final.append(line)
continue
req = list(parse_requirements(ln_))[0]
req = list(_parse_requirements([ln_]))[0]
if req.name not in packages:
final.append(line)
print(final)
Expand Down
1 change: 1 addition & 0 deletions .actions/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonargparse >=4.16.0, <4.28.0
requests
packaging
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
requires = [
"setuptools",
"wheel",
"packaging",
]


Expand Down
4 changes: 2 additions & 2 deletions src/lightning/__setup__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from types import ModuleType
from typing import Any, Dict

from setuptools import find_packages
from setuptools import find_namespace_packages

_PROJECT_ROOT = "."
_SOURCE_ROOT = os.path.join(_PROJECT_ROOT, "src")
Expand Down Expand Up @@ -87,7 +87,7 @@ def _setup_args() -> Dict[str, Any]:
"url": about.__homepage__,
"download_url": "https://github.com/Lightning-AI/lightning",
"license": about.__license__,
"packages": find_packages(where="src", include=["lightning", "lightning.*"]),
"packages": find_namespace_packages(where="src", include=["lightning", "lightning.*"]),
"package_dir": {"": "src"},
"long_description": long_description,
"long_description_content_type": "text/markdown",
Expand Down
3 changes: 0 additions & 3 deletions src/lightning/fabric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

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

# for compatibility with namespace packages
__import__("pkg_resources").declare_namespace(__name__)


if os.environ.get("POSSIBLE_USER_WARNINGS", "").lower() in ("0", "off"):
disable_possible_user_warnings()
2 changes: 0 additions & 2 deletions src/lightning/pytorch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

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

# for compatibility with namespace packages
__import__("pkg_resources").declare_namespace(__name__)

LIGHTNING_LOGO: str = """
####
Expand Down

0 comments on commit 2dc9c3d

Please sign in to comment.