Clarification about how to implement namespace packages (as in PEP 420) via import hooks for PEP 660 use case
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Métriques de merge des PR
- Métriques de PR en attente
Description
This is a follow up of 2 previous discussions in the Python discourse and an issue in pypa/packaging-problems:
- https://discuss.python.org/t/pep-660-and-setuptools/14855/15
- https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192
- https://github.com/pypa/packaging-problems/issues/588
Summary
One of the popular ways of implementing PEP 660 is via import hooks (since other alternatives like symlinking package folders are not available in all platforms).
However the reference implementation editables currently does not support namespaces.
Specifically the dynamic path computation aspect of PEP 420 is a challenge.
After inspecting the implementation for importlib._bootstrap_external, I may have managed to find 2 solutions for this problem, described in the detail in this discourse post and demonstrated as a PoC in this gist:
- Solution A: Create a custom "path class" that tries to emulate the
_NamespacePathbehaviour. - Solution B: Create a custom
PathEntryFinderand add a “bogus” entry tosys.pathjust to trigger it (implicitly relying thatimportlib.machinery.PathFinderwill take care of setting__path__to_NamespacePathobject under the hood).
However, the problem is that I don't know if these approaches are reliable in the long run and in accordance with the language specification.
Open Questions
Questions like the following are still not clear to me:
- Is there a chance that an internal check for namespace packages will fail if
__path__uses a custom class instead of_NamespacePatheven if the behaviour described in PEP 420 is observed? - Can we rely on the fact that the import machinery will automatically set the namespace loader for specs in the form of
ModuleSpec("name", None, is_package=True)? - Is
pkgutil(and particularlypkgutil.extend_path) still considered first class citizen of the standard library or is it better to avoid it when writing new code? - Can we rely on the behaviour of
sys.path_hooks+importlib.machinery.PathFinderto createModuleSpecobjects for namespaces that will automatically perform dynamic path computation? Or is this an internal implementation detail that can change in the future?
If the discussed approaches are not recommended, how can we support namespace packages using import hooks and deliver a complete solution for PEP 660?
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par lire Lib/importlib._bootstrap_external.py autour de l’implémentation référencée des namespace packages, puis comparez PEP 420 avec les discussions Discourse liées et le gist de preuve de concept. Le travail sera considéré comme terminé lorsqu’il aura été établi et documenté si Solutions A ou B sont compatibles avec la spécification et de quelle manière les import hooks devraient prendre en charge le cas d’utilisation de PEP 660.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 20/100