Repository: pyg-team/pytorch_geometric
Pull request #6769 (2023-02-22 12:22:05)

Fix `ModuleDict.items()`

codecov[bot]: # [Codecov](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) Report
> Merging [#6769](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) (889b678) into [master](https://codecov.io/gh/pyg-team/pytorch_geometric/commit/ceb7af61baad10c38844930f6ba4e016c4a2afdf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) (ceb7af6) will **decrease** coverage by `1.57%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 889b678 differs from pull request most recent head 0c2a41e. Consider uploading reports for the commit 0c2a41e to get more accurate results

```diff
@@            Coverage Diff             @@
##           master    #6769      +/-   ##
==========================================
- Coverage   90.71%   89.15%   -1.57%     
==========================================
  Files         425      425              
  Lines       23150    23116      -34     
==========================================
- Hits        21001    20608     -393     
- Misses       2149     2508     +359     
```


| [Impacted Files](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) | Coverage Δ | |
|---|---|---|
| [torch\_geometric/nn/encoding.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL2VuY29kaW5nLnB5) | `100.00% <100.00%> (ø)` | |
| [torch\_geometric/nn/module\_dict.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL21vZHVsZV9kaWN0LnB5) | `100.00% <100.00%> (ø)` | |
| [torch\_geometric/nn/models/dimenet\_utils.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL21vZGVscy9kaW1lbmV0X3V0aWxzLnB5) | `0.00% <0.00%> (-75.52%)` | :arrow_down: |
| [torch\_geometric/nn/models/dimenet.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL21vZGVscy9kaW1lbmV0LnB5) | `17.66% <0.00%> (-49.78%)` | :arrow_down: |
| [torch\_geometric/visualization/graph.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL3Zpc3VhbGl6YXRpb24vZ3JhcGgucHk=) | `68.25% <0.00%> (-26.99%)` | :arrow_down: |
| [torch\_geometric/nn/conv/utils/typing.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL2NvbnYvdXRpbHMvdHlwaW5nLnB5) | `83.75% <0.00%> (-15.00%)` | :arrow_down: |
| [torch\_geometric/nn/pool/asap.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL3Bvb2wvYXNhcC5weQ==) | `91.89% <0.00%> (-8.11%)` | :arrow_down: |
| [torch\_geometric/resolver.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL3Jlc29sdmVyLnB5) | `82.75% <0.00%> (-6.90%)` | :arrow_down: |
| [torch\_geometric/nn/dense/linear.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL2RlbnNlL2xpbmVhci5weQ==) | `88.97% <0.00%> (-5.92%)` | :arrow_down: |
| [torch\_geometric/nn/models/attentive\_fp.py](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team#diff-dG9yY2hfZ2VvbWV0cmljL25uL21vZGVscy9hdHRlbnRpdmVfZnAucHk=) | `95.83% <0.00%> (-4.17%)` | :arrow_down: |
| ... and [30 more](https://codecov.io/gh/pyg-team/pytorch_geometric/pull/6769?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) | |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team)


Patch:
diff --git a/torch_geometric/nn/encoding.py b/torch_geometric/nn/encoding.py
index 7e545b22d..4bef1838c 100644
--- a/torch_geometric/nn/encoding.py
+++ b/torch_geometric/nn/encoding.py
@@ -41,6 +41,11 @@ class PositionalEncoding(torch.nn.Module):
         frequency = torch.logspace(0, 1, out_channels // 2, base_freq)
         self.register_buffer('frequency', frequency)
 
+        self.reset_parameters()
+
+    def reset_parameters(self):
+        pass
+
     def forward(self, x: Tensor) -> Tensor:
         """"""
         x = x / self.granularity if self.granularity != 1.0 else x
diff --git a/torch_geometric/nn/module_dict.py b/torch_geometric/nn/module_dict.py
index b9683ad3d..09c3cab8c 100644
--- a/torch_geometric/nn/module_dict.py
+++ b/torch_geometric/nn/module_dict.py
@@ -1,4 +1,4 @@
-from typing import Iterable, Mapping, Optional
+from typing import Iterable, Mapping, Optional, Tuple
 
 import torch
 from torch.nn import Module
@@ -10,8 +10,7 @@ from torch.nn import Module
 # representation.
 class ModuleDict(torch.nn.ModuleDict):
     def __init__(self, modules: Optional[Mapping[str, Module]] = None):
-        # Replace the keys in modules.
-        if modules:
+        if modules is not None:  # Replace the keys in modules:
             modules = {
                 self.to_internal_key(key): module
                 for key, module in modules.items()
@@ -40,3 +39,7 @@ class ModuleDict(torch.nn.ModuleDict):
 
     def keys(self) -> Iterable[str]:
         return [self.to_external_key(key) for key in super().keys()]
+
+    def items(self) -> Iterable[Tuple[str, Module]]:
+        return [(self.to_external_key(key), value)
+                for key, value in super().items()]


Test patch:
diff --git a/test/nn/test_module_dict.py b/test/nn/test_module_dict.py
index 8a4df545b..16fced0bc 100644
--- a/test/nn/test_module_dict.py
+++ b/test/nn/test_module_dict.py
@@ -1,33 +1,30 @@
-from typing import Mapping
-
 import torch
-from torch.nn import Module
 
 from torch_geometric.nn.module_dict import ModuleDict
 
 
 def test_internal_external_key_conversion():
-    assert ModuleDict.to_internal_key("a.b") == "a#b"
-    assert ModuleDict.to_internal_key("ab") == "ab"
-    assert ModuleDict.to_internal_key("a.b.c") == "a#b#c"
+    assert ModuleDict.to_internal_key('a.b') == 'a#b'
+    assert ModuleDict.to_internal_key('ab') == 'ab'
+    assert ModuleDict.to_internal_key('a.b.c') == 'a#b#c'
 
-    assert ModuleDict.to_external_key("a#b") == "a.b"
-    assert ModuleDict.to_external_key("a#b#c") == "a.b.c"
+    assert ModuleDict.to_external_key('a#b') == 'a.b'
+    assert ModuleDict.to_external_key('a#b#c') == 'a.b.c'
 
 
 def test_dot_syntax_keys():
-    modules: Mapping[str, Module] = {
-        "lin1": torch.nn.Linear(16, 16),
-        "model.lin2": torch.nn.Linear(8, 8),
-        "model.sub_model.lin3": torch.nn.Linear(4, 4),
-    }
-    module_dict = ModuleDict(modules)
-
-    expected_keys = {"lin1", "model.lin2", "model.sub_model.lin3"}
+    module_dict = ModuleDict({
+        'lin1': torch.nn.Linear(16, 16),
+        'model.lin2': torch.nn.Linear(8, 8),
+        'model.sub_model.lin3': torch.nn.Linear(4, 4),
+    })
+
+    expected_keys = {'lin1', 'model.lin2', 'model.sub_model.lin3'}
     assert set(module_dict.keys()) == expected_keys
+    assert set([key for key, _ in module_dict.items()]) == expected_keys
 
     for key in expected_keys:
         assert key in module_dict
 
-    del module_dict["model.lin2"]
-    assert "model.lin2" not in module_dict
+    del module_dict['model.lin2']
+    assert 'model.lin2' not in module_dict