pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.08k stars 3.63k forks source link

Fix `map_annotation` when annotation is `{list,dict}` instead of `typing.{List,Dict}` #9269

Closed akihironitta closed 5 months ago

akihironitta commented 5 months ago

Fixes this error:

from dataclasses import dataclass
from typing import Dict

from torch_geometric.config_store import register

@register()
@dataclass
class MyClass1:
    something: Dict[str, int]

@register()
@dataclass
class MyClass2:
    something: dict[str, int]
Traceback (most recent call last):
  File "/home/aki/work/github.com/pyg-team/pytorch_geometric/test_config.py", line 13, in <module>
    @register()
     ^^^^^^^^^^
  File "/home/aki/work/github.com/pyg-team/pytorch_geometric/torch_geometric/config_store.py", line 360, in bounded_register
    register(cls=cls, data_cls=data_cls, group=group, **kwargs)
  File "/home/aki/work/github.com/pyg-team/pytorch_geometric/torch_geometric/config_store.py", line 345, in register
    data_cls = to_dataclass(cls, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/aki/work/github.com/pyg-team/pytorch_geometric/torch_geometric/config_store.py", line 255, in to_dataclass
    annotation = map_annotation(annotation, mapping=MAPPING)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/aki/work/github.com/pyg-team/pytorch_geometric/torch_geometric/config_store.py", line 170, in map_annotation
    annotation.__args__ = tuple(map_annotation(a, mapping) for a in args)
    ^^^^^^^^^^^^^^^^^^^
AttributeError: readonly attribute
akihironitta commented 5 months ago

TODO: handle the case with older Python versions