pyrefly: Pyrefly Cannot Correctly Figure Out Dependencies in Non-VSCode Editors
Describe the Bug
(.venv) noten@noten:~/Documents/GitHub/hukuksuzlugun-bedeli$ ls
backend.sh frontend.sh pyproject.toml README.md
backend frontend LICENSE pyrightconfig.json
In my project, the backend module contains python code. Pyrefly cannot detect that I am using a virtualenv and therefore gives an error when I run pyrefly check, as it thinks I haven’t installed those modules. My venv directory is .venv/.
I already have a pyproject.toml setup:
[tool.pyrefly]
search_path = ["backend/"] # analyze these directories
#python_executable = ".venv/bin/python3"
# site_package_path=[".venv/lib/python3.12/site-packages"]
# python_interpreter=".venv/bin/python3"
I tried commenting and uncommenting the site_package_path and python_interpreter, it still doesn’t work.
I am guessing I am missing something, what am I doing wrong?
(.venv) noten@noten:~/Documents/GitHub/hukuksuzlugun-bedeli$ pyrefly check
INFO Checking project configured at `/home/noten/Documents/GitHub/hukuksuzlugun-bedeli/pyproject.toml`
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/actions.py:2:21-27: Could not import `bcrypt` from `passlib` [missing-module-attribute]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/evdsclient.py:10:25-42: No attribute `adapters` in module `requests` [missing-attribute]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/evdsclient.py:17:14-25: Attribute `poolmanager` is implicitly defined by assignment in method `init_poolmanager`, which is not a constructor [implicitly-defined-attribute]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/evdsclient.py:55:14-21: Attribute `session` is implicitly defined by assignment in method `__create_session`, which is not a constructor [implicitly-defined-attribute]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/evdsclient.py:78:13-46: Item assignment is not supported on `DataFrame | Series | Unknown`
Expected `__setitem__` to be a callable, got BoundMethod[DataFrame, (self: Self@DataFrame, key: Unknown, value: Unknown) -> None] | BoundMethod[Series, (self: Self@Series, key: Unknown, value: Unknown) -> None] | Unknown [not-callable]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/evdsclient.py:103:33-76: Object of class `ndarray` has no attribute `to_list` [missing-attribute]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/fetcher.py:208:27-58: Expected 0 type arguments for `FrequencyEVDS`, got 1 [bad-specialization]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/fetcher.py:208:41-57: Expected a type form, got instance of `FrequencyEnum` [not-a-type]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/main.py:53:5-23: Missing argument `self` in function `functools._Wrapped.__call__` [missing-argument]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/main.py:60:1-19: Expected `Ellipsis` to be a ParamSpec value in function `starlette.applications.Starlette.add_middleware` [bad-argument-type]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/main.py:61:5-19: Argument `type[CORSMiddleware]` is not assignable to parameter with type `_MiddlewareFactory[@_]` in function `starlette.applications.Starlette.add_middleware` [bad-argument-type]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/main.py:227:33-56: Item assignment is not supported on `dict[str, str]`
Argument `list[@_]` is not assignable to parameter with type `str` in function `dict.__setitem__` [bad-argument-type]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/main.py:240:21-48: Item assignment is not supported on `dict[str, dict[str, list[Unknown]]]`
Argument `dict[str, str]` is not assignable to parameter with type `dict[str, list[Unknown]]` in function `dict.__setitem__` [bad-argument-type]
ERROR /home/noten/Documents/GitHub/hukuksuzlugun-bedeli/backend/models.py:89:11-15: Class member `MetricDataPoint.Meta` overrides parent class `Model` in an inconsistent manner [bad-override]
INFO 14 errors shown, 1 errors ignored, 15 modules, 1,610 transitive dependencies, 753,673 lines, took 1.76s (checking 1.06s; listing 0.69s), peak memory physical 494.4 MiB
If it helps, I am on Linux Mint 22.1 with Zed editor, however I don’t think it’s something related to Zed.
Sandbox Link
No response
(Only applicable for extension issues) IDE Information
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 30 (15 by maintainers)
Commits related to this issue
- Use Pyrefly config finding default logic before langauge client provides expected python environment Summary: vscode-python is only used in VSCode, so our logic around getting the interpreter from th... — committed to connernilsen/pyrefly by connernilsen a year ago
- Use Pyrefly config finding default logic before langauge client provides expected python environment (#451) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/451 vscode-python... — committed to connernilsen/pyrefly by connernilsen a year ago
- Use Pyrefly config finding default logic before langauge client provides expected python environment (#451) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/451 vscode-python... — committed to facebook/pyrefly by connernilsen a year ago
@connernilsen It’s working fine on my company’s computer now! On Windows 11, I ran
uv init hello(to create the virtual environment) anduv add httpx typer(to add dependencies), then edited the files directly with Helix text editor (without runningpyrefly init). There were no import errors. Auto-completion works fine too 🎉🎉🎉@anzhi0708 @Notenlish Alright, just released 0.19.1, which should have the changes. Try it out when you get a chance, and let me know if it’s working for you!
For clarification, @Notenlish #451 should address your issue too.
@anzhi0708 @Notenlish I’ll do a release later this week (probably tomorrow) once I land another fix that needs to get in, and we can double check to see if your problem is solved.
To add more color, we don’t only support vscode for the interpreter selector. Our .vsix is also deployed to openvsx among other places. Other language clients that support the workspace capability can send a
workspace/configurationresponse if they have their own method for selecting Python interpreters. I can add more documentation if that is something people are interested in.But in this case, the configuration file or
.venvdetection is likely the easiest solution.Hey @Notenlish and @anzhi0708, sorry, but I was out for part of last week, and I’m still catching up with stuff from this week. I’ll get around to looking at this tomorrow or early next week. Just wanted to let you know that it’s still in my queue!
@Notenlish it looks like the
passliblibrary you’re using does a lot of weird dynamic stuff in it that static analysis likely won’t be able to handle. The solution here, sincepasslibis included in typeshed stubs, is to use that stubs lib automatically, which we have someone working on setting up right now.Until then, if you want to just get something working in the meantime, I’d recommend the following:
python3 import site; print(site.getsitepackages())(source your venv before if you have one)site-package-pathto[<path to typeshed from step 2>/stubs, <site package path from step 1>]This should include the stubs in a way Pyrefly can use for you.
Alternatively, if you’re sharing this with others, you can check in the project stubs you need from typeshed somewhere into your project and add it as a
search-pathentry in your config (e.g. copytypeshed/stubs/passlibto something like<your_project>/stubs, then setsearch-path = [<your project stuff>, "stubs"]). This may cause type checking to behave weirdly in some cases though, so you may have to wait until we fix the typeshed importing stuff to be able to use this.Oh interesting @kinto0.
@Notenlish We have a task for someone to work on auto including the typeshed stubs that I believe someone is going to pick up soon.
Glad to hear that! I should have clarified from the beginning: (1) I’m not using the VS Code extension, and (2) my issue is specifically related to LSP mode.
If you could let me know which version I should test and where I can provide feedback, I’d be happy to help.
Alright, I opened #451 to address the interpreter finding stuff so that we can hopefully be better about finding interpreters (especially local venv interpreters) for non-vscode IDEs. Still need to test once more, but that should be a big improvement there.
Also opened #450 to make our debug logging less confusing about
python_interpreter@anzhi0708 I see. I’ll go through and try to make that more clear, since it really is quite confusing to see that when it’s really not the true state of things (I was also tripped up until I realized that’s what was going on 😅).
And yeah, I’ve been trying to work on getting the automatic-functionality working, but haven’t been able to look at it/fix it in a few weeks. Currently, the way that Pyrefly only uses the MS-python extension to determine the environment in LSP mode before falling back to some system defaults was an intentional design choice at a time where we were more constrained (c.c. @kinto0).
I’m pretty sure I have an idea of how to implement that stuff better now though, so that we can still use our original environment finding logic when an IDE doesn’t tell us which interpreter to use, which I’d imagine is how all non-vscode editors would behave. Ideally, after this, you shouldn’t need to specify your interpreter path!
By the way, the editor I use is
helix. From my perspective, once I understand how to select the Python interpreter forpyrefly, it seems to come down to a matter of choice — that is, whetherpyreflyshould default to using the system-wide interpreter, or automatically detect at startup whether it’s inside a likely virtual environment directory and default to using the interpreter provided by that environment.