Skip to content

Commit 58d3f4d

Browse files
committed
revert refactor
1 parent 405df4a commit 58d3f4d

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/lightning/pytorch/utilities/model_helpers.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from lightning_utilities.core.imports import RequirementCache
2121
from torch import nn
22-
from typing_extensions import Concatenate, ParamSpec
22+
from typing_extensions import Concatenate, ParamSpec, override
2323

2424
import lightning.pytorch as pl
2525

@@ -104,23 +104,30 @@ def _check_mixed_imports(instance: object) -> None:
104104
_R_co = TypeVar("_R_co", covariant=True) # return type of the decorated method
105105

106106

107-
def _restricted_classmethod_impl(method: Callable[Concatenate[type[_T], _P], _R_co]) -> classmethod:
107+
class _restricted_classmethod_impl(classmethod):
108108
"""Drop-in replacement for @classmethod, but raises an exception when the decorated method is called on an instance
109109
instead of a class type."""
110110

111-
# The wrapper ensures that the method can be inspected, but not called on an instance
112-
@functools.wraps(method)
113-
def wrapper(cls: type[_T], *args: _P.args, **kwargs: _P.kwargs) -> _R_co:
114-
# Workaround for https://github.com/pytorch/pytorch/issues/67146
115-
is_scripting = any(os.path.join("torch", "jit") in frameinfo.filename for frameinfo in inspect.stack())
116-
if inspect.isclass(cls) and not is_scripting:
117-
raise TypeError(
118-
f"The classmethod `{cls.__name__}.{method.__name__}` cannot be called on an instance."
119-
" Please call it on the class type and make sure the return value is used."
120-
)
121-
return method(cls, *args, **kwargs)
122-
123-
return classmethod(wrapper)
111+
def __init__(self, method: Callable[Concatenate[type[_T], _P], _R_co]) -> None:
112+
super().__init__(method)
113+
self.method = method
114+
115+
@override
116+
def __get__(self, instance: Optional[_T], cls: Optional[type[_T]] = None) -> Callable[_P, _R_co]:
117+
# The wrapper ensures that the method can be inspected, but not called on an instance
118+
@functools.wraps(self.method)
119+
def wrapper(*args: Any, **kwargs: Any) -> _R_co:
120+
# Workaround for https://github.com/pytorch/pytorch/issues/67146
121+
is_scripting = any(os.path.join("torch", "jit") in frameinfo.filename for frameinfo in inspect.stack())
122+
cls_type = cls if cls is not None else type(instance)
123+
if instance is not None and not is_scripting:
124+
raise TypeError(
125+
f"The classmethod `{cls_type.__name__}.{self.method.__name__}` cannot be called on an instance."
126+
" Please call it on the class type and make sure the return value is used."
127+
)
128+
return self.method(cls_type, *args, **kwargs)
129+
130+
return wrapper
124131

125132

126133
if TYPE_CHECKING:

0 commit comments

Comments
 (0)