Page MenuHomePhabricator

Use explicit submodule imports for typings
Closed, DeclinedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

A lot of parts of pywikibot use classes from other submodules, but it relies on the implicit importation of submodules from other submodules for it. This is incompatible with typecheckers like Pylance (https://github.com/microsoft/pylance-release/issues/4014).

What happens?: Type checkers cannot resolve forward references when the submodule import is not explicit.

What should have happened instead?: Type checking should work

Software version (skip for WMF-hosted wikis like Wikipedia):

Other information (browser name/version, screenshots, etc.):

image.png (128×1 px, 41 KB)

Event Timeline

Xqt triaged this task as Low priority.Mar 6 2023, 3:20 PM
Xqt subscribed.

These implicit types are typing.ForwardRef types and must be recognised by introspective tools. This is an upstream only issue. In addition we cannot solve it due to circular import errors in most cases,

There are alternative ways to only import ForwardRef types for type checkers. A common usage I've seen is:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .mod import SomeClass

def func(param: 'SomeClass') -> int: ...

All static type checkers will set TYPE_CHECKING to True and evaluate the code blocks in there. Ref: https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING