jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1342001?usp=email )

Change subject: Use lazy logging and f-strings for API and user messages
......................................................................

Use lazy logging and f-strings for API and user messages

Defer interpolation in query-limit and user-configuration logging.
Use f-strings for exception representations and namespace, dictionary
and OAuth errors while preserving their message text.

Change-Id: I99db49d07bde6dbc4cddb853144d8cb2c6e978a8
---
M pywikibot/data/api/_generators.py
M pywikibot/exceptions.py
M pywikibot/scripts/generate_user_files.py
M pywikibot/scripts/login.py
M pywikibot/site/_apisite.py
M pywikibot/site/_namespace.py
M pywikibot/tools/__init__.py
7 files changed, 29 insertions(+), 44 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/data/api/_generators.py 
b/pywikibot/data/api/_generators.py
index e6d7aac..4fb0a31 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -598,17 +598,12 @@

         if prev_limit != new_limit:
             pywikibot.debug(
-                '{name}: query_limit: {query}, api_limit: {api}, '
-                'limit: {limit}, new_limit: {new}, count: {count}\n'
-                '{name}: {prefix}limit: {value}'
-                .format(name=self.__class__.__name__,
-                        query=self.query_limit,
-                        api=self.api_limit,
-                        limit=self.limit,
-                        new=new_limit,
-                        count=self._count,
-                        prefix=self.prefix,
-                        value=self.request[self.prefix + 'limit']))
+                '%s: query_limit: %s, api_limit: %s, '
+                'limit: %s, new_limit: %s, count: %s\n'
+                '%s: %slimit: %s',
+                self.__class__.__name__, self.query_limit, self.api_limit,
+                self.limit, new_limit, self._count, self.__class__.__name__,
+                self.prefix, self.request[self.prefix + 'limit'])
         return prev_limit, new_limit

     def _get_resultdata(self):
diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 861f2b4..0a909ff 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -237,18 +237,15 @@

     def __repr__(self) -> str:
         """Return internal representation."""
-        return '{name}("{code}", "{info}", {other})'.format(
-            name=self.__class__.__name__, **self.__dict__)
+        return (f'{self.__class__.__name__}("{self.code}", "{self.info}", '
+                f'{self.other})')

     def __str__(self) -> str:
         """Return a string representation."""
         if self.other:
-            return '{}: {}\n[{}]'.format(
-                self.code,
-                self.info,
-                ';\n '.join(
-                    f'{key}: {val}'
-                    for key, val in self.other.items()))
+            other = ';\n '.join(
+                f'{key}: {val}' for key, val in self.other.items())
+            return f'{self.code}: {self.info}\n[{other}]'

         return f'{self.code}: {self.info}'

diff --git a/pywikibot/scripts/generate_user_files.py 
b/pywikibot/scripts/generate_user_files.py
index e39ac28..f049cdb 100755
--- a/pywikibot/scripts/generate_user_files.py
+++ b/pywikibot/scripts/generate_user_files.py
@@ -97,8 +97,8 @@
     """Return whether the file exists and print a message if it exists."""
     path = Path(filename)
     if path.exists():
-        pywikibot.info('{1} already exists in the target directory "{0}".'
-                       .format(path.parent, path.name))
+        pywikibot.info('%s already exists in the target directory "%s".',
+                       path.name, path.parent)
         return True
     return False

@@ -508,9 +508,9 @@

     local_args = pywikibot.handle_args(args)
     if local_args:
-        pywikibot.info('Unknown argument{}: {}'
-                       .format('s' if len(local_args) > 1 else '',
-                               ', '.join(local_args)))
+        pywikibot.info('Unknown argument%s: %s',
+                       's' if len(local_args) > 1 else '',
+                       ', '.join(local_args))
         return

     pywikibot.info('You can abort at any time by pressing ctrl-c')
diff --git a/pywikibot/scripts/login.py b/pywikibot/scripts/login.py
index c978000..e0796d5 100755
--- a/pywikibot/scripts/login.py
+++ b/pywikibot/scripts/login.py
@@ -86,9 +86,8 @@
         pywikibot.error(f'Invalid OAuth info for {site}.')
     elif site.username() != identity['username']:
         pywikibot.error(
-            'Logged in on {site} via OAuth as {wrong}, but expect as {right}'
-            .format(site=site,
-                    wrong=identity['username'], right=site.username()))
+            'Logged in on %s via OAuth as %s, but expect as %s',
+            site, identity['username'], site.username())
     else:
         oauth_token = login_manager.consumer_token + login_manager.access_token
         pywikibot.info(
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 6194192..50b090f 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -416,16 +416,12 @@
                 error_msg = ('No username has been defined in your '
                              'user config file: you have to add in this '
                              'file the following line:\n'
-                             "usernames['{family}'][{lang!r}]= {username!r}"
-                             .format(family=self.family,
-                                     lang=self.lang,
-                                     username=self.userinfo['name']))
+                             f"usernames['{self.family}'][{self.lang!r}]= "
+                             f"{self.userinfo['name']!r}")
             else:
-                error_msg = ('Logged in on {site} via OAuth as {wrong}, but '
-                             'expect as {right}'
-                             .format(site=self,
-                                     wrong=self.userinfo['name'],
-                                     right=self.username()))
+                error_msg = (f'Logged in on {self} via OAuth as '
+                             f"{self.userinfo['name']}, but "
+                             f'expect as {self.username()}')

             raise NoUsernameError(error_msg)

diff --git a/pywikibot/site/_namespace.py b/pywikibot/site/_namespace.py
index 447e055..3210f1b 100644
--- a/pywikibot/site/_namespace.py
+++ b/pywikibot/site/_namespace.py
@@ -456,10 +456,9 @@

         # Namespace.lookup_name returns None if the name is not recognised
         if None in result:
-            raise KeyError(
-                'Namespace identifier(s) not recognised: {}'
-                .format(','.join(str(identifier)
-                                 for identifier, ns in zip(identifiers, result)
-                                 if ns is None)))
+            names = ','.join(str(identifier)
+                             for identifier, ns in zip(identifiers, result)
+                             if ns is None)
+            raise KeyError(f'Namespace identifier(s) not recognised: {names}')

         return result
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 7e8b55b..26301bb 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -855,9 +855,8 @@
         conflicts.update(key for key in arg if key in result)
         result.update(arg)
     if conflicts:
-        raise ValueError('Multiple dicts contain the same keys: {}'
-                         .format(', '.join(sorted(str(key)
-                                                  for key in conflicts))))
+        keys = ', '.join(sorted(str(key) for key in conflicts))
+        raise ValueError(f'Multiple dicts contain the same keys: {keys}')
     return result



--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1342001?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I99db49d07bde6dbc4cddb853144d8cb2c6e978a8
Gerrit-Change-Number: 1342001
Gerrit-PatchSet: 3
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to