python / python/typeshed

Unify typing for `__path__`?

Open
#6,650 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

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] https://github.com/python/typeshed/pull/6200
typeshed - pkgutil.extend_path list[str] https://github.com/python/typeshed/pull/5222
mypy list[str] https://github.com/python/mypy/pull/9454
pyright Iterable[str] https://github.com/microsoft/pylance-release/issues/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__.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the typeshed ModuleType and pkgutil.extend_path annotations with Python's module-path and pkgutil documentation, then review the linked mypy and pyright discussions. Determine a consistent type contract that reflects the documented and observed behavior. Done means the relevant annotations agree across the affected stubs and remain compatible with extend_path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.