Validate arguments passed as dict to settings.update() (#18337)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Mohammed Yasin 2024-12-22 08:24:40 +08:00 committed by GitHub
parent 66487e5451
commit d43fc78a77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,12 +12,12 @@ import subprocess
import sys import sys
import threading import threading
import time import time
import urllib
import uuid import uuid
from pathlib import Path from pathlib import Path
from threading import Lock from threading import Lock
from types import SimpleNamespace from types import SimpleNamespace
from typing import Union from typing import Union
from urllib.parse import unquote
import cv2 import cv2
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -1130,7 +1130,8 @@ class JSONDict(dict):
def __str__(self): def __str__(self):
"""Return a pretty-printed JSON string representation of the dictionary.""" """Return a pretty-printed JSON string representation of the dictionary."""
return f'JSONDict("{self.file_path}"):\n{json.dumps(dict(self), indent=2, ensure_ascii=False, default=self._json_default)}' contents = json.dumps(dict(self), indent=2, ensure_ascii=False, default=self._json_default)
return f'JSONDict("{self.file_path}"):\n{contents}'
def update(self, *args, **kwargs): def update(self, *args, **kwargs):
"""Update the dictionary and persist changes.""" """Update the dictionary and persist changes."""
@ -1238,14 +1239,23 @@ class SettingsManager(JSONDict):
f"Please change one to avoid possible issues during training. {self.help_msg}" f"Please change one to avoid possible issues during training. {self.help_msg}"
) )
def __setitem__(self, key, value):
"""Updates one key: value pair."""
self.update({key: value})
def update(self, *args, **kwargs): def update(self, *args, **kwargs):
"""Updates settings, validating keys and types.""" """Updates settings, validating keys and types."""
for arg in args:
if isinstance(arg, dict):
kwargs.update(arg)
for k, v in kwargs.items(): for k, v in kwargs.items():
if k not in self.defaults: if k not in self.defaults:
raise KeyError(f"No Ultralytics setting '{k}'. {self.help_msg}") raise KeyError(f"No Ultralytics setting '{k}'. {self.help_msg}")
t = type(self.defaults[k]) t = type(self.defaults[k])
if not isinstance(v, t): if not isinstance(v, t):
raise TypeError(f"Ultralytics setting '{k}' must be of type '{t}', not '{type(v)}'. {self.help_msg}") raise TypeError(
f"Ultralytics setting '{k}' must be '{t.__name__}' type, not '{type(v).__name__}'. {self.help_msg}"
)
super().update(*args, **kwargs) super().update(*args, **kwargs)
def reset(self): def reset(self):
@ -1265,7 +1275,7 @@ def deprecation_warn(arg, new_arg=None):
def clean_url(url): def clean_url(url):
"""Strip auth from URL, i.e. https://url.com/file.txt?auth -> https://url.com/file.txt.""" """Strip auth from URL, i.e. https://url.com/file.txt?auth -> https://url.com/file.txt."""
url = Path(url).as_posix().replace(":/", "://") # Pathlib turns :// -> :/, as_posix() for Windows url = Path(url).as_posix().replace(":/", "://") # Pathlib turns :// -> :/, as_posix() for Windows
return urllib.parse.unquote(url).split("?")[0] # '%2F' to '/', split https://url.com/file.txt?auth return unquote(url).split("?")[0] # '%2F' to '/', split https://url.com/file.txt?auth
def url2file(url): def url2file(url):