fix: coerce sleep_requests and numeric settings from string form values

`sleep_requests` (and other numeric inheritable video download settings)
could arrive as strings ('' or '0.75') from the frontend NumberInputForm
and DownloadRequest bodies. These were persisted into Download.settings
(JSONB) and later triggered:

    if effective['sleep_requests'] > 0:
    TypeError: '>' not supported between instances of 'str' and 'int'

in VideoDownloader.execute_download (and similar sites).

Root cause: DownloadSettings.__post_init__ only filtered None/[] and did
no type coercion, even though the dataclass declared Optional[float]/int.

Changes (minimal targeted fix):
- Add _coerce_optional_number helper and apply it to all numeric fields
  in DownloadSettings.__post_init__ (the existing sanitization boundary).
- Centralize legacy string coercion for persisted data in the single
  read chokepoint: get_effective_video_settings.
- Simplify VideoDownloaderConfig.sleep_requests getter.
- Frontend: use parseFloat for type=number inputs + null defaults for
  sleep_requests (prevents '' from ever being submitted).
- Add focused regression test for string coercion path.
- Revert unrelated change to string field filtering behavior.

No NaN/Inf speculation, no duplicated try/float guards at every call site,
no behavior change for non-numeric settings.
This commit is contained in:
Roland 2026-05-31 12:43:56 -06:00 • committed by lrnselfreliance
parent cd9fb4c6de
commit 9cc5c15e32
8 changed files with 111 additions and 12 deletions

View file

@ -950,8 +950,12 @@ class VideoDownloader(Downloader, ABC):
cmd = (*cmd, '--write-info-json')
if effective['yt_dlp_extra_args']:
cmd = (*cmd, *effective['yt_dlp_extra_args'].split(' '))
if effective['sleep_requests'] > 0:
cmd = (*cmd, '--sleep-requests', str(effective['sleep_requests']))
if effective.get('sleep_requests'):
try:
if float(effective['sleep_requests']) > 0:
cmd = (*cmd, '--sleep-requests', str(effective['sleep_requests']))
except (TypeError, ValueError):
pass
on_stdout = make_progress_callback(ctx.report_progress, parse_ytdlp_progress)

View file

@ -689,7 +689,10 @@ class VideoDownloaderConfig(ConfigFile):
@property
def sleep_requests(self) -> float:
return self._config.get('sleep_requests', 0.75)
try:
return float(self._config.get('sleep_requests', 0.75))
except (TypeError, ValueError):
return 0.75
@sleep_requests.setter
def sleep_requests(self, value: float):
@ -778,6 +781,15 @@ def get_effective_video_settings(download_settings: dict = None) -> dict:
for key in INHERITABLE_VIDEO_SETTINGS:
if key in download_settings:
effective[key] = download_settings[key]
# Safety net for already-persisted downloads that may contain string values
# for numeric settings (from before the DownloadSettings coercion fix).
# This is the single chokepoint so downstream code can assume correct types.
if effective.get('sleep_requests') not in (None, ''):
try:
effective['sleep_requests'] = float(effective['sleep_requests'])
except (TypeError, ValueError):
effective['sleep_requests'] = 0.0
return effective
@ -798,8 +810,11 @@ def get_yt_dlp_sleep_opts() -> dict:
This adds a delay between HTTP requests during metadata extraction,
helping avoid bot detection. Does not affect video file download speed."""
config = get_videos_downloader_config()
if config.sleep_requests > 0:
return {'sleep_requests': config.sleep_requests}
try:
if float(config.sleep_requests) > 0:
return {'sleep_requests': config.sleep_requests}
except (TypeError, ValueError):
pass
return {}

View file

@ -66,7 +66,7 @@ class ChannelDownloadRequest:
if not self.url:
raise ValidationError('url cannot be empty')
# Validate settings contents. Remove empty values.
# Validate settings contents. Remove empty values.
from wrolpi.schema import DownloadSettings
self.settings = {k: v for k, v in DownloadSettings(**self.settings).__dict__.items() if v not in ([], None)}