jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1340498?usp=email )
Change subject: Use direct mapping iteration when building key lists
......................................................................
Use direct mapping iteration when building key lists
Replace list(mapping.keys()) with list(mapping) where iteration yields
keys. Retain the lists used for snapshots, sorting, mutation, JSON
ordering fields, and output formatting. Apply the same simplification
to the corresponding examples and test helpers.
Keep SizedKeyCollection.keys(), since that collection iterates values,
and retain the OptionSet test that explicitly exercises keys().
Change-Id: I1f181c64ad2211344302df9e7edbb366afc62206
---
M pywikibot/page/_wikibase.py
M pywikibot/site/_apisite.py
M pywikibot/site/_generators.py
M pywikibot/textlib.py
M pywikibot/xmlreader.py
M scripts/maintenance/make_i18n_dict.py
M tests/page_tests.py
M tests/tools_tests.py
8 files changed, 18 insertions(+), 18 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index ec64857..7778ca3 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1933,7 +1933,7 @@
else:
if self.qualifiers:
data['qualifiers'] = {}
- data['qualifiers-order'] = list(self.qualifiers.keys())
+ data['qualifiers-order'] = list(self.qualifiers)
for prop, qualifiers in self.qualifiers.items():
for qualifier in qualifiers:
assert qualifier.isQualifier is True
@@ -1944,7 +1944,7 @@
data['references'] = []
for collection in self.sources:
reference = {
- 'snaks': {}, 'snaks-order': list(collection.keys())}
+ 'snaks': {}, 'snaks-order': list(collection)}
for prop, val in collection.items():
reference['snaks'][prop] = []
for source in val:
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 7c0c0c3..6f9deda 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -1743,10 +1743,10 @@
>>> site = pywikibot.Site()
>>> tokens = site.get_tokens([]) # get all tokens
- >>> list(tokens.keys()) # result depends on user
+ >>> list(tokens) # result depends on user
['createaccount', 'login']
>>> tokens = site.get_tokens(['csrf', 'patrol'])
- >>> list(tokens.keys()) # doctest: +SKIP
+ >>> list(tokens) # doctest: +SKIP
['csrf', 'patrol']
>>> token = site.get_tokens(['csrf']).get('csrf') # get a single token
>>> token # doctest: +SKIP
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index 3481db1..695cf77 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -206,7 +206,7 @@
# only use pageids if all pages have them
rvgen.request['pageids'] = set(pageids)
else:
- rvgen.request['titles'] = list(cache.keys())
+ rvgen.request['titles'] = list(cache)
rvgen.request['rvprop'] = self._rvprops(content=content)
if coordinates:
rvgen.request['coprop'] = [
@@ -238,7 +238,7 @@
except KeyError:
pywikibot.debug(f"No 'title' in {pagedata}\n"
f'{pageids=!s}\n'
- f'titles={list(cache.keys())}')
+ f'titles={list(cache)}')
continue
priority, page = cache[pagedata['title']]
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index e65b96b..dc27ebd 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -281,7 +281,7 @@
r'\[\[:?(%s)\s?:[^\]]*\]\]\s*',
lambda site: '|'.join(
ignore_case(i) for i in site.validLanguageLinks()
- + list(site.family.obsolete.keys()))),
+ + list(site.family.obsolete))),
# Module invocations (currently only Lua)
'invoke': (
r'\{\{\s*\#(?:%s):(?s:.*?)\}\}',
@@ -1476,7 +1476,7 @@
# This regular expression will find every interwiki link, plus trailing
# whitespace.
languages = '|'.join(site.validLanguageLinks()
- + list(site.family.obsolete.keys()))
+ + list(site.family.obsolete))
if not languages:
return text
interwikiR = re.compile(rf'\[\[({languages})\s?:[^\[\]\n]*\]\][\s]*',
@@ -1654,7 +1654,7 @@
if insite is None:
insite = pywikibot.Site()
- ar = interwikiSort(list(links.keys()), insite)
+ ar = interwikiSort(list(links), insite)
s = []
for site in ar:
if isinstance(links[site], pywikibot.Link):
diff --git a/pywikibot/xmlreader.py b/pywikibot/xmlreader.py
index 74037a6..5887384 100644
--- a/pywikibot/xmlreader.py
+++ b/pywikibot/xmlreader.py
@@ -164,7 +164,7 @@
since='9.0.0')
if revisions not in self.rev_actions:
- actions = str(list(self.rev_actions.keys())).strip('[]')
+ actions = str(list(self.rev_actions)).strip('[]')
raise ValueError(f"'revisions' must be one of {actions}.")
self._parse = self.rev_actions[revisions]
diff --git a/scripts/maintenance/make_i18n_dict.py
b/scripts/maintenance/make_i18n_dict.py
index 7a42caf..9ccc544 100755
--- a/scripts/maintenance/make_i18n_dict.py
+++ b/scripts/maintenance/make_i18n_dict.py
@@ -76,7 +76,7 @@
print('No messages found, read them first.\n'
'Use "run" or "to_json" methods')
return
- keys = list(self.dict.keys())
+ keys = list(self.dict)
keys.remove('qqq')
keys.sort()
keys.insert(0, 'qqq')
@@ -97,7 +97,7 @@
def read(self, oldmsg, newmsg=None) -> None:
"""Read a single message from source script."""
msg = getattr(self.script, oldmsg)
- keys = list(msg.keys())
+ keys = list(msg)
keys.append('qqq')
if newmsg is None:
newmsg = oldmsg
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 05c7cd5..0c06c81 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -112,11 +112,11 @@
for num in self.namespaces:
for prefix in self.namespaces[num]:
link = pywikibot.page.Link(
- prefix + list(self.titles.keys())[0], self.enwiki)
+ prefix + list(self.titles)[0], self.enwiki)
self.assertEqual(link.namespace, num)
# namespace prefixes are case-insensitive
lowered_link = pywikibot.page.Link(
- prefix.lower() + list(self.titles.keys())[1], self.enwiki)
+ prefix.lower() + list(self.titles)[1], self.enwiki)
self.assertEqual(lowered_link.namespace, num)
def testTitles(self) -> None:
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index e821ed6..06a9aca 100755
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -461,9 +461,9 @@
if key in (hash, passthrough):
if isinstance(deduped, OrderedDict):
- self.assertEqual(list(deduped.keys()), [1, 3])
+ self.assertEqual(list(deduped), [1, 3])
elif isinstance(deduped, Mapping):
- self.assertCountEqual(list(deduped.keys()), [1, 3])
+ self.assertCountEqual(list(deduped), [1, 3])
else:
self.assertEqual(deduped, {1, 3})
@@ -472,9 +472,9 @@
if key in (hash, passthrough):
if isinstance(deduped, OrderedDict):
- self.assertEqual(list(deduped.keys()), [1, 3, 2, 4])
+ self.assertEqual(list(deduped), [1, 3, 2, 4])
elif isinstance(deduped, Mapping):
- self.assertCountEqual(list(deduped.keys()), [1, 2, 3, 4])
+ self.assertCountEqual(list(deduped), [1, 2, 3, 4])
else:
self.assertEqual(deduped, {1, 2, 3, 4})
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1340498?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: I1f181c64ad2211344302df9e7edbb366afc62206
Gerrit-Change-Number: 1340498
Gerrit-PatchSet: 2
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]