Skip to content

Commit 4197e59

Browse files
generatedunixname647790274085263facebook-github-bot
authored andcommitted
[codemod][py3.12] Convert dataclasses mutable default values to use default_factory
Summary: As of [Python 3.11](https://docs.python.org/3.11/whatsnew/3.11.html#dataclasses), dataclasses now only allow defaults that are hashable. Using mutable (non-hashable) default values under Python 3.11+ will cause a runtime error: ``` ValueError: mutable default <class 'problematic.ClassName'> for field <field> is not allowed: use default_factory ``` This codemod attempts to automatically convert mutable defaults to use `default_factory` NOTE: The change is not semantically equivalent to before the change. Before, all dataclass instances with a mutable default value were sharing the same instance. This change results each dataclass instance using a new instance of the mutable value. It is likely that the before state was a latent bug, but it's still a behavior change! Reviewed By: moutaigua8183 Differential Revision: D73523400 fbshipit-source-id: c00bb2dc5bcf1df22ce6a663588fc2d5dd1a36be
1 parent a883719 commit 4197e59

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

mmf/models/alignment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import math
44
from copy import deepcopy
5-
from dataclasses import dataclass
5+
from dataclasses import dataclass, field
66
from typing import Any, Tuple
77

88
import torch
@@ -82,8 +82,8 @@ class Config(BaseModel.Config):
8282
direct_features_input: bool = False
8383
image_encoder: Any = MISSING
8484
text_encoder: Any = MISSING
85-
image_projection: Any = IdentityEncoder.Config()
86-
text_projection: Any = IdentityEncoder.Config()
85+
image_projection: Any = field(default_factory=lambda: IdentityEncoder.Config())
86+
text_projection: Any = field(default_factory=lambda: IdentityEncoder.Config())
8787

8888
def __init__(self, config: Config):
8989
"""Initialize the config which is the model configuration."""

0 commit comments

Comments
 (0)