Skip to content

Unify typing for __path__? #6650

@cdce8p

Description

@cdce8p

After the last mypy update v0.920, I started to notice an inconsistency with how the module variable __path__ is typed mainly across typeshed, mypy, and pyright.

Type Link
typeshed - ModuleType MutableSequence[str] #6200
typeshed - pkgutil.extend_path list[str] #5222
mypy list[str] python/mypy#9454
pyright Iterable[str] microsoft/pylance-release#1098

In the Python docs it's mentioned as:

`__path__` must be an iterable of strings, but it may be empty.

However the docs for pkgutil.extend_path go on say it's mostly a list (?) cpython/pkgutil.py

    If the input path is not a list (as is the case for frozen
    packages) it is returned unchanged.  The input path is not
    modified; an extended copy is returned.  Items are only appended
    to the copy at the end.

From my point of view, it isn't entirely clear what the "correct" type should be. Some points to consider though:

  • In most cases it seems to be a list[str].
  • It was mentioned here however that it can sometimes also be a MutableSequence[str]
  • The __path__ type should be valid as first argument to pkgutil.extend_path. At the moment that only applies to list[str] but obviously the argument type for extend_path could be changed to, for example, accept a TypeVar bound to Iterable[str] instead.
  • It seems to be fairly common (and safe ?) to access the at least the first item of __path__ via __getitem__.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions