Skip to content

Improved the jsonargparse[signatures] availability variable #13035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pytorch_lightning/utilities/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
from pytorch_lightning import Callback, LightningDataModule, LightningModule, seed_everything, Trainer
from pytorch_lightning.utilities.cloud_io import get_filesystem
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _RequirementAvaliable
from pytorch_lightning.utilities.imports import _RequirementAvailable
from pytorch_lightning.utilities.meta import get_all_subclasses
from pytorch_lightning.utilities.model_helpers import is_overridden
from pytorch_lightning.utilities.rank_zero import _warn, rank_zero_deprecation, rank_zero_warn
from pytorch_lightning.utilities.seed import _select_seed_randomly

_JSONARGPARSE_SIGNATURES_AVAILABLE = _RequirementAvaliable("jsonargparse[signatures]>=4.7.1")
_JSONARGPARSE_SIGNATURES_AVAILABLE = _RequirementAvailable("jsonargparse[signatures]>=4.7.1")

if _JSONARGPARSE_SIGNATURES_AVAILABLE:
import docstring_parser
Expand Down
42 changes: 31 additions & 11 deletions pytorch_lightning/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,44 @@ def _compare_version(package: str, op: Callable, version: str, use_base_version:
return op(pkg_version, Version(version))


class _RequirementAvaliable:
"""Boolean-like class for check of requirement with extras and version specifiers."""

def __init__(self, requirement: str):
self.available = True
self.message = ""
try:
pkg_resources.require([requirement])
except Exception as ex:
self.available = False
self.message = f"Requirement '{requirement}' not met, {ex.__class__.__name__}: {ex}"
class _RequirementAvailable:
"""Boolean-like class for check of requirement with extras and version specifiers.

>>> _RequirementAvailable("jsonargparse[signatures]>=4.7.3")
Requirement 'jsonargparse[signatures]>=4.7.3' not met, VersionConflict: (jsonargparse 4.7.2 ..., Requirement.parse('jsonargparse[signatures]>=4.7.3'))
>>> bool(_RequirementAvailable("jsonargparse[signatures]>=4.7.3"))
False
>>> bool(_RequirementAvailable("jsonargparse[signatures]>=4.7.2"))
True
"""

def __init__(self, requirement: str) -> None:
self.available = None
self.requirement = requirement
self.__repr__ = self.__str__

def _check_requirement(self) -> None:
if self.available is None:
try:
pkg_resources.require([self.requirement])
except Exception as ex:
self.available = False
self.message = f"Requirement {self.requirement!r} not met, {ex.__class__.__name__}: {ex}"
else:
self.available = True
self.message = f"Requirement {self.requirement!r} met"

def __bool__(self) -> bool:
self._check_requirement()
return self.available

def __str__(self) -> str:
self._check_requirement()
return self.message

def __repr__(self) -> str:
return self.__str__()


_IS_WINDOWS = platform.system() == "Windows"
_IS_INTERACTIVE = hasattr(sys, "ps1") # https://stackoverflow.com/a/64523765
Expand Down
8 changes: 4 additions & 4 deletions tests/utilities/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
_OMEGACONF_AVAILABLE,
_POPTORCH_AVAILABLE,
)
from pytorch_lightning.utilities.imports import _compare_version, _RequirementAvaliable, torch
from pytorch_lightning.utilities.imports import _compare_version, _RequirementAvailable, torch


def test_module_exists():
Expand Down Expand Up @@ -55,9 +55,9 @@ def test_compare_version(monkeypatch):


def test_requirement_avaliable():
assert _RequirementAvaliable(f"torch>={torch.__version__}")
assert not _RequirementAvaliable(f"torch<{torch.__version__}")
assert "Requirement '-' not met" in str(_RequirementAvaliable("-"))
assert _RequirementAvailable(f"torch>={torch.__version__}")
assert not _RequirementAvailable(f"torch<{torch.__version__}")
assert "Requirement '-' not met" in str(_RequirementAvailable("-"))


def test_imports():
Expand Down