Page MenuHomePhabricator

importing typing.DefaultDict fails for Python < 3.5.2
Closed, ResolvedPublic

Description

import typing.DefaultDict for annotation fails for Python 3.5.0 and 3.5.1. This generic class was introduced with Python 3.5.2: https://docs.python.org/3/library/typing.html?highlight=typing%20defaultdict#typing.DefaultDict

======================================================================
ERROR: deletionbot_tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: deletionbot_tests
Traceback (most recent call last):
  File "c:\python350-x64\lib\unittest\loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "C:\projects\pywikibot-g4xqx\tests\deletionbot_tests.py", line 13, in <module>
    from scripts import delete
  File "C:\projects\pywikibot-g4xqx\scripts\delete.py", line 61, in <module>
    from typing import DefaultDict, Set
ImportError: cannot import name 'DefaultDict'
======================================================================

See https://ci.appveyor.com/project/Ladsgroup/pywikibot-g4xqx/builds/34055924/job/9s5el2co7v37y36y

Details

Related Changes in Gerrit:

Event Timeline

Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald Transcript
Xqt triaged this task as High priority.Jul 13 2020, 4:59 AM

The 3.5 docs don't have the note about being introduced in 3.5.2, which is what I used as a reference. https://docs.python.org/3.5/library/typing.html#typing.DefaultDict

The typing module was provisional before Python 3.7.

Options:

  1. Add typing-extensions (PyPI; GitHub) as a dependency.
  2. Add typing-extensions as a dependency for Python < 3.7.
  3. Reqire Python >= 3.5.3
  4. Workaround typing issues - in this case, use Dict instead of DefaultDict

Unless we can get typing-extensions installed on Toolfoge, options 1 and 2 are out.
Option 2 wouldn't allow the use of types introduced in Python 3.7.2+ (e.g. TypedDict).
Option 3 could still leave us with cases where workarounds still need to be applied for typing classes introduced later, but fewer cases than supporting Python < 3.5.3. Since Toolforge has Python 3.5.3, limiting support to Python 3.5.3+ could be reasonable if there aren't major distros with default Python < 3.5.3.

Change 612146 had a related patch set uploaded (by JJMC89; owner: JJMC89):
[pywikibot/core@master] [FIX] Use Dict in place of DefaultDict

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

Change 612146 merged by jenkins-bot:
[pywikibot/core@master] [FIX] Use Dict in place of DefaultDict

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