mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
[red-knot] Move the vendored typeshed stubs to the module resolver crate (#11966)
This commit is contained in:
parent
736a4ead14
commit
3277d031f8
587 changed files with 78 additions and 73 deletions
14
.github/workflows/sync_typeshed.yaml
vendored
14
.github/workflows/sync_typeshed.yaml
vendored
|
@ -37,13 +37,13 @@ jobs:
|
||||||
- name: Sync typeshed
|
- name: Sync typeshed
|
||||||
id: sync
|
id: sync
|
||||||
run: |
|
run: |
|
||||||
rm -rf ruff/crates/red_knot/vendor/typeshed
|
rm -rf ruff/crates/red_knot_module_resolver/vendor/typeshed
|
||||||
mkdir ruff/crates/red_knot/vendor/typeshed
|
mkdir ruff/crates/red_knot_module_resolver/vendor/typeshed
|
||||||
cp typeshed/README.md ruff/crates/red_knot/vendor/typeshed
|
cp typeshed/README.md ruff/crates/red_knot_module_resolver/vendor/typeshed
|
||||||
cp typeshed/LICENSE ruff/crates/red_knot/vendor/typeshed
|
cp typeshed/LICENSE ruff/crates/red_knot_module_resolver/vendor/typeshed
|
||||||
cp -r typeshed/stdlib ruff/crates/red_knot/vendor/typeshed/stdlib
|
cp -r typeshed/stdlib ruff/crates/red_knot_module_resolver/vendor/typeshed/stdlib
|
||||||
rm -rf ruff/crates/red_knot/vendor/typeshed/stdlib/@tests
|
rm -rf ruff/crates/red_knot_module_resolver/vendor/typeshed/stdlib/@tests
|
||||||
git -C typeshed rev-parse HEAD > ruff/crates/red_knot/vendor/typeshed/source_commit.txt
|
git -C typeshed rev-parse HEAD > ruff/crates/red_knot_module_resolver/vendor/typeshed/source_commit.txt
|
||||||
- name: Commit the changes
|
- name: Commit the changes
|
||||||
id: commit
|
id: commit
|
||||||
if: ${{ steps.sync.outcome == 'success' }}
|
if: ${{ steps.sync.outcome == 'success' }}
|
||||||
|
|
|
@ -2,7 +2,7 @@ fail_fast: true
|
||||||
|
|
||||||
exclude: |
|
exclude: |
|
||||||
(?x)^(
|
(?x)^(
|
||||||
crates/red_knot/vendor/.*|
|
crates/red_knot_module_resolver/vendor/.*|
|
||||||
crates/ruff_linter/resources/.*|
|
crates/ruff_linter/resources/.*|
|
||||||
crates/ruff_linter/src/rules/.*/snapshots/.*|
|
crates/ruff_linter/src/rules/.*/snapshots/.*|
|
||||||
crates/ruff/resources/.*|
|
crates/ruff/resources/.*|
|
||||||
|
|
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1991,8 +1991,6 @@ dependencies = [
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"tracing-tree",
|
"tracing-tree",
|
||||||
"walkdir",
|
|
||||||
"zip",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2006,6 +2004,8 @@ dependencies = [
|
||||||
"smol_str",
|
"smol_str",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
"walkdir",
|
||||||
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[files]
|
[files]
|
||||||
# https://github.com/crate-ci/typos/issues/868
|
# https://github.com/crate-ci/typos/issues/868
|
||||||
extend-exclude = ["crates/red_knot/vendor/**/*", "**/resources/**/*", "**/snapshots/**/*"]
|
extend-exclude = ["crates/red_knot_module_resolver/vendor/**/*", "**/resources/**/*", "**/snapshots/**/*"]
|
||||||
|
|
||||||
[default.extend-words]
|
[default.extend-words]
|
||||||
"arange" = "arange" # e.g. `numpy.arange`
|
"arange" = "arange" # e.g. `numpy.arange`
|
||||||
|
|
|
@ -37,11 +37,6 @@ smol_str = { version = "0.2.1" }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
tracing-subscriber = { workspace = true }
|
tracing-subscriber = { workspace = true }
|
||||||
tracing-tree = { workspace = true }
|
tracing-tree = { workspace = true }
|
||||||
zip = { workspace = true }
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
zip = { workspace = true }
|
|
||||||
walkdir = { workspace = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
insta = { workspace = true }
|
insta = { workspace = true }
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
# Red Knot
|
|
||||||
|
|
||||||
The Red Knot crate contains code working towards multifile analysis, type inference and, ultimately, type-checking. It's very much a work in progress for now.
|
|
||||||
|
|
||||||
## Vendored types for the stdlib
|
|
||||||
|
|
||||||
Red Knot vendors [typeshed](https://github.com/python/typeshed)'s stubs for the standard library. The vendored stubs can be found in `crates/red_knot/vendor/typeshed`. The file `crates/red_knot/vendor/typeshed/source_commit.txt` tells you the typeshed commit that our vendored stdlib stubs currently correspond to.
|
|
||||||
|
|
||||||
The typeshed stubs are updated every two weeks via an automated PR using the `sync_typeshed.yaml` workflow in the `.github/workflows` directory. This workflow can also be triggered at any time via [workflow dispatch](https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow#running-a-workflow).
|
|
|
@ -763,11 +763,8 @@ impl PackageKind {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::io::{Cursor, Read};
|
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use zip::ZipArchive;
|
|
||||||
|
|
||||||
use crate::db::tests::TestDb;
|
use crate::db::tests::TestDb;
|
||||||
use crate::db::SourceDb;
|
use crate::db::SourceDb;
|
||||||
|
@ -919,28 +916,6 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typeshed_zip_created_at_build_time() -> anyhow::Result<()> {
|
|
||||||
// The file path here is hardcoded in this crate's `build.rs` script.
|
|
||||||
// Luckily this crate will fail to build if this file isn't available at build time.
|
|
||||||
const TYPESHED_ZIP_BYTES: &[u8] =
|
|
||||||
include_bytes!(concat!(env!("OUT_DIR"), "/zipped_typeshed.zip"));
|
|
||||||
|
|
||||||
let mut typeshed_zip_archive = ZipArchive::new(Cursor::new(TYPESHED_ZIP_BYTES))?;
|
|
||||||
|
|
||||||
let path_to_functools = Path::new("stdlib").join("functools.pyi");
|
|
||||||
let mut functools_module_stub = typeshed_zip_archive
|
|
||||||
.by_name(path_to_functools.to_str().unwrap())
|
|
||||||
.unwrap();
|
|
||||||
assert!(functools_module_stub.is_file());
|
|
||||||
|
|
||||||
let mut functools_module_stub_source = String::new();
|
|
||||||
functools_module_stub.read_to_string(&mut functools_module_stub_source)?;
|
|
||||||
|
|
||||||
assert!(functools_module_stub_source.contains("def update_wrapper("));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn resolve_package() -> anyhow::Result<()> {
|
fn resolve_package() -> anyhow::Result<()> {
|
||||||
let TestCase {
|
let TestCase {
|
||||||
|
|
|
@ -320,28 +320,30 @@ mod tests {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
const ONE: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(1) };
|
const ONE: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(1) };
|
||||||
|
|
||||||
#[test]
|
// TODO(Alex): move VERSIONS parsing logic to red_knot_module_resolver, add this test back
|
||||||
fn can_parse_vendored_versions_file() {
|
//
|
||||||
let versions_data = include_str!(concat!(
|
// #[test]
|
||||||
env!("CARGO_MANIFEST_DIR"),
|
// fn can_parse_vendored_versions_file() {
|
||||||
"/vendor/typeshed/stdlib/VERSIONS"
|
// let versions_data = include_str!(concat!(
|
||||||
));
|
// env!("CARGO_MANIFEST_DIR"),
|
||||||
|
// "/vendor/typeshed/stdlib/VERSIONS"
|
||||||
|
// ));
|
||||||
|
|
||||||
let versions = TypeshedVersions::from_str(versions_data).unwrap();
|
// let versions = TypeshedVersions::from_str(versions_data).unwrap();
|
||||||
assert!(versions.len() > 100);
|
// assert!(versions.len() > 100);
|
||||||
assert!(versions.len() < 1000);
|
// assert!(versions.len() < 1000);
|
||||||
|
|
||||||
assert!(versions.contains_module("asyncio"));
|
// assert!(versions.contains_module("asyncio"));
|
||||||
assert!(versions.module_exists_on_version("asyncio", SupportedPyVersion::Py310));
|
// assert!(versions.module_exists_on_version("asyncio", SupportedPyVersion::Py310));
|
||||||
|
|
||||||
assert!(versions.contains_module("asyncio.staggered"));
|
// assert!(versions.contains_module("asyncio.staggered"));
|
||||||
assert!(versions.module_exists_on_version("asyncio.staggered", SupportedPyVersion::Py38));
|
// assert!(versions.module_exists_on_version("asyncio.staggered", SupportedPyVersion::Py38));
|
||||||
assert!(!versions.module_exists_on_version("asyncio.staggered", SupportedPyVersion::Py37));
|
// assert!(!versions.module_exists_on_version("asyncio.staggered", SupportedPyVersion::Py37));
|
||||||
|
|
||||||
assert!(versions.contains_module("audioop"));
|
// assert!(versions.contains_module("audioop"));
|
||||||
assert!(versions.module_exists_on_version("audioop", SupportedPyVersion::Py312));
|
// assert!(versions.module_exists_on_version("audioop", SupportedPyVersion::Py312));
|
||||||
assert!(!versions.module_exists_on_version("audioop", SupportedPyVersion::Py313));
|
// assert!(!versions.module_exists_on_version("audioop", SupportedPyVersion::Py313));
|
||||||
}
|
// }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_parse_mock_versions_file() {
|
fn can_parse_mock_versions_file() {
|
||||||
|
|
|
@ -17,6 +17,11 @@ ruff_python_stdlib = { workspace = true }
|
||||||
salsa = { workspace = true }
|
salsa = { workspace = true }
|
||||||
smol_str = { workspace = true }
|
smol_str = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
|
zip = { workspace = true }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
walkdir = { workspace = true }
|
||||||
|
zip = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
|
|
9
crates/red_knot_module_resolver/README.md
Normal file
9
crates/red_knot_module_resolver/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Red Knot
|
||||||
|
|
||||||
|
A work-in-progress multifile module resolver for Ruff.
|
||||||
|
|
||||||
|
## Vendored types for the stdlib
|
||||||
|
|
||||||
|
This crate vendors [typeshed](https://github.com/python/typeshed)'s stubs for the standard library. The vendored stubs can be found in `crates/red_knot_module_resolver/vendor/typeshed`. The file `crates/red_knot_module_resolver/vendor/typeshed/source_commit.txt` tells you the typeshed commit that our vendored stdlib stubs currently correspond to.
|
||||||
|
|
||||||
|
The typeshed stubs are updated every two weeks via an automated PR using the `sync_typeshed.yaml` workflow in the `.github/workflows` directory. This workflow can also be triggered at any time via [workflow dispatch](https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow#running-a-workflow).
|
|
@ -3,7 +3,7 @@
|
||||||
//!
|
//!
|
||||||
//! This script should be automatically run at build time
|
//! This script should be automatically run at build time
|
||||||
//! whenever the script itself changes, or whenever any files
|
//! whenever the script itself changes, or whenever any files
|
||||||
//! in `crates/red_knot/vendor/typeshed` change.
|
//! in `crates/red_knot_module_resolver/vendor/typeshed` change.
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
|
@ -1,6 +1,7 @@
|
||||||
mod db;
|
mod db;
|
||||||
mod module;
|
mod module;
|
||||||
mod resolver;
|
mod resolver;
|
||||||
|
mod typeshed;
|
||||||
|
|
||||||
pub use db::{Db, Jar};
|
pub use db::{Db, Jar};
|
||||||
pub use module::{ModuleKind, ModuleName};
|
pub use module::{ModuleKind, ModuleName};
|
||||||
|
|
27
crates/red_knot_module_resolver/src/typeshed.rs
Normal file
27
crates/red_knot_module_resolver/src/typeshed.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::io::{self, Read};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn typeshed_zip_created_at_build_time() -> anyhow::Result<()> {
|
||||||
|
// The file path here is hardcoded in this crate's `build.rs` script.
|
||||||
|
// Luckily this crate will fail to build if this file isn't available at build time.
|
||||||
|
const TYPESHED_ZIP_BYTES: &[u8] =
|
||||||
|
include_bytes!(concat!(env!("OUT_DIR"), "/zipped_typeshed.zip"));
|
||||||
|
|
||||||
|
let mut typeshed_zip_archive = zip::ZipArchive::new(io::Cursor::new(TYPESHED_ZIP_BYTES))?;
|
||||||
|
|
||||||
|
let path_to_functools = Path::new("stdlib").join("functools.pyi");
|
||||||
|
let mut functools_module_stub = typeshed_zip_archive
|
||||||
|
.by_name(path_to_functools.to_str().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
assert!(functools_module_stub.is_file());
|
||||||
|
|
||||||
|
let mut functools_module_stub_source = String::new();
|
||||||
|
functools_module_stub.read_to_string(&mut functools_module_stub_source)?;
|
||||||
|
|
||||||
|
assert!(functools_module_stub_source.contains("def update_wrapper("));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue