Unify typing for `__path__`?
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 topkgutil.extend_path. At the moment that only applies tolist[str]but obviously the argument type forextend_pathcould be changed to, for example, accept a TypeVar bound toIterable[str]instead. - It seems to be fairly common (and safe ?) to access the at least the first item of
__path__via__getitem__.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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