Browse Source

[utils] Apply `partial_application` decorator to existing functions

Thx: yt-dlp/yt-dlp#10653 (etc)
dirkf 1 month ago
parent
commit
a9b4649d92
2 changed files with 9 additions and 2 deletions
  1. 2 2
      test/test_utils.py
  2. 7 0
      youtube_dl/utils.py

+ 2 - 2
test/test_utils.py

@@ -1730,13 +1730,13 @@ Line 1
             callable(test_fn(kwarg=10)),
             'missing positional parameter should apply partially')
         self.assertEqual(
-            test_fn(10, kwarg=0.1), '10, kwarg=0.1',
+            test_fn(10, kwarg=42), '10, kwarg=42',
             'positionally passed argument should call function')
         self.assertEqual(
             test_fn(x=10), '10, kwarg=None',
             'keyword passed positional should call function')
         self.assertEqual(
-            test_fn(kwarg=0.1)(10), '10, kwarg=0.1',
+            test_fn(kwarg=42)(10), '10, kwarg=42',
             'call after partial application should call the function')
 
 

+ 7 - 0
youtube_dl/utils.py

@@ -3187,6 +3187,7 @@ def extract_timezone(date_str):
     return timezone, date_str
 
 
+@partial_application
 def parse_iso8601(date_str, delimiter='T', timezone=None):
     """ Return a UNIX timestamp from the given date """
 
@@ -3264,6 +3265,7 @@ def unified_timestamp(date_str, day_first=True):
         return calendar.timegm(timetuple) + pm_delta * 3600 - compat_datetime_timedelta_total_seconds(timezone)
 
 
+@partial_application
 def determine_ext(url, default_ext='unknown_video'):
     if url is None or '.' not in url:
         return default_ext
@@ -3842,6 +3844,7 @@ def base_url(url):
     return re.match(r'https?://[^?#&]+/', url).group()
 
 
+@partial_application
 def urljoin(base, path):
     path = _decode_compat_str(path, encoding='utf-8', or_none=True)
     if not path:
@@ -3866,6 +3869,7 @@ class PUTRequest(compat_urllib_request.Request):
         return 'PUT'
 
 
+@partial_application
 def int_or_none(v, scale=1, default=None, get_attr=None, invscale=1, base=None):
     if get_attr:
         if v is not None:
@@ -3892,6 +3896,7 @@ def str_to_int(int_str):
         return int_or_none(int_str)
 
 
+@partial_application
 def float_or_none(v, scale=1, invscale=1, default=None):
     if v is None:
         return default
@@ -4286,6 +4291,7 @@ def urlencode_postdata(*args, **kargs):
     return compat_urllib_parse_urlencode(*args, **kargs).encode('ascii')
 
 
+@partial_application
 def update_url(url, **kwargs):
     """Replace URL components specified by kwargs
        url: compat_str or parsed URL tuple
@@ -4307,6 +4313,7 @@ def update_url(url, **kwargs):
     return compat_urllib_parse.urlunparse(url._replace(**kwargs))
 
 
+@partial_application
 def update_url_query(url, query):
     return update_url(url, query_update=query)