Skip to content

Commit a80f3fd

Browse files
connernilsenfacebook-github-bot
authored andcommitted
Use new PythonEnvironment::find_interpreter() function
Summary: This diff finishes the functionality required to get venv working without having an interpreter sourced. In this diff, we switch to the new `PythonEnvironment::find_interpreter()` function when an interpreter is not provided to search for the interpreter to use. This replaces the call to get the default interpreter, which is used as a final fallback in the `find_interpreter()` function instead. Reviewed By: stroxler Differential Revision: D74366025 fbshipit-source-id: 084e06f49458d25178d382f0adf1d3228fbd361d
1 parent d16482c commit a80f3fd

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pyrefly/lib/config/config.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ pub enum ConfigSource {
6060
Synthetic,
6161
}
6262

63+
impl ConfigSource {
64+
pub fn path<'a>(&'a self) -> Option<&'a Path> {
65+
match &self {
66+
Self::File(path) => Some(path),
67+
Self::Synthetic => None,
68+
}
69+
}
70+
}
71+
6372
/// Where the importable Python code in a project lives. There are two common Python project layouts, src and flat.
6473
/// See: https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/#src-layout-vs-flat-layout
6574
#[derive(Default)]
@@ -361,12 +370,10 @@ impl ConfigFile {
361370
/// which should probably be everything except for `PathBuf` or `Globs` types.
362371
pub fn configure(&mut self) {
363372
if self.python_environment.any_empty() {
364-
if let Some(interpreter) = self
365-
.python_interpreter
366-
.as_deref()
367-
.or(PythonEnvironment::get_default_interpreter())
368-
{
369-
let system_env = PythonEnvironment::get_interpreter_env(interpreter);
373+
if let Some(interpreter) = self.python_interpreter.clone().or_else(|| {
374+
PythonEnvironment::find_interpreter(self.source.path().and_then(|p| p.parent()))
375+
}) {
376+
let system_env = PythonEnvironment::get_interpreter_env(&interpreter);
370377
self.python_environment.override_empty(system_env);
371378
} else {
372379
self.python_environment.set_empty_to_default();

test/basic.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,17 @@ $ mkdir $TMPDIR/foo && touch $TMPDIR/foo/bar.py && echo "x: str = 0" > $TMPDIR/f
137137
INFO 0 errors* (glob)
138138
[0]
139139
```
140+
141+
## We can find a venv interpreter, even when not sourced
142+
143+
```scrut {output_stream: stderr}
144+
$ python3 -m venv $TMPDIR/venv && \
145+
> echo "import third_party.test2" > $TMPDIR/test.py && \
146+
> export site_packages=$($TMPDIR/venv/bin/python -c "import site; print(site.getsitepackages()[0])") && \
147+
> mkdir $site_packages/third_party && \
148+
> echo "x = 1" > $site_packages/third_party/test2.py && \
149+
> touch $TMPDIR/pyrefly.toml && \
150+
> $PYREFLY check $TMPDIR/test.py
151+
INFO 0 errors* (glob)
152+
[0]
153+
```

0 commit comments

Comments
 (0)