Skip to content

Commit 6b64630

Browse files
authored
Update --python to accept paths to executables in virtual environments (#17954)
## Summary Updates the `--python` flag to accept Python executables in virtual environments. Notably, we do not query the executable and it _must_ be in a canonical location in a virtual environment. This is pretty naive, but solves for the trivial case of `ty check --python .venv/bin/python3` which will be a common mistake (and `ty check --python $(which python)`) I explored this while trying to understand Python discovery in ty in service of astral-sh/ty#272, I'm not attached to it, but figure it's worth sharing. As an alternative, we can add more variants to the `SearchPathValidationError` and just improve the _error_ message, i.e., by hinting that this looks like a virtual environment and suggesting the concrete alternative path they should provide. We'll probably want to do that for some other cases anyway (e.g., `3.13` as described in the linked issue) This functionality is also briefly mentioned in astral-sh/ty#193 Closes astral-sh/ty#318 ## Test Plan e.g., ``` uv run ty check --python .venv/bin/python3 ``` needs test coverage still
1 parent d545b5b commit 6b64630

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

crates/ty/docs/cli.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ ty check [OPTIONS] [PATH]...
5656
</ul></dd><dt id="ty-check--project"><a href="#ty-check--project"><code>--project</code></a> <i>project</i></dt><dd><p>Run the command within the given project directory.</p>
5757
<p>All <code>pyproject.toml</code> files will be discovered by walking up the directory tree from the given project directory, as will the project's virtual environment (<code>.venv</code>) unless the <code>venv-path</code> option is set.</p>
5858
<p>Other command-line arguments (such as relative paths) will be resolved relative to the current working directory.</p>
59-
</dd><dt id="ty-check--python"><a href="#ty-check--python"><code>--python</code></a> <i>path</i></dt><dd><p>Path to the Python installation from which ty resolves type information and third-party dependencies.</p>
60-
<p>If not specified, ty will look at the <code>VIRTUAL_ENV</code> environment variable.</p>
61-
<p>ty will search in the path's <code>site-packages</code> directories for type information and third-party imports.</p>
62-
<p>This option is commonly used to specify the path to a virtual environment.</p>
59+
</dd><dt id="ty-check--python"><a href="#ty-check--python"><code>--python</code></a> <i>path</i></dt><dd><p>Path to the Python environment.</p>
60+
<p>ty uses the Python environment to resolve type information and third-party dependencies.</p>
61+
<p>If not specified, ty will attempt to infer it from the <code>VIRTUAL_ENV</code> environment variable or discover a <code>.venv</code> directory in the project root or working directory.</p>
62+
<p>If a path to a Python interpreter is provided, e.g., <code>.venv/bin/python3</code>, ty will attempt to find an environment two directories up from the interpreter's path, e.g., <code>.venv</code>. At this time, ty does not invoke the interpreter to determine the location of the environment. This means that ty will not resolve dynamic executables such as a shim.</p>
63+
<p>ty will search in the resolved environments's <code>site-packages</code> directories for type information and third-party imports.</p>
6364
</dd><dt id="ty-check--python-platform"><a href="#ty-check--python-platform"><code>--python-platform</code></a>, <code>--platform</code> <i>platform</i></dt><dd><p>Target platform to assume when resolving types.</p>
6465
<p>This is used to specialize the type of <code>sys.platform</code> and will affect the visibility of platform-specific functions and attributes. If the value is set to <code>all</code>, no assumptions are made about the target platform. If unspecified, the current system's platform will be used.</p>
6566
</dd><dt id="ty-check--python-version"><a href="#ty-check--python-version"><code>--python-version</code></a>, <code>--target-version</code> <i>version</i></dt><dd><p>Python version to assume when resolving types</p>

crates/ty/src/args.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ pub(crate) struct CheckCommand {
5151
#[arg(long, value_name = "PROJECT")]
5252
pub(crate) project: Option<SystemPathBuf>,
5353

54-
/// Path to the Python installation from which ty resolves type information and third-party dependencies.
54+
/// Path to the Python environment.
5555
///
56-
/// If not specified, ty will look at the `VIRTUAL_ENV` environment variable.
56+
/// ty uses the Python environment to resolve type information and third-party dependencies.
5757
///
58-
/// ty will search in the path's `site-packages` directories for type information and
59-
/// third-party imports.
58+
/// If not specified, ty will attempt to infer it from the `VIRTUAL_ENV` environment variable or
59+
/// discover a `.venv` directory in the project root or working directory.
6060
///
61-
/// This option is commonly used to specify the path to a virtual environment.
61+
/// If a path to a Python interpreter is provided, e.g., `.venv/bin/python3`, ty will attempt to
62+
/// find an environment two directories up from the interpreter's path, e.g., `.venv`. At this
63+
/// time, ty does not invoke the interpreter to determine the location of the environment. This
64+
/// means that ty will not resolve dynamic executables such as a shim.
65+
///
66+
/// ty will search in the resolved environments's `site-packages` directories for type
67+
/// information and third-party imports.
6268
#[arg(long, value_name = "PATH")]
6369
pub(crate) python: Option<SystemPathBuf>,
6470

crates/ty_python_semantic/src/module_resolver/resolver.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,33 @@ impl SearchPaths {
247247
.and_then(|env| env.site_packages_directories(system))?
248248
}
249249

250+
PythonPath::Resolve(target, origin) => {
251+
tracing::debug!("Resolving {origin}: {target}");
252+
253+
let root = system
254+
// If given a file, assume it's a Python executable, e.g., `.venv/bin/python3`,
255+
// and search for a virtual environment in the root directory. Ideally, we'd
256+
// invoke the target to determine `sys.prefix` here, but that's more complicated
257+
// and may be deferred to uv.
258+
.is_file(target)
259+
.then(|| target.as_path())
260+
.take_if(|target| {
261+
// Avoid using the target if it doesn't look like a Python executable, e.g.,
262+
// to deny cases like `.venv/bin/foo`
263+
target
264+
.file_name()
265+
.is_some_and(|name| name.starts_with("python"))
266+
})
267+
.and_then(SystemPath::parent)
268+
.and_then(SystemPath::parent)
269+
// If not a file, use the path as given and allow let `PythonEnvironment::new`
270+
// handle the error.
271+
.unwrap_or(target);
272+
273+
PythonEnvironment::new(root, *origin, system)
274+
.and_then(|venv| venv.site_packages_directories(system))?
275+
}
276+
250277
PythonPath::Discover(root) => {
251278
tracing::debug!("Discovering virtual environment in `{root}`");
252279
let virtual_env_path = discover_venv_in(db.system(), root);

crates/ty_python_semantic/src/program.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ pub enum PythonPath {
145145
/// [`sys.prefix`]: https://docs.python.org/3/library/sys.html#sys.prefix
146146
SysPrefix(SystemPathBuf, SysPrefixPathOrigin),
147147

148+
/// Resolve a path to an executable (or environment directory) into a usable environment.
149+
Resolve(SystemPathBuf, SysPrefixPathOrigin),
150+
148151
/// Tries to discover a virtual environment in the given path.
149152
Discover(SystemPathBuf),
150153

@@ -161,6 +164,6 @@ impl PythonPath {
161164
}
162165

163166
pub fn from_cli_flag(path: SystemPathBuf) -> Self {
164-
Self::SysPrefix(path, SysPrefixPathOrigin::PythonCliFlag)
167+
Self::Resolve(path, SysPrefixPathOrigin::PythonCliFlag)
165168
}
166169
}

crates/ty_test/src/config.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,17 @@ pub(crate) struct Environment {
6868
/// Additional search paths to consider when resolving modules.
6969
pub(crate) extra_paths: Option<Vec<SystemPathBuf>>,
7070

71-
/// Path to the Python installation from which ty resolves type information and third-party dependencies.
71+
/// Path to the Python environment.
7272
///
73-
/// ty will search in the path's `site-packages` directories for type information and
74-
/// third-party imports.
73+
/// ty uses the Python environment to resolve type information and third-party dependencies.
7574
///
76-
/// This option is commonly used to specify the path to a virtual environment.
75+
/// If a path to a Python interpreter is provided, e.g., `.venv/bin/python3`, ty will attempt to
76+
/// find an environment two directories up from the interpreter's path, e.g., `.venv`. At this
77+
/// time, ty does not invoke the interpreter to determine the location of the environment. This
78+
/// means that ty will not resolve dynamic executables such as a shim.
79+
///
80+
/// ty will search in the resolved environment's `site-packages` directories for type
81+
/// information and third-party imports.
7782
#[serde(skip_serializing_if = "Option::is_none")]
7883
pub python: Option<SystemPathBuf>,
7984
}

0 commit comments

Comments
 (0)