Skip to content

Fix mutable default error in MetricsConfig dataclasses #3137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DerekWWang
Copy link

Summary:

Context

Found a dataclass validation error: "mutable default ... is not allowed: use default_factory". The issue was that DefaultMetricsConfig and EmptyMetricsConfig were created as static instances with mutable objects (lists/dicts), but then used as defaults in dataclass fields. This violates Python's dataclass rules since all instances would share the same mutable objects.

Check out this for more info on python dataclasses:
https://docs.python.org/3/library/dataclasses.html#mutable-default-values

Changes

Converted the static config instances to factory functions that return fresh objects each time:

  • DefaultMetricsConfig_create_default_metrics_config()
  • EmptyMetricsConfig_create_empty_metrics_config()

Updated the dataclass field in TrainingAppConfig to use field(default_factory=_create_empty_metrics_config) instead of the static instance.

Now each dataclass gets its own separate config object, fixing the mutable default error.

Differential Revision: D77263018

Summary:
# Context
Found a dataclass validation error: "mutable default ... is not allowed: use default_factory". The issue was that `DefaultMetricsConfig` and `EmptyMetricsConfig` were created as static instances with mutable objects (lists/dicts), but then used as defaults in dataclass fields. This violates Python's dataclass rules since all instances would share the same mutable objects.

Check out this for more info on python dataclasses:
https://docs.python.org/3/library/dataclasses.html#mutable-default-values

# Changes
Converted the static config instances to factory functions that return fresh objects each time:

*   `DefaultMetricsConfig` → `_create_default_metrics_config()`
*   `EmptyMetricsConfig` → `_create_empty_metrics_config()`

Updated the dataclass field in `TrainingAppConfig` to use `field(default_factory=_create_empty_metrics_config)` instead of the static instance.

Now each dataclass gets its own separate config object, fixing the mutable default error.

Differential Revision: D77263018
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 25, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D77263018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants