|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | | - |
15 | | -from typing import Union |
| 14 | +from functools import partial |
| 15 | +from typing import Optional, Type, Union |
| 16 | +from unittest.mock import Mock |
16 | 17 |
|
17 | 18 | from pytorch_lightning.core.datamodule import LightningDataModule |
18 | 19 | from pytorch_lightning.core.lightning import LightningModule |
| 20 | +from pytorch_lightning.utilities import rank_zero_deprecation |
19 | 21 |
|
20 | 22 |
|
21 | | -def is_overridden(method_name: str, model: Union[LightningModule, LightningDataModule]) -> bool: |
22 | | - # if you pass DataModule instead of None or a LightningModule, we use LightningDataModule as super |
23 | | - # TODO - refector this function to accept model_name, instance, parent so it makes more sense |
24 | | - super_object = LightningModule if not isinstance(model, LightningDataModule) else LightningDataModule |
| 23 | +def is_overridden( |
| 24 | + method_name: str, |
| 25 | + instance: Optional[object] = None, |
| 26 | + parent: Optional[Type[object]] = None, |
| 27 | + model: Optional[Union[LightningModule, LightningDataModule]] = None, |
| 28 | +) -> bool: |
| 29 | + if model is not None and instance is None: |
| 30 | + rank_zero_deprecation( |
| 31 | + '`is_overriden(model=...)` has been deprecated and will be removed in v1.6.' |
| 32 | + 'Please use `is_overriden(instance=...)`' |
| 33 | + ) |
| 34 | + instance = model |
25 | 35 |
|
26 | | - if not hasattr(model, method_name) or not hasattr(super_object, method_name): |
27 | | - # in case of calling deprecated method |
| 36 | + if instance is None: |
| 37 | + # if `self.lightning_module` was passed as instance, it can be `None` |
28 | 38 | return False |
29 | 39 |
|
30 | | - instance_attr = getattr(model, method_name) |
31 | | - if not instance_attr: |
| 40 | + if parent is None: |
| 41 | + if isinstance(instance, LightningModule): |
| 42 | + parent = LightningModule |
| 43 | + elif isinstance(instance, LightningDataModule): |
| 44 | + parent = LightningDataModule |
| 45 | + if parent is None: |
| 46 | + raise ValueError("Expected a parent") |
| 47 | + |
| 48 | + instance_attr = getattr(instance, method_name, None) |
| 49 | + # `Mock(wraps=...)` support |
| 50 | + if isinstance(instance_attr, Mock): |
| 51 | + # access the wrapped function |
| 52 | + instance_attr = instance_attr._mock_wraps |
| 53 | + # `partial` support |
| 54 | + elif isinstance(instance_attr, partial): |
| 55 | + instance_attr = instance_attr.func |
| 56 | + if instance_attr is None: |
32 | 57 | return False |
33 | | - super_attr = getattr(super_object, method_name) |
34 | | - |
35 | | - # when code pointers are different, it was implemented |
36 | | - if hasattr(instance_attr, 'patch_loader_code'): |
37 | | - # cannot pickle __code__ so cannot verify if PatchDataloader |
38 | | - # exists which shows dataloader methods have been overwritten. |
39 | | - # so, we hack it by using the string representation |
40 | | - is_overridden = instance_attr.patch_loader_code != str(super_attr.__code__) |
41 | | - else: |
42 | | - is_overridden = instance_attr.__code__ is not super_attr.__code__ |
43 | | - return is_overridden |
| 58 | + |
| 59 | + parent_attr = getattr(parent, method_name, None) |
| 60 | + if parent_attr is None: |
| 61 | + raise ValueError("The parent should define the method") |
| 62 | + |
| 63 | + # cannot pickle `__code__` so cannot verify if `PatchDataloader` |
| 64 | + # exists which shows dataloader methods have been overwritten. |
| 65 | + # so, we hack it by using the string representation |
| 66 | + instance_code = getattr(instance_attr, 'patch_loader_code', None) or instance_attr.__code__ |
| 67 | + parent_code = parent_attr.__code__ |
| 68 | + |
| 69 | + return instance_code != parent_code |
0 commit comments