Page MenuHomePhabricator

FilePage.getRedirectTarget should return a FilePage
Closed, ResolvedPublic

Description

Please treat this as an RFC rather than a bug report. The following simplified code should illustrate the problem:

val = pywikibot.FilePage(commons, filename)
while val.isRedirectPage():
      val = val.getRedirectTarget() #type(val) is now pywikibot.Page
claim = pywikibot.Claim(self.item.repo, prop, datatype="commonsMedia")
claim.setTarget(val) #this fails if filename is redirect, works otherwise

Even if theoretically you could have cross-ns redirects, these are highly discouraged and so I believe it would make sense to override getRedirectTarget in FilePage to ensure it returns a FilePage rather than a simple Page.

Event Timeline

Strainu updated the task description. (Show Details)

I'd go further and have BasePage.getRedirectTarget() return an object of the same class as the redirect. This would keep the implementation general and allow other subclasses that want the returned object to be the same class to use the method without overriding it.

It could be implemented in BasePage.getRedirectTarget() or Site.getredirtarget().

def getRedirectTarget(self):
    target = self.site.getredirtarget(self)
    try:
        target = self.__class__(target)
    except ValueError:
        pass
    return target
Xqt triaged this task as Low priority.Sep 21 2019, 7:50 AM

What about internamespace redirects?

Change 538378 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [IMPR] Upcast getRedirectTarget result

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

What about internamespace redirects?

Currently a InterwikiRedirectPage exception will be raised

Change 538378 merged by jenkins-bot:
[pywikibot/core@master] [IMPR] Upcast getRedirectTarget result

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

What about internamespace redirects?

These are not affected by Xqt's patch - you just get the right class according to the class, which I think is good - if you expect a Page, you can still use it as such, if not, you use it according to the expected type.