Skip to content

PEP 660 support #2114

Closed
Closed

Description

@nihilox

Environment data

  • Language Server version: v2021.11.2
  • OS and version: Mac OS 12.0
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.0

Actual behavior

Resolved correctly with pylance disabled

Code Snippet / Additional information

foo.pth

import _foo

_foo.py

from editables.redirector import RedirectingFinder as F
F.install()
F.map_module('foo', '...')

Activity

  1. brettcannon commented on Feb 22, 2022

    @brettcannon
    Member

    Discussion about adding support in various editors has now been opened at https://discuss.python.org/t/pep-660-support-and-ides/13878 .

  2. gaborbernat commented on Mar 22, 2022

    @gaborbernat

    How can the community help with this? Who would be able to sign off on a proposal to solve this? Which part of the code base would need to be impacted? Thanks!

  3. erictraut commented on Mar 22, 2022

    @erictraut
    Contributor

    Which portion of this isn't working? Pyright (the type checker that pylance is built upon) already supports pth files, so I would think that this would already work.

    It would be helpful if you could provide a specific example project, set of installation steps, and an indication of expected vs. observed behaviors for that project.

  4. gaborbernat commented on Mar 22, 2022

    @gaborbernat

    Code Snippet / Additional information

    foo.pth

    import _foo
    

    _foo.py

    from editables.redirector import RedirectingFinder as F
    F.install()
    F.map_module('foo', '...')

    Note here the pth file does not inject the path as normally does but rather installs a custom import hook that resolves imports. So the pth file support does not work. Here you need custom import hook support, or something like that.

  5. erictraut commented on Mar 22, 2022

    @erictraut
    Contributor

    Are you saying that the code is dynamically modifying the module map? Static analysis tools like linters, language servers, and type checkers (including pylance) do not execute the Python code. If the behavior is completely dynamic, then we probably won't be able to make PEP 660 work with pylance. Is there a way for pylance to statically find the editable install without executing any Python code?

  6. brettcannon commented on Mar 22, 2022

    @brettcannon
    Member

    Are you saying that the code is dynamically modifying the map module?

    It's dynamically adding the foo module as an editable install (e.g. settings things up so imports pull from the source code in the workspace and not somewhere in site-packages).

  7. erictraut commented on Mar 22, 2022

    @erictraut
    Contributor

    If this is an entirely dynamic mechanism, that would be really unfortunate. We probably won't be able to support it in pylance then, and we'll need to recommend against anyone using it if they want to take advantage of any static analysis tools or language servers.

  8. gaborbernat commented on Mar 22, 2022

    @gaborbernat

    Can we come up together with a mechanism that provides enough info for the IDE/language server on what's being done, that perhaps the backends can call? E.g. in this case we could say module x is available at path Y in a JSON file? (and at runtime would be an import dynamic hook, but the language server would know from where to materialize it by using the static analysis tools).

  9. brettcannon commented on Mar 22, 2022

    @brettcannon
    Member

    If this is an entirely dynamic mechanism, that would be really unfortunate.

    This mechanism is, yes.

    https://peps.python.org/pep-0660/ outlines how to tell build tools the user wants an editable install, but it doesn't prescribe the solution. The one mentioned here is one possible approach, but it's also the one gaining traction.

    As Bernát Gábor (@gaborbernat) mentioned while I was typing this, can the Pylance team and the packaging community collaborate on a solution that build tools can use in order to support editable installs but is defined in such a way you can process the information appropriately?

  10. erictraut commented on Mar 22, 2022

    @erictraut
    Contributor

    The import resolution mechanism in Python is already incredibly complex and customizable. Do we really need yet another incompatible mechanism that the entire ecosystem needs to now implement? Why can't this mechanism make use of "pth" files so the location of the library is automatically added to the import resolution paths?

  11. erictraut commented on Mar 22, 2022

    @erictraut
    Contributor

    If PEP 660 is incomplete and didn't take into account static analysis tools, that's a big miss. It sounds like a follow-on PEP — or an amendment to PEP 660 — may be required to fix the problem.

  12. gaborbernat commented on Mar 22, 2022

    @gaborbernat

    Why can't this mechanism make use of "pth" files so the location of the library is automatically added to the import resolution paths?

    The big problem with pth files is that they are very strict and does not allow you to handle files generated during the build 🤔 What would be a good API for pylance, that could somehow handle files generated dynamically. To bring up an example imagine you have a JSON Schema and you're generating the python code from it during package build time via https://pydantic-docs.helpmanual.io/datamodel_code_generator

  13. 12 remaining items

  14. erictraut commented on Mar 24, 2022

    @erictraut
    Contributor

    No, that's not how language servers work.

    Import resolution should not require tools to open source files and scan for comments. I'll give you points for creativity, but let's keep looking for better options.

    This is probably not the right forum to be discussing this. We should move this to typing-sig or some other forum where other tools developers can weigh in.

  15. gaborbernat commented on Mar 25, 2022

    @gaborbernat

    This is probably not the right forum to be discussing this. We should move this to typing-sig or some other forum where other tools developers can weigh in.

    The forum for that is https://discuss.python.org/t/pep-660-support-and-ides/13878/9 but tool developers doesn't seem to weigh much in 😊

  16. erictraut commented on Mar 25, 2022

    @erictraut
    Contributor

    Tool developers don't hang out in that forum. You will get more attention from them if you post in the typing-sig or the discussions section of python/typing.

  17. gaborbernat commented on Mar 25, 2022

    @gaborbernat

    But this is a topic about packaging why would open a discussion on the unrelated typing forum?

  18. judej commented on Apr 19, 2022

    @judej

    Until the typing community and the packaging community agree on a direction, there is nothing that Pylance can do.

  19. gaborbernat commented on Apr 20, 2022

    @gaborbernat

    But this isn't related to the typing system. This is purely about packaging 🤔 and this issue was a call for input from the PyLance team 🤔 there's no point in us agreeing on something that the pylance team then doesn't adopt.

  20. erictraut commented on Apr 20, 2022

    @erictraut
    Contributor

    Bernát Gábor (@gaborbernat), this is an issue that goes beyond pylance. A viable solution must also work for other static analysis tooling (mypy, pytype, pyre, pylint, PyCharm, etc.). If you want to engage with authors of static analysis tools and get their support, you need to go to the forums where they hang out, and you need to work with that community on a standard.

    If the standard involves one of the existing mechanisms for discovering import resolution paths in a static manner (as I recommend), then there will be no need for pylance or any other existing tools to do any work to support PEP 660.

    If there is some new mechanism required, then it should take the form of a PEP that goes through the normal review and ratification process. Once a mechanism is agreed upon, pylance will implement support for it.

  21. erictraut commented on Apr 20, 2022

    @erictraut
    Contributor

    I just noticed that you started a thread on the typing-sig mailing list. Thanks for doing that! I'm hopeful that we'll receive some good ideas and input from the broader community within that discussion forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pep 660Issues related to PEP 660 import hooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions