Skip to content

Commit 86002c0

Browse files
mauvilsacarmocca
authored andcommitted
Remove use of jsonargparse internals (#12918)
1 parent c58a509 commit 86002c0

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

pytorch_lightning/utilities/cli.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@
2929
from pytorch_lightning import Callback, LightningDataModule, LightningModule, seed_everything, Trainer
3030
from pytorch_lightning.utilities.cloud_io import get_filesystem
3131
from pytorch_lightning.utilities.exceptions import MisconfigurationException
32-
from pytorch_lightning.utilities.imports import _JSONARGPARSE_AVAILABLE
32+
from pytorch_lightning.utilities.imports import _DOCSTRING_PARSER_AVAILABLE, _JSONARGPARSE_AVAILABLE
3333
from pytorch_lightning.utilities.meta import get_all_subclasses
3434
from pytorch_lightning.utilities.model_helpers import is_overridden
3535
from pytorch_lightning.utilities.rank_zero import _warn, rank_zero_warn
3636
from pytorch_lightning.utilities.types import LRSchedulerType, LRSchedulerTypeTuple, LRSchedulerTypeUnion
3737

3838
if _JSONARGPARSE_AVAILABLE:
3939
from jsonargparse import ActionConfigFile, ArgumentParser, class_from_function, Namespace, set_config_read_mode
40-
from jsonargparse.optionals import import_docstring_parse
4140

4241
set_config_read_mode(fsspec_enabled=True)
4342
else:
4443
locals()["ArgumentParser"] = object
4544
locals()["Namespace"] = object
4645

46+
if _DOCSTRING_PARSER_AVAILABLE:
47+
import docstring_parser
48+
4749

4850
class _Registry(dict):
4951
def __call__(self, cls: Type, key: Optional[str] = None, override: bool = False) -> Type:
@@ -888,9 +890,13 @@ def instantiate_class(args: Union[Any, Tuple[Any, ...]], init: Dict[str, Any]) -
888890

889891

890892
def _get_short_description(component: object) -> Optional[str]:
891-
parse, _ = import_docstring_parse("LightningCLI(run=True)")
892-
try:
893-
docstring = parse(component.__doc__)
894-
return docstring.short_description
895-
except ValueError:
896-
rank_zero_warn(f"Failed parsing docstring for {component}")
893+
if component.__doc__ is None:
894+
return None
895+
if not _DOCSTRING_PARSER_AVAILABLE:
896+
rank_zero_warn(f"Failed parsing docstring for {component}: docstring-parser package is required")
897+
else:
898+
try:
899+
docstring = docstring_parser.parse(component.__doc__)
900+
return docstring.short_description
901+
except (ValueError, docstring_parser.ParseError) as ex:
902+
rank_zero_warn(f"Failed parsing docstring for {component}: {ex}")

pytorch_lightning/utilities/imports.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def _compare_version(package: str, op: Callable, version: str, use_base_version:
100100
_DEEPSPEED_AVAILABLE = _package_available("deepspeed")
101101
_DEEPSPEED_GREATER_EQUAL_0_5_9 = _DEEPSPEED_AVAILABLE and _compare_version("deepspeed", operator.ge, "0.5.9")
102102
_DEEPSPEED_GREATER_EQUAL_0_6 = _DEEPSPEED_AVAILABLE and _compare_version("deepspeed", operator.ge, "0.6.0")
103+
_DOCSTRING_PARSER_AVAILABLE = _package_available("docstring_parser")
103104
_FAIRSCALE_AVAILABLE = not _IS_WINDOWS and _module_available("fairscale.nn")
104105
_FAIRSCALE_OSS_FP16_BROADCAST_AVAILABLE = _FAIRSCALE_AVAILABLE and _compare_version("fairscale", operator.ge, "0.3.3")
105106
_FAIRSCALE_FULLY_SHARDED_AVAILABLE = _FAIRSCALE_AVAILABLE and _compare_version("fairscale", operator.ge, "0.3.4")

0 commit comments

Comments
 (0)