From 35f55f482293cb9c443ca83da11ec1bb07d43eb7 Mon Sep 17 00:00:00 2001 From: David Peter Date: Thu, 9 Jan 2025 10:28:59 +0100 Subject: [PATCH 1/2] [red-knot] Typeshed sync: use copy instead of symlink --- .github/workflows/sync_typeshed.yaml | 15 ++++++++--- .../typeshed/stdlib/knot_extensions.pyi | 27 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) mode change 120000 => 100644 crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi diff --git a/.github/workflows/sync_typeshed.yaml b/.github/workflows/sync_typeshed.yaml index 20925a523e370..074d752faa883 100644 --- a/.github/workflows/sync_typeshed.yaml +++ b/.github/workflows/sync_typeshed.yaml @@ -46,10 +46,19 @@ jobs: cp -r typeshed/stdlib ruff/crates/red_knot_vendored/vendor/typeshed/stdlib rm -rf ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/@tests git -C typeshed rev-parse HEAD > ruff/crates/red_knot_vendored/vendor/typeshed/source_commit.txt + # Patch the typeshed stubs to include `knot_extensions` - ln -s ../../../knot_extensions/knot_extensions.pyi ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/ - echo "# Patch applied for red_knot:" >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS - echo "knot_extensions: 3.0-" >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS + source="crates/red_knot_vendored/knot_extensions/knot_extensions.pyi" + target="crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi" + ( + echo "# WARNING: This file is generated by the typeshed sync workflow. Do not edit it manually." + echo "# Edit the source file at '$source' instead and then copy it over to this file." + ) > "ruff/$target" + cat "ruff/$source" >> "ruff/$target" + ( + echo "# Patch applied for red_knot:" + echo "knot_extensions: 3.0-" + ) >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS - name: Commit the changes id: commit if: ${{ steps.sync.outcome == 'success' }} diff --git a/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi b/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi deleted file mode 120000 index 8e6b527c81002..0000000000000 --- a/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi +++ /dev/null @@ -1 +0,0 @@ -../../../knot_extensions/knot_extensions.pyi \ No newline at end of file diff --git a/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi b/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi new file mode 100644 index 0000000000000..d7d8bf2e2428c --- /dev/null +++ b/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi @@ -0,0 +1,26 @@ +# WARNING: This file is generated by the typeshed sync workflow. Do not edit it manually. +# Edit the source file at 'crates/red_knot_vendored/knot_extensions/knot_extensions.pyi' instead. +from typing import _SpecialForm, Any, LiteralString + +# Special operations +def static_assert(condition: object, msg: LiteralString | None = None) -> None: ... + +# Types +Unknown = object() + +# Special forms +Not: _SpecialForm +Intersection: _SpecialForm +TypeOf: _SpecialForm + +# Predicates on types +# +# Ideally, these would be annotated using `TypeForm`, but that has not been +# standardized yet (https://peps.python.org/pep-0747). +def is_equivalent_to(type_a: Any, type_b: Any) -> bool: ... +def is_subtype_of(type_derived: Any, typ_ebase: Any) -> bool: ... +def is_assignable_to(type_target: Any, type_source: Any) -> bool: ... +def is_disjoint_from(type_a: Any, type_b: Any) -> bool: ... +def is_fully_static(type: Any) -> bool: ... +def is_singleton(type: Any) -> bool: ... +def is_single_valued(type: Any) -> bool: ... From 974963316853d9b87de0f297c2fbc84dfdeeac5b Mon Sep 17 00:00:00 2001 From: David Peter Date: Thu, 9 Jan 2025 10:57:00 +0100 Subject: [PATCH 2/2] Use build.rs script instead of workflow --- .github/workflows/sync_typeshed.yaml | 13 ---------- crates/red_knot_vendored/build.rs | 17 ++++++++++-- .../vendor/typeshed/stdlib/VERSIONS | 2 -- .../typeshed/stdlib/knot_extensions.pyi | 26 ------------------- 4 files changed, 15 insertions(+), 43 deletions(-) delete mode 100644 crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi diff --git a/.github/workflows/sync_typeshed.yaml b/.github/workflows/sync_typeshed.yaml index 074d752faa883..d1785034e1ca4 100644 --- a/.github/workflows/sync_typeshed.yaml +++ b/.github/workflows/sync_typeshed.yaml @@ -46,19 +46,6 @@ jobs: cp -r typeshed/stdlib ruff/crates/red_knot_vendored/vendor/typeshed/stdlib rm -rf ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/@tests git -C typeshed rev-parse HEAD > ruff/crates/red_knot_vendored/vendor/typeshed/source_commit.txt - - # Patch the typeshed stubs to include `knot_extensions` - source="crates/red_knot_vendored/knot_extensions/knot_extensions.pyi" - target="crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi" - ( - echo "# WARNING: This file is generated by the typeshed sync workflow. Do not edit it manually." - echo "# Edit the source file at '$source' instead and then copy it over to this file." - ) > "ruff/$target" - cat "ruff/$source" >> "ruff/$target" - ( - echo "# Patch applied for red_knot:" - echo "knot_extensions: 3.0-" - ) >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS - name: Commit the changes id: commit if: ${{ steps.sync.outcome == 'success' }} diff --git a/crates/red_knot_vendored/build.rs b/crates/red_knot_vendored/build.rs index 08ad3418bff8c..965dd34025ec3 100644 --- a/crates/red_knot_vendored/build.rs +++ b/crates/red_knot_vendored/build.rs @@ -6,6 +6,7 @@ //! in `crates/red_knot_vendored/vendor/typeshed` change. use std::fs::File; +use std::io::Write; use std::path::Path; use path_slash::PathExt; @@ -14,6 +15,7 @@ use zip::write::{FileOptions, ZipWriter}; use zip::CompressionMethod; const TYPESHED_SOURCE_DIR: &str = "vendor/typeshed"; +const KNOT_EXTENSIONS_STUBS: &str = "knot_extensions/knot_extensions.pyi"; const TYPESHED_ZIP_LOCATION: &str = "/zipped_typeshed.zip"; /// Recursively zip the contents of an entire directory. @@ -55,9 +57,14 @@ fn zip_dir(directory_path: &str, writer: File) -> ZipResult { // Some unzip tools unzip files with directory paths correctly, some do not! if absolute_path.is_file() { println!("adding file {absolute_path:?} as {normalized_relative_path:?} ..."); - zip.start_file(normalized_relative_path, options)?; + zip.start_file(&*normalized_relative_path, options)?; let mut f = File::open(absolute_path)?; std::io::copy(&mut f, &mut zip).unwrap(); + + // Patch the VERSIONS file to make `knot_extensions` available + if normalized_relative_path == "stdlib/VERSIONS" { + writeln!(&mut zip, "knot_extensions: 3.0-")?; + } } else if !normalized_relative_path.is_empty() { // Only if not root! Avoids path spec / warning // and mapname conversion failed error on unzip @@ -65,11 +72,17 @@ fn zip_dir(directory_path: &str, writer: File) -> ZipResult { zip.add_directory(normalized_relative_path, options)?; } } + + // Patch typeshed and add the stubs for the `knot_extensions` module + println!("adding file {KNOT_EXTENSIONS_STUBS} as stdlib/knot_extensions.pyi ..."); + zip.start_file("stdlib/knot_extensions.pyi", options)?; + let mut f = File::open(KNOT_EXTENSIONS_STUBS)?; + std::io::copy(&mut f, &mut zip).unwrap(); + zip.finish() } fn main() { - println!("cargo::rerun-if-changed={TYPESHED_SOURCE_DIR}"); assert!( Path::new(TYPESHED_SOURCE_DIR).is_dir(), "Where is typeshed?" diff --git a/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS b/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS index 6cb47d4bfeb13..3c6898dc1a777 100644 --- a/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS +++ b/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS @@ -341,5 +341,3 @@ zipfile._path: 3.12- zipimport: 3.0- zlib: 3.0- zoneinfo: 3.9- -# Patch applied for red_knot: -knot_extensions: 3.0- diff --git a/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi b/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi deleted file mode 100644 index d7d8bf2e2428c..0000000000000 --- a/crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi +++ /dev/null @@ -1,26 +0,0 @@ -# WARNING: This file is generated by the typeshed sync workflow. Do not edit it manually. -# Edit the source file at 'crates/red_knot_vendored/knot_extensions/knot_extensions.pyi' instead. -from typing import _SpecialForm, Any, LiteralString - -# Special operations -def static_assert(condition: object, msg: LiteralString | None = None) -> None: ... - -# Types -Unknown = object() - -# Special forms -Not: _SpecialForm -Intersection: _SpecialForm -TypeOf: _SpecialForm - -# Predicates on types -# -# Ideally, these would be annotated using `TypeForm`, but that has not been -# standardized yet (https://peps.python.org/pep-0747). -def is_equivalent_to(type_a: Any, type_b: Any) -> bool: ... -def is_subtype_of(type_derived: Any, typ_ebase: Any) -> bool: ... -def is_assignable_to(type_target: Any, type_source: Any) -> bool: ... -def is_disjoint_from(type_a: Any, type_b: Any) -> bool: ... -def is_fully_static(type: Any) -> bool: ... -def is_singleton(type: Any) -> bool: ... -def is_single_valued(type: Any) -> bool: ...