davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.78k stars 508 forks source link

What exactly does "smart_sys_path" do? #1987

Closed gwerbin closed 6 months ago

gwerbin commented 6 months ago

The doc for Project says:

smart_sys_path – If this is enabled (default), adds paths from local directories. Otherwise you will have to rely on your packages being properly configured on the sys.path.

I apologize if I'm just being dense, but what does this actually mean? What is considered a "path from local directories" from the perspective of Jedi? A practical example would be very much appreciated.

I took a look at the source code, but from what I could glean it looks like it does the following:

  1. Look for buildout.cfg files, for use with the Buildout tool.
  2. Search upward from something called inference_state.script_path, looking for __init__.py files. Is that "script path" the file currently under analysis?
davidhalter commented 6 months ago

I'm curious, why are you interested in this specific fact?

What is considered a "path from local directories" from the perspective of Jedi? A practical example would be very much appreciated.

I'm struggling a bit to give you an example, because I'm pretty sure I would miss some things without reading the code properly. I think you're on the right track though. In general Jedi tries to find a "project" if none is provided and that might as well be wrong. So we add sys paths like that current or parent directory depending on a few factors. It's not that complicated, but I'm a bit too lazy to read all of that code now :)

You also have to see it from a practical perspective: Jedi is mostly about good completions, not about 100% correctness. So adding a sys path that might not be correct typically does no harm, but is a very useful addition in some cases.

Search upward from something called inference_state.script_path, looking for init.py files. Is that "script path" the file currently under analysis?

Exactly.

gwerbin commented 6 months ago

Thanks for the reply! The reason I am interested is because currently all three established Python language servers are built on Jedi. Exploring their source code, it looks like they are all relatively thin wrappers when it comes to environment detection and configuration. When I am using any one of them, it's helpful to understand what is handled automatically by Jedi, and what I need to set up manually. It's also helpful for anyone who might want to work on an application that uses Jedi, even if it's just contributing documentation.

davidhalter commented 6 months ago

Thanks for your answer. There's also Pyright, just FYI and my upcoming rewrite of Jedi/Mypy in Rust.

I assume your question was answered?

gwerbin commented 6 months ago

It was, thank you! Should I contribute a doc update for this, or do you expect the behavior to be changed in said upcoming rewrite?

davidhalter commented 6 months ago

A contribution would probably make sense (if you find good wording for it).

The behavior will not change in the rewrite, because those projects are going to coexist.