Page MenuHomePhabricator

textlib.replace_links() raises different exceptions if site parameter is not given
Closed, ResolvedPublicBUG REPORT

Description

List of steps to reproduce and what happens:

>>> import pywikibot
>>> from pywkibot.textlib import replace_links
>>> replace_links('foo', 'ba')
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    replace_links('foo', 'ba')
  File "C:\pwb\GIT\core\pywikibot\textlib.py", line 664, in replace_links
    replace_list[1] = pywikibot.Page(site, replace_list[1])
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 2031, in __init__
    super().__init__(source, title, ns)
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 183, in __init__
    raise Error(
pywikibot.exceptions.Error: Invalid argument type '<class 'NoneType'>' in Page initializer: None
>>> replace_links('foo', None)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    replace_links('foo', None)
  File "C:\pwb\GIT\core\pywikibot\textlib.py", line 673, in replace_links
    raise ValueError('The "site" argument must be provided.')
ValueError: The "site" argument must be provided.
>>> replace_links('foo', 'ba')
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    replace_links('foo', 'ba')
  File "C:\pwb\GIT\core\pywikibot\textlib.py", line 664, in replace_links
    replace_list[1] = pywikibot.Page(site, replace_list[1])
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 2031, in __init__
    super().__init__(source, title, ns)
  File "C:\pwb\GIT\core\pywikibot\page\__init__.py", line 183, in __init__
    raise Error(
pywikibot.exceptions.Error: Invalid argument type '<class 'NoneType'>' in Page initializer: None
>>> replace_links('foo', ['baz', False])
WARNING: <pyshell#11>:1: FutureWarning: site=None is deprecated for 2 years and 8 months; use a valid site for list or tuple parameter "replace" instead.

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    replace_links('foo', ['baz', False])
  File "C:\pwb\GIT\core\pywikibot\textlib.py", line 675, in replace_links
    linktrail = site.linktrail()
AttributeError: 'NoneType' object has no attribute 'linktrail'

Anyway it never passes because site.linktrail is used in line 675 but no site object is created if the parameter is None. The deprecation warning is not usefull

What should have happened instead?:

A site object should always be passes to the function i.e. site parameter should be mandatory

Software version:
Pywikibot 3.0.20190430+

Event Timeline

Xqt triaged this task as High priority.Oct 29 2021, 3:17 PM

Change 735666 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [bugfix] Make site parameter of textlib.replace_links() mandatory

https://gerrit.wikimedia.org/r/735666

Change 735666 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] Make site parameter of textlib.replace_links() mandatory

https://gerrit.wikimedia.org/r/735666

Xqt claimed this task.