-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[red-knot] Add a read_directory()
method to the ruff_db::system::System
trait
#12289
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me, but I'd rather have Micha look at this.
Just noting for the future that pth files may contain multiple paths, and the paths need not be absolute. |
read_dir()
method to the ruff_db::system::System
traitread_directory()
method to the ruff_db::system::System
trait
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat
pub(crate) fn read_directory( | ||
&self, | ||
path: impl AsRef<SystemPath>, | ||
) -> Result<impl Iterator<Item = Result<DirectoryEntry>> + '_> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like to have named return types (over impl
) because callers can then name those types (and e.g. store it in a Struct
).
I would suggest introducing a ReadDirectory
struct similar to std::fs::read_dir
that is a thin wrapper around std::vec::IntoIter
(or whatever the type of your wild iter chain below is)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the value in doing that generally, but here I wonder if an opaque return type might actually be better? If you're e.g. relying on the exact type being returned from this method in a test (for example), you might get into difficulties if you later switch the test to using the OsSystem
and find that a different type is returned from that struct's read_directory()
method. I think there's some value in saying that the only API guarantee we give here is that some kind of iterator is returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the argument is API compatibility with System
than the method should also return a Box<dyn...>
(and accept &SystemPath
instead of AsRef<SystemPath>
as the argument). I think it's rare that we would switch between systems in tests and the change would be minimal. But having a concrete type can be helpful for a system implementation that's based on the memory file system (e.g. WASM).
Anyway, I don't feel strongly (except that we shouldn't change the return type to Box<dyn>
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly either. But I propose we add it when we need it
I just noticed one technical difference. The This might be fine, but it could also be a real foot gun where tests pass because the paths are absolute and later fail in production. |
Summary
Editable installations in Python are (usually1) implemented via static
.pth
files that are inserted intosite-packages
by the build backend. If the sole contents of a.pth
file in thesite-packages
directory are an absolute path to a directory on disk, Python will consider that path to be an additional module-resolution search path that will be appended tosys.path
on startup.To support editable installations in the red-knot module resolver, we'll need to similarly search through the top level of
site-packages
searching for.pth
files. In order to achieve this, this PR adds aread_dir()
method to theruff_db::system::System
trait.A rough indication of the functionality we'll need for editable support can be seen in the following function, which is part of the port @charliermarsh did of pyright's module resolver:
ruff/crates/ruff_python_resolver/src/search.rs
Lines 97 to 134 in d0298dc
Test Plan
cargo test -p ruff_db
Footnotes
Newer versions of setuptools use a more dynamic approach for editable installations, where the
.pth
file contains Python code which, when executed, dynamically computes the path which should be appended tosys.path
. Setuptools is alone in using this approach for editable installations, and no other type checkers support editable installations produced via this method. Setuptools also provides a way of switching to the old approach for editable installations, where the.pth
file simply contains a static path. As such, I do not intend to attempt to support editable installations created using this approach. ↩