Skip to content

Commit a536e77

Browse files
vincedovyVincent Dovydaitissayakpaul
authored
Fix json WindowsPath crash (#8662)
* Add check for WindowsPath in to_json_string On Windows, os.path.join returns a WindowsPath. to_json_string does not convert this from a WindowsPath to a string. Added check for WindowsPath to to_json_saveable. * Remove extraneous convert to string in test_check_path_types (tests/others/test_config.py) * Fix style issues in tests/others/test_config.py * Add unit test to test_config.py to verify that PosixPath and WindowsPath (depending on system) both work when converted to JSON * Remove distinction between PosixPath and WindowsPath in ConfigMixIn.to_json_string(). Conditional now tests for Path, and uses Path.as_posix() to convert to string. --------- Co-authored-by: Vincent Dovydaitis <[email protected]> Co-authored-by: Sayak Paul <[email protected]>
1 parent 3b01d72 commit a536e77

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/diffusers/configuration_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os
2424
import re
2525
from collections import OrderedDict
26-
from pathlib import PosixPath
26+
from pathlib import Path
2727
from typing import Any, Dict, Tuple, Union
2828

2929
import numpy as np
@@ -587,8 +587,8 @@ def to_json_string(self) -> str:
587587
def to_json_saveable(value):
588588
if isinstance(value, np.ndarray):
589589
value = value.tolist()
590-
elif isinstance(value, PosixPath):
591-
value = str(value)
590+
elif isinstance(value, Path):
591+
value = value.as_posix()
592592
return value
593593

594594
config_dict = {k: to_json_saveable(v) for k, v in config_dict.items()}

tests/others/test_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import json
1617
import tempfile
1718
import unittest
19+
from pathlib import Path
1820

1921
from diffusers import (
2022
DDIMScheduler,
@@ -91,6 +93,14 @@ def __init__(
9193
pass
9294

9395

96+
class SampleObjectPaths(ConfigMixin):
97+
config_name = "config.json"
98+
99+
@register_to_config
100+
def __init__(self, test_file_1=Path("foo/bar"), test_file_2=Path("foo bar\\bar")):
101+
pass
102+
103+
94104
class ConfigTester(unittest.TestCase):
95105
def test_load_not_from_mixin(self):
96106
with self.assertRaises(ValueError):
@@ -286,3 +296,11 @@ def test_use_default_values(self):
286296

287297
# Nevertheless "e" should still be correctly loaded to [1, 3] from SampleObject2 instead of defaulting to [1, 5]
288298
assert new_config_2.config.e == [1, 3]
299+
300+
def test_check_path_types(self):
301+
# Verify that we get a string returned from a WindowsPath or PosixPath (depending on system)
302+
config = SampleObjectPaths()
303+
json_string = config.to_json_string()
304+
result = json.loads(json_string)
305+
assert result["test_file_1"] == config.config.test_file_1.as_posix()
306+
assert result["test_file_2"] == config.config.test_file_2.as_posix()

0 commit comments

Comments
 (0)