google-deepmind / google-deepmind/chex
Mypy index type error with `chex.dataclass`
- Dominant language
- Python
- Stars
- 957
- Forks
- 74
- Avg merge
- 21h 10m
- Merged PRs (30d)
- 1
Description
According to the [docs](https://chex.readthedocs.io/en/latest/api.html#dataclasses), by default a class wrapped with `chex.dataclass` can be indexed, because the dataclass becomes compatible with `collections.abc.Mapping` (because `mappable_dataclass=True`).
However, mypy doesn't seem to understand this. For example:
```python
import chex
@chex.dataclass
class Container:
foo: float
c = Container(foo=1.)
d = c.foo # OK.
e = c['foo'] # error: Value of type "Container" is not indexable [index]
```
[Looking at the code](https://github.com/deepmind/chex/blob/master/chex/_src/dataclass.py#L49), it seems that this is related to methods such as `__getitem__` that are added dynamically with `setattr` which [mypy doesn't recognise](https://github.com/python/mypy/issues/5719).
Any ideas how to go around this apart for (i) explicitly silencing the error (ii) using a different method of accessing the variables?
Keep up the good work!
Hylke
Contributor guide
Assessment
This issue has not been assessed yet.