Skip to content
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

Fix import path of inherited classmethod not resolving correctly #548

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Fixed
- ``dump`` failing when a link target requires serialization and
``skip_link_targets=False`` (`#542
<https://github.com/omni-us/jsonargparse/pull/542>`__).
- Import path of inherited classmethod not resolving correctly (`lightning#19863
comment
<https://github.com/Lightning-AI/pytorch-lightning/discussions/19863#discussioncomment-10010226>`__).


v4.31.0 (2024-06-27)
Expand Down
10 changes: 7 additions & 3 deletions jsonargparse/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,12 @@ def get_import_path(value: Any) -> Optional[str]:
"""Returns the shortest dot import path for the given object."""
path = None
value = get_generic_origin(value)
module_path = getattr(value, "__module__", None)
qualname = getattr(value, "__qualname__", "")
if hasattr(value, "__self__") and inspect.isclass(value.__self__) and inspect.ismethod(value):
module_path = getattr(value.__self__, "__module__", None)
qualname = f"{value.__self__.__name__}.{value.__name__}"
else:
module_path = getattr(value, "__module__", None)
qualname = getattr(value, "__qualname__", "")

if module_path is None:
path = unresolvable_import_paths.get(value)
Expand All @@ -244,7 +248,7 @@ def get_import_path(value: Any) -> Optional[str]:
if getattr(module, attr, None) is value:
path = module_path + "." + attr
break
elif getattr(obj, attr, None) is value:
elif getattr(obj, attr, None) == value:
path = module_path + "." + qualname
break
elif getattr(module, qualname, None) is value:
Expand Down
17 changes: 17 additions & 0 deletions jsonargparse_tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,23 @@ def test_get_import_path_static_method_shorthand():
assert get_import_path(static_method) == f"{__name__}.static_method"


class ParentClassmethod:
__module__ = "jsonargparse_tests"

@classmethod
def class_method(cls):
pass


class ChildClassmethod(ParentClassmethod):
pass


def test_get_import_path_classpath_inheritance():
assert get_import_path(ParentClassmethod.class_method) == "jsonargparse_tests.ParentClassmethod.class_method"
assert get_import_path(ChildClassmethod.class_method) == f"{__name__}.ChildClassmethod.class_method"


def unresolvable_import():
pass

Expand Down