Repository: pyg-team/pytorch_geometric
Pull request #9461 (2024-06-26 14:34:33)

Fix `ConfigMixin.from_config` when config is of type `dict`

akihironitta: No need for a changelog entry as `ConfigMixin` hasn't been included in any release yet.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pyg-team/pytorch_geometric/pull/9461?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 86.89%. Comparing base [(`dafbd30`)](https://app.codecov.io/gh/pyg-team/pytorch_geometric/commit/dafbd3013b19737ac1511d16d22d6529786a63c4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) to head [(`c9cee9b`)](https://app.codecov.io/gh/pyg-team/pytorch_geometric/commit/c9cee9b93e498d2965333af43ebf0219d8d7f40e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team).
> Report is 26 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #9461      +/-   ##
==========================================
- Coverage   88.25%   86.89%   -1.37%     
==========================================
  Files         473      475       +2     
  Lines       30844    30981     +137     
==========================================
- Hits        27221    26920     -301     
- Misses       3623     4061     +438     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pyg-team/pytorch_geometric/pull/9461?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team).


Patch:
diff --git a/torch_geometric/config_mixin.py b/torch_geometric/config_mixin.py
index 6dbe5c430..0cb89fab8 100644
--- a/torch_geometric/config_mixin.py
+++ b/torch_geometric/config_mixin.py
@@ -33,7 +33,7 @@ class ConfigMixin:
         if getattr(cfg, '_target_', None):
             cls = _locate_cls(cfg._target_)
         elif isinstance(cfg, dict) and '_target_' in cfg:
-            cls = _locate_cls(cfg['target'])
+            cls = _locate_cls(cfg['_target_'])
 
         data_cls = cfg.__class__
         if not is_dataclass(data_cls):


Test patch:
diff --git a/test/test_config_mixin.py b/test/test_config_mixin.py
index fbff499fd..1fb053d96 100644
--- a/test/test_config_mixin.py
+++ b/test/test_config_mixin.py
@@ -1,4 +1,4 @@
-from dataclasses import dataclass, is_dataclass
+from dataclasses import asdict, dataclass, is_dataclass
 
 import torch
 
@@ -68,3 +68,19 @@ def test_config_mixin() -> None:
     assert isinstance(model.data, Dataclass)
     assert model.data.x == 2
     assert model.data.y == 3
+
+    cfg = asdict(cfg)
+
+    model = Module.from_config(cfg)
+    assert isinstance(model, Module)
+    assert model.x == 0
+    assert isinstance(model.data, dict)
+    assert model.data['x'] == 1
+    assert model.data['y'] == 2
+
+    model = Base.from_config(cfg)
+    assert isinstance(model, Module)
+    assert model.x == 0
+    assert isinstance(model.data, dict)
+    assert model.data['x'] == 1
+    assert model.data['y'] == 2