google / google/flax

Improve Error Message: Consider extending AttributeError on Flax Modules

Open
#1,685 3 comments 0 reactions 1 assignee Claimed by @marcvanzee View on GitHub
Priority: P2 - no schedule
Dominant language
Jupyter Notebook
Stars
7.3k
Forks
833
Avg merge
5h 11m
Merged PRs (30d)
5

Description

@untom was surprised about the error from this example:

```python
from flax import linen as nn
from jax import random

class MyClass(nn.Module):
def setup(self):
self.myvar = 42

def __call__(self, x):
return x * self.myvar

model = MyClass()
params = model.init(random.PRNGKey(0), 0)
print(model.myvar)
```

`AttributeError: "MyClass" object has no attribute "myvar"`.

While we are planning to write a "Module Lifecycle" Doc (#1576), I think it could still be useful to improve this error message. It seems like a case that users familiar to Pytorch will hit often, so we could consider extending `AttributeError` on `Module` with a custom error message that provides a brief explanation and points to the Module Lifecycle Doc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.