Compare commits
1 Commits
podman_mou
...
pythonplus
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f97547b5f |
@@ -1,10 +1,3 @@
|
|||||||
[alias]
|
[alias]
|
||||||
dev = "run --package ruff_dev --bin ruff_dev"
|
dev = "run --package ruff_dev --bin ruff_dev"
|
||||||
benchmark = "bench -p ruff_benchmark --bench linter --bench formatter --"
|
benchmark = "bench -p ruff_benchmark --bench linter --bench formatter --"
|
||||||
|
|
||||||
# statically link the C runtime so the executable does not depend on
|
|
||||||
# that shared/dynamic library.
|
|
||||||
#
|
|
||||||
# See: https://github.com/astral-sh/ruff/issues/11503
|
|
||||||
[target.'cfg(all(target_env="msvc", target_os = "windows"))']
|
|
||||||
rustflags = ["-C", "target-feature=+crt-static"]
|
|
||||||
|
|||||||
6
.gitattributes
vendored
6
.gitattributes
vendored
@@ -8,11 +8,5 @@ crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_3.py text eol=crlf
|
|||||||
crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples_crlf.py text eol=crlf
|
crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples_crlf.py text eol=crlf
|
||||||
crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_crlf.py.snap text eol=crlf
|
crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_crlf.py.snap text eol=crlf
|
||||||
|
|
||||||
crates/ruff_python_parser/resources/invalid/re_lexing/line_continuation_windows_eol.py text eol=crlf
|
|
||||||
crates/ruff_python_parser/resources/invalid/re_lex_logical_token_windows_eol.py text eol=crlf
|
|
||||||
crates/ruff_python_parser/resources/invalid/re_lex_logical_token_mac_eol.py text eol=cr
|
|
||||||
|
|
||||||
crates/ruff_python_parser/resources/inline linguist-generated=true
|
|
||||||
|
|
||||||
ruff.schema.json linguist-generated=true text=auto eol=lf
|
ruff.schema.json linguist-generated=true text=auto eol=lf
|
||||||
*.md.snap linguist-language=Markdown
|
*.md.snap linguist-language=Markdown
|
||||||
|
|||||||
11
.github/CODEOWNERS
vendored
11
.github/CODEOWNERS
vendored
@@ -5,16 +5,11 @@
|
|||||||
# - The '*' pattern is global owners.
|
# - The '*' pattern is global owners.
|
||||||
# - Order is important. The last matching pattern has the most precedence.
|
# - Order is important. The last matching pattern has the most precedence.
|
||||||
|
|
||||||
/crates/ruff_notebook/ @dhruvmanila
|
# Jupyter
|
||||||
|
/crates/ruff_linter/src/jupyter/ @dhruvmanila
|
||||||
/crates/ruff_formatter/ @MichaReiser
|
/crates/ruff_formatter/ @MichaReiser
|
||||||
/crates/ruff_python_formatter/ @MichaReiser
|
/crates/ruff_python_formatter/ @MichaReiser
|
||||||
/crates/ruff_python_parser/ @MichaReiser @dhruvmanila
|
/crates/ruff_python_parser/ @MichaReiser
|
||||||
|
|
||||||
# flake8-pyi
|
# flake8-pyi
|
||||||
/crates/ruff_linter/src/rules/flake8_pyi/ @AlexWaygood
|
/crates/ruff_linter/src/rules/flake8_pyi/ @AlexWaygood
|
||||||
|
|
||||||
# Script for fuzzing the parser
|
|
||||||
/scripts/fuzz-parser/ @AlexWaygood
|
|
||||||
|
|
||||||
# red-knot
|
|
||||||
/crates/red_knot/ @carljm @MichaReiser
|
|
||||||
|
|||||||
21
.github/dependabot.yml
vendored
Normal file
21
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "weekly"
|
||||||
|
labels: ["internal"]
|
||||||
|
groups:
|
||||||
|
actions:
|
||||||
|
patterns:
|
||||||
|
- "*"
|
||||||
|
ignore:
|
||||||
|
# The latest versions of these are not compatible with our release workflow
|
||||||
|
- dependency-name: "actions/upload-artifact"
|
||||||
|
- dependency-name: "actions/download-artifact"
|
||||||
|
|
||||||
|
- package-ecosystem: "cargo"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "weekly"
|
||||||
|
labels: ["internal"]
|
||||||
86
.github/renovate.json5
vendored
86
.github/renovate.json5
vendored
@@ -1,86 +0,0 @@
|
|||||||
{
|
|
||||||
$schema: "https://docs.renovatebot.com/renovate-schema.json",
|
|
||||||
dependencyDashboard: true,
|
|
||||||
suppressNotifications: ["prEditedNotification"],
|
|
||||||
extends: ["config:recommended"],
|
|
||||||
labels: ["internal"],
|
|
||||||
schedule: ["before 4am on Monday"],
|
|
||||||
semanticCommits: "disabled",
|
|
||||||
separateMajorMinor: false,
|
|
||||||
prHourlyLimit: 10,
|
|
||||||
enabledManagers: ["github-actions", "pre-commit", "cargo", "pep621", "npm"],
|
|
||||||
cargo: {
|
|
||||||
// See https://docs.renovatebot.com/configuration-options/#rangestrategy
|
|
||||||
rangeStrategy: "update-lockfile",
|
|
||||||
},
|
|
||||||
pep621: {
|
|
||||||
fileMatch: ["^(python|scripts)/.*pyproject\\.toml$"],
|
|
||||||
},
|
|
||||||
npm: {
|
|
||||||
fileMatch: ["^playground/.*package\\.json$"],
|
|
||||||
},
|
|
||||||
"pre-commit": {
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
packageRules: [
|
|
||||||
{
|
|
||||||
// Group upload/download artifact updates, the versions are dependent
|
|
||||||
groupName: "Artifact GitHub Actions dependencies",
|
|
||||||
matchManagers: ["github-actions"],
|
|
||||||
matchDatasources: ["gitea-tags", "github-tags"],
|
|
||||||
matchPackagePatterns: ["actions/.*-artifact"],
|
|
||||||
description: "Weekly update of artifact-related GitHub Actions dependencies",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// This package rule disables updates for GitHub runners:
|
|
||||||
// we'd only pin them to a specific version
|
|
||||||
// if there was a deliberate reason to do so
|
|
||||||
groupName: "GitHub runners",
|
|
||||||
matchManagers: ["github-actions"],
|
|
||||||
matchDatasources: ["github-runners"],
|
|
||||||
description: "Disable PRs updating GitHub runners (e.g. 'runs-on: macos-14')",
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// Disable updates of `zip-rs`; intentionally pinned for now due to ownership change
|
|
||||||
// See: https://github.com/astral-sh/uv/issues/3642
|
|
||||||
matchPackagePatterns: ["zip"],
|
|
||||||
matchManagers: ["cargo"],
|
|
||||||
enabled: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupName: "pre-commit dependencies",
|
|
||||||
matchManagers: ["pre-commit"],
|
|
||||||
description: "Weekly update of pre-commit dependencies",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupName: "NPM Development dependencies",
|
|
||||||
matchManagers: ["npm"],
|
|
||||||
matchDepTypes: ["devDependencies"],
|
|
||||||
description: "Weekly update of NPM development dependencies",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupName: "Monaco",
|
|
||||||
matchManagers: ["npm"],
|
|
||||||
matchPackagePatterns: ["monaco"],
|
|
||||||
description: "Weekly update of the Monaco editor",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupName: "strum",
|
|
||||||
matchManagers: ["cargo"],
|
|
||||||
matchPackagePatterns: ["strum"],
|
|
||||||
description: "Weekly update of strum dependencies",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupName: "ESLint",
|
|
||||||
matchManagers: ["npm"],
|
|
||||||
matchPackageNames: ["eslint"],
|
|
||||||
allowedVersions: "<9",
|
|
||||||
description: "Constraint ESLint to version 8 until TypeScript-eslint supports ESLint 9", // https://github.com/typescript-eslint/typescript-eslint/issues/8211
|
|
||||||
},
|
|
||||||
],
|
|
||||||
vulnerabilityAlerts: {
|
|
||||||
commitMessageSuffix: "",
|
|
||||||
labels: ["internal", "security"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
68
.github/workflows/build-docker.yml
vendored
68
.github/workflows/build-docker.yml
vendored
@@ -1,68 +0,0 @@
|
|||||||
# Build and publish a Docker image.
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a local
|
|
||||||
# artifacts job within `cargo-dist`.
|
|
||||||
#
|
|
||||||
# TODO(charlie): Ideally, the publish step would happen as a publish job within `cargo-dist`, but
|
|
||||||
# sharing the built image as an artifact between jobs is challenging.
|
|
||||||
name: "[ruff] Build Docker image"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_call:
|
|
||||||
inputs:
|
|
||||||
plan:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
pull_request:
|
|
||||||
paths:
|
|
||||||
- .github/workflows/build-docker.yml
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
docker-publish:
|
|
||||||
name: Build Docker image (ghcr.io/astral-sh/ruff)
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment:
|
|
||||||
name: release
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
|
|
||||||
- uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.repository_owner }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata (tags, labels) for Docker
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ghcr.io/astral-sh/ruff
|
|
||||||
|
|
||||||
- name: Check tag consistency
|
|
||||||
if: ${{ inputs.plan != '' && !fromJson(inputs.plan).announcement_tag_is_implicit }}
|
|
||||||
run: |
|
|
||||||
version=$(grep "version = " pyproject.toml | sed -e 's/version = "\(.*\)"/\1/g')
|
|
||||||
if [ "${{ fromJson(inputs.plan).announcement_tag }}" != "${version}" ]; then
|
|
||||||
echo "The input tag does not match the version from pyproject.toml:" >&2
|
|
||||||
echo "${{ fromJson(inputs.plan).announcement_tag }}" >&2
|
|
||||||
echo "${version}" >&2
|
|
||||||
exit 1
|
|
||||||
else
|
|
||||||
echo "Releasing ${version}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: "Build and push Docker image"
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
# Reuse the builder
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
push: ${{ inputs.plan != '' && !fromJson(inputs.plan).announcement_tag_is_implicit }}
|
|
||||||
tags: ghcr.io/astral-sh/ruff:latest,ghcr.io/astral-sh/ruff:${{ (inputs.plan != '' && fromJson(inputs.plan).announcement_tag) || 'dry-run' }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
130
.github/workflows/ci.yaml
vendored
130
.github/workflows/ci.yaml
vendored
@@ -23,8 +23,6 @@ jobs:
|
|||||||
name: "Determine changes"
|
name: "Determine changes"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
outputs:
|
||||||
# Flag that is raised when any code that affects parser is changed
|
|
||||||
parser: ${{ steps.changed.outputs.parser_any_changed }}
|
|
||||||
# Flag that is raised when any code that affects linter is changed
|
# Flag that is raised when any code that affects linter is changed
|
||||||
linter: ${{ steps.changed.outputs.linter_any_changed }}
|
linter: ${{ steps.changed.outputs.linter_any_changed }}
|
||||||
# Flag that is raised when any code that affects formatter is changed
|
# Flag that is raised when any code that affects formatter is changed
|
||||||
@@ -37,21 +35,10 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- uses: tj-actions/changed-files@v44
|
- uses: tj-actions/changed-files@v42
|
||||||
id: changed
|
id: changed
|
||||||
with:
|
with:
|
||||||
files_yaml: |
|
files_yaml: |
|
||||||
parser:
|
|
||||||
- Cargo.toml
|
|
||||||
- Cargo.lock
|
|
||||||
- crates/ruff_python_trivia/**
|
|
||||||
- crates/ruff_source_file/**
|
|
||||||
- crates/ruff_text_size/**
|
|
||||||
- crates/ruff_python_ast/**
|
|
||||||
- crates/ruff_python_parser/**
|
|
||||||
- scripts/fuzz-parser/**
|
|
||||||
- .github/workflows/ci.yaml
|
|
||||||
|
|
||||||
linter:
|
linter:
|
||||||
- Cargo.toml
|
- Cargo.toml
|
||||||
- Cargo.lock
|
- Cargo.lock
|
||||||
@@ -59,6 +46,7 @@ jobs:
|
|||||||
- "!crates/ruff_python_formatter/**"
|
- "!crates/ruff_python_formatter/**"
|
||||||
- "!crates/ruff_formatter/**"
|
- "!crates/ruff_formatter/**"
|
||||||
- "!crates/ruff_dev/**"
|
- "!crates/ruff_dev/**"
|
||||||
|
- "!crates/ruff_shrinking/**"
|
||||||
- scripts/*
|
- scripts/*
|
||||||
- python/**
|
- python/**
|
||||||
- .github/workflows/ci.yaml
|
- .github/workflows/ci.yaml
|
||||||
@@ -167,9 +155,6 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
shell: bash
|
shell: bash
|
||||||
env:
|
|
||||||
# Workaround for <https://github.com/nextest-rs/nextest/issues/1493>.
|
|
||||||
RUSTUP_WINDOWS_PATH_ADD_BIN: 1
|
|
||||||
run: |
|
run: |
|
||||||
cargo nextest run --all-features --profile ci
|
cargo nextest run --all-features --profile ci
|
||||||
cargo test --all-features --doc
|
cargo test --all-features --doc
|
||||||
@@ -196,54 +181,6 @@ jobs:
|
|||||||
cd crates/ruff_wasm
|
cd crates/ruff_wasm
|
||||||
wasm-pack test --node
|
wasm-pack test --node
|
||||||
|
|
||||||
cargo-build-release:
|
|
||||||
name: "cargo build (release)"
|
|
||||||
runs-on: macos-latest
|
|
||||||
needs: determine_changes
|
|
||||||
if: ${{ needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main' }}
|
|
||||||
timeout-minutes: 20
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: "Install Rust toolchain"
|
|
||||||
run: rustup show
|
|
||||||
- name: "Install mold"
|
|
||||||
uses: rui314/setup-mold@v1
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- name: "Build"
|
|
||||||
run: cargo build --release --locked
|
|
||||||
|
|
||||||
cargo-build-msrv:
|
|
||||||
name: "cargo build (msrv)"
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: determine_changes
|
|
||||||
if: ${{ needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main' }}
|
|
||||||
timeout-minutes: 20
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: SebRollen/toml-action@v1.2.0
|
|
||||||
id: msrv
|
|
||||||
with:
|
|
||||||
file: "Cargo.toml"
|
|
||||||
field: "workspace.package.rust-version"
|
|
||||||
- name: "Install Rust toolchain"
|
|
||||||
run: rustup default ${{ steps.msrv.outputs.value }}
|
|
||||||
- name: "Install mold"
|
|
||||||
uses: rui314/setup-mold@v1
|
|
||||||
- name: "Install cargo nextest"
|
|
||||||
uses: taiki-e/install-action@v2
|
|
||||||
with:
|
|
||||||
tool: cargo-nextest
|
|
||||||
- name: "Install cargo insta"
|
|
||||||
uses: taiki-e/install-action@v2
|
|
||||||
with:
|
|
||||||
tool: cargo-insta
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- name: "Run tests"
|
|
||||||
shell: bash
|
|
||||||
env:
|
|
||||||
NEXTEST_PROFILE: "ci"
|
|
||||||
run: cargo +${{ steps.msrv.outputs.value }} insta test --all-features --unreferenced reject --test-runner nextest
|
|
||||||
|
|
||||||
cargo-fuzz:
|
cargo-fuzz:
|
||||||
name: "cargo fuzz"
|
name: "cargo fuzz"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -257,47 +194,12 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
with:
|
with:
|
||||||
workspaces: "fuzz -> target"
|
workspaces: "fuzz -> target"
|
||||||
- name: "Install cargo-binstall"
|
- name: "Install cargo-fuzz"
|
||||||
uses: cargo-bins/cargo-binstall@main
|
uses: taiki-e/install-action@v2
|
||||||
with:
|
with:
|
||||||
tool: cargo-fuzz@0.11.2
|
tool: cargo-fuzz@0.11.2
|
||||||
- name: "Install cargo-fuzz"
|
|
||||||
# Download the latest version from quick install and not the github releases because github releases only has MUSL targets.
|
|
||||||
run: cargo binstall cargo-fuzz --force --disable-strategies crate-meta-data --no-confirm
|
|
||||||
- run: cargo fuzz build -s none
|
- run: cargo fuzz build -s none
|
||||||
|
|
||||||
fuzz-parser:
|
|
||||||
name: "Fuzz the parser"
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs:
|
|
||||||
- cargo-test-linux
|
|
||||||
- determine_changes
|
|
||||||
if: ${{ needs.determine_changes.outputs.parser == 'true' }}
|
|
||||||
timeout-minutes: 20
|
|
||||||
env:
|
|
||||||
FORCE_COLOR: 1
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
|
||||||
- name: Install uv
|
|
||||||
run: curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
||||||
- name: Install Python requirements
|
|
||||||
run: uv pip install -r scripts/fuzz-parser/requirements.txt --system
|
|
||||||
- uses: actions/download-artifact@v4
|
|
||||||
name: Download Ruff binary to test
|
|
||||||
id: download-cached-binary
|
|
||||||
with:
|
|
||||||
name: ruff
|
|
||||||
path: ruff-to-test
|
|
||||||
- name: Fuzz
|
|
||||||
run: |
|
|
||||||
# Make executable, since artifact download doesn't preserve this
|
|
||||||
chmod +x ${{ steps.download-cached-binary.outputs.download-path }}/ruff
|
|
||||||
|
|
||||||
python scripts/fuzz-parser/fuzz.py 0-500 --test-executable ${{ steps.download-cached-binary.outputs.download-path }}/ruff
|
|
||||||
|
|
||||||
scripts:
|
scripts:
|
||||||
name: "test scripts"
|
name: "test scripts"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -326,7 +228,9 @@ jobs:
|
|||||||
- determine_changes
|
- determine_changes
|
||||||
# Only runs on pull requests, since that is the only we way we can find the base version for comparison.
|
# Only runs on pull requests, since that is the only we way we can find the base version for comparison.
|
||||||
# Ecosystem check needs linter and/or formatter changes.
|
# Ecosystem check needs linter and/or formatter changes.
|
||||||
if: ${{ github.event_name == 'pull_request' && needs.determine_changes.outputs.code == 'true' }}
|
if: github.event_name == 'pull_request' && ${{
|
||||||
|
needs.determine_changes.outputs.code == 'true'
|
||||||
|
}}
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -341,7 +245,7 @@ jobs:
|
|||||||
name: ruff
|
name: ruff
|
||||||
path: target/debug
|
path: target/debug
|
||||||
|
|
||||||
- uses: dawidd6/action-download-artifact@v6
|
- uses: dawidd6/action-download-artifact@v3
|
||||||
name: Download baseline Ruff binary
|
name: Download baseline Ruff binary
|
||||||
with:
|
with:
|
||||||
name: ruff
|
name: ruff
|
||||||
@@ -433,16 +337,22 @@ jobs:
|
|||||||
name: ecosystem-result
|
name: ecosystem-result
|
||||||
path: ecosystem-result
|
path: ecosystem-result
|
||||||
|
|
||||||
cargo-shear:
|
cargo-udeps:
|
||||||
name: "cargo shear"
|
name: "cargo udeps"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: determine_changes
|
needs: determine_changes
|
||||||
if: ${{ needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main' }}
|
if: ${{ needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main' }}
|
||||||
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: cargo-bins/cargo-binstall@main
|
- name: "Install nightly Rust toolchain"
|
||||||
- run: cargo binstall --no-confirm cargo-shear
|
# Only pinned to make caching work, update freely
|
||||||
- run: cargo shear
|
run: rustup toolchain install nightly-2023-10-15
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
- name: "Install cargo-udeps"
|
||||||
|
uses: taiki-e/install-action@cargo-udeps
|
||||||
|
- name: "Run cargo-udeps"
|
||||||
|
run: cargo +nightly-2023-10-15 udeps
|
||||||
|
|
||||||
python-package:
|
python-package:
|
||||||
name: "python package"
|
name: "python package"
|
||||||
@@ -599,7 +509,7 @@ jobs:
|
|||||||
benchmarks:
|
benchmarks:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: determine_changes
|
needs: determine_changes
|
||||||
if: ${{ github.repository == 'astral-sh/ruff' && (needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main') }}
|
if: ${{ needs.determine_changes.outputs.code == 'true' || github.ref == 'refs/heads/main' }}
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout Branch"
|
- name: "Checkout Branch"
|
||||||
|
|||||||
72
.github/workflows/daily_fuzz.yaml
vendored
72
.github/workflows/daily_fuzz.yaml
vendored
@@ -1,72 +0,0 @@
|
|||||||
name: Daily parser fuzz
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
schedule:
|
|
||||||
- cron: "0 0 * * *"
|
|
||||||
pull_request:
|
|
||||||
paths:
|
|
||||||
- ".github/workflows/daily_fuzz.yaml"
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_INCREMENTAL: 0
|
|
||||||
CARGO_NET_RETRY: 10
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
RUSTUP_MAX_RETRIES: 10
|
|
||||||
PACKAGE_NAME: ruff
|
|
||||||
FORCE_COLOR: 1
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
fuzz:
|
|
||||||
name: Fuzz
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 20
|
|
||||||
# Don't run the cron job on forks:
|
|
||||||
if: ${{ github.repository == 'astral-sh/ruff' || github.event_name != 'schedule' }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: "3.12"
|
|
||||||
- name: Install uv
|
|
||||||
run: curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
||||||
- name: Install Python requirements
|
|
||||||
run: uv pip install -r scripts/fuzz-parser/requirements.txt --system
|
|
||||||
- name: "Install Rust toolchain"
|
|
||||||
run: rustup show
|
|
||||||
- name: "Install mold"
|
|
||||||
uses: rui314/setup-mold@v1
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
|
||||||
- name: Build ruff
|
|
||||||
# A debug build means the script runs slower once it gets started,
|
|
||||||
# but this is outweighed by the fact that a release build takes *much* longer to compile in CI
|
|
||||||
run: cargo build --locked
|
|
||||||
- name: Fuzz
|
|
||||||
run: python scripts/fuzz-parser/fuzz.py $(shuf -i 0-9999999999999999999 -n 1000) --test-executable target/debug/ruff
|
|
||||||
|
|
||||||
create-issue-on-failure:
|
|
||||||
name: Create an issue if the daily fuzz surfaced any bugs
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: fuzz
|
|
||||||
if: ${{ github.repository == 'astral-sh/ruff' && always() && github.event_name == 'schedule' && needs.fuzz.result == 'failure' }}
|
|
||||||
permissions:
|
|
||||||
issues: write
|
|
||||||
steps:
|
|
||||||
- uses: actions/github-script@v7
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
script: |
|
|
||||||
await github.rest.issues.create({
|
|
||||||
owner: "astral-sh",
|
|
||||||
repo: "ruff",
|
|
||||||
title: `Daily parser fuzz failed on ${new Date().toDateString()}`,
|
|
||||||
body: "Runs listed here: https://github.com/astral-sh/ruff/actions/workflows/daily_fuzz.yml",
|
|
||||||
labels: ["bug", "parser", "fuzzer"],
|
|
||||||
})
|
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
# Publish the Ruff documentation.
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a post-announce
|
|
||||||
# job within `cargo-dist`.
|
|
||||||
name: mkdocs
|
name: mkdocs
|
||||||
|
|
||||||
on:
|
on:
|
||||||
@@ -11,11 +7,8 @@ on:
|
|||||||
description: "The commit SHA, tag, or branch to publish. Uses the default branch if not specified."
|
description: "The commit SHA, tag, or branch to publish. Uses the default branch if not specified."
|
||||||
default: ""
|
default: ""
|
||||||
type: string
|
type: string
|
||||||
workflow_call:
|
release:
|
||||||
inputs:
|
types: [published]
|
||||||
plan:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
mkdocs:
|
mkdocs:
|
||||||
@@ -54,7 +47,7 @@ jobs:
|
|||||||
run: mkdocs build --strict -f mkdocs.public.yml
|
run: mkdocs build --strict -f mkdocs.public.yml
|
||||||
- name: "Deploy to Cloudflare Pages"
|
- name: "Deploy to Cloudflare Pages"
|
||||||
if: ${{ env.CF_API_TOKEN_EXISTS == 'true' }}
|
if: ${{ env.CF_API_TOKEN_EXISTS == 'true' }}
|
||||||
uses: cloudflare/wrangler-action@v3.6.1
|
uses: cloudflare/wrangler-action@v3.4.1
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ secrets.CF_API_TOKEN }}
|
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||||
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
||||||
29
.github/workflows/notify-dependents.yml
vendored
29
.github/workflows/notify-dependents.yml
vendored
@@ -1,29 +0,0 @@
|
|||||||
# Notify downstream repositories of a new release.
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a post-announce
|
|
||||||
# job within `cargo-dist`.
|
|
||||||
name: "[ruff] Notify dependents"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_call:
|
|
||||||
inputs:
|
|
||||||
plan:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
update-dependents:
|
|
||||||
name: Notify dependents
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: "Update pre-commit mirror"
|
|
||||||
uses: actions/github-script@v7
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.RUFF_PRE_COMMIT_PAT }}
|
|
||||||
script: |
|
|
||||||
github.rest.actions.createWorkflowDispatch({
|
|
||||||
owner: 'astral-sh',
|
|
||||||
repo: 'ruff-pre-commit',
|
|
||||||
workflow_id: 'main.yml',
|
|
||||||
ref: 'main',
|
|
||||||
})
|
|
||||||
@@ -1,16 +1,9 @@
|
|||||||
# Publish the Ruff playground.
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a post-announce
|
|
||||||
# job within `cargo-dist`.
|
|
||||||
name: "[Playground] Release"
|
name: "[Playground] Release"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
workflow_call:
|
release:
|
||||||
inputs:
|
types: [published]
|
||||||
plan:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_INCREMENTAL: 0
|
CARGO_INCREMENTAL: 0
|
||||||
@@ -47,7 +40,7 @@ jobs:
|
|||||||
working-directory: playground
|
working-directory: playground
|
||||||
- name: "Deploy to Cloudflare Pages"
|
- name: "Deploy to Cloudflare Pages"
|
||||||
if: ${{ env.CF_API_TOKEN_EXISTS == 'true' }}
|
if: ${{ env.CF_API_TOKEN_EXISTS == 'true' }}
|
||||||
uses: cloudflare/wrangler-action@v3.6.1
|
uses: cloudflare/wrangler-action@v3.4.1
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ secrets.CF_API_TOKEN }}
|
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||||
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
||||||
12
.github/workflows/pr-comment.yaml
vendored
12
.github/workflows/pr-comment.yaml
vendored
@@ -17,7 +17,7 @@ jobs:
|
|||||||
comment:
|
comment:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: dawidd6/action-download-artifact@v6
|
- uses: dawidd6/action-download-artifact@v3
|
||||||
name: Download pull request number
|
name: Download pull request number
|
||||||
with:
|
with:
|
||||||
name: pr-number
|
name: pr-number
|
||||||
@@ -32,7 +32,7 @@ jobs:
|
|||||||
echo "pr-number=$(<pr-number)" >> $GITHUB_OUTPUT
|
echo "pr-number=$(<pr-number)" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- uses: dawidd6/action-download-artifact@v6
|
- uses: dawidd6/action-download-artifact@v3
|
||||||
name: "Download ecosystem results"
|
name: "Download ecosystem results"
|
||||||
id: download-ecosystem-result
|
id: download-ecosystem-result
|
||||||
if: steps.pr-number.outputs.pr-number
|
if: steps.pr-number.outputs.pr-number
|
||||||
@@ -48,14 +48,6 @@ jobs:
|
|||||||
id: generate-comment
|
id: generate-comment
|
||||||
if: steps.download-ecosystem-result.outputs.found_artifact == 'true'
|
if: steps.download-ecosystem-result.outputs.found_artifact == 'true'
|
||||||
run: |
|
run: |
|
||||||
# Guard against malicious ecosystem results that symlink to a secret
|
|
||||||
# file on this runner
|
|
||||||
if [[ -L pr/ecosystem/ecosystem-result ]]
|
|
||||||
then
|
|
||||||
echo "Error: ecosystem-result cannot be a symlink"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Note this identifier is used to find the comment to update on
|
# Note this identifier is used to find the comment to update on
|
||||||
# subsequent runs
|
# subsequent runs
|
||||||
echo '<!-- generated-comment ecosystem -->' >> comment.txt
|
echo '<!-- generated-comment ecosystem -->' >> comment.txt
|
||||||
|
|||||||
34
.github/workflows/publish-pypi.yml
vendored
34
.github/workflows/publish-pypi.yml
vendored
@@ -1,34 +0,0 @@
|
|||||||
# Publish a release to PyPI.
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a publish job
|
|
||||||
# within `cargo-dist`.
|
|
||||||
name: "[ruff] Publish to PyPI"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_call:
|
|
||||||
inputs:
|
|
||||||
plan:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
pypi-publish:
|
|
||||||
name: Upload to PyPI
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment:
|
|
||||||
name: release
|
|
||||||
permissions:
|
|
||||||
# For PyPI's trusted publishing.
|
|
||||||
id-token: write
|
|
||||||
steps:
|
|
||||||
- uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
pattern: wheels-*
|
|
||||||
path: wheels
|
|
||||||
merge-multiple: true
|
|
||||||
- name: Publish to PyPi
|
|
||||||
uses: pypa/gh-action-pypi-publish@release/v1
|
|
||||||
with:
|
|
||||||
skip-existing: true
|
|
||||||
packages-dir: wheels
|
|
||||||
verbose: true
|
|
||||||
@@ -1,23 +1,21 @@
|
|||||||
# Build ruff on all platforms.
|
name: "[ruff] Release"
|
||||||
#
|
|
||||||
# Generates both wheels (for PyPI) and archived binaries (for GitHub releases).
|
|
||||||
#
|
|
||||||
# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a local
|
|
||||||
# artifacts job within `cargo-dist`.
|
|
||||||
name: "Build binaries"
|
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_call:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
plan:
|
tag:
|
||||||
required: true
|
description: "The version to tag, without the leading 'v'. If omitted, will initiate a dry run (no uploads)."
|
||||||
|
type: string
|
||||||
|
sha:
|
||||||
|
description: "The full sha of the commit to be released. If omitted, the latest commit on the default branch will be used."
|
||||||
|
default: ""
|
||||||
type: string
|
type: string
|
||||||
pull_request:
|
pull_request:
|
||||||
paths:
|
paths:
|
||||||
# When we change pyproject.toml, we want to ensure that the maturin builds still work.
|
# When we change pyproject.toml, we want to ensure that the maturin builds still work
|
||||||
- pyproject.toml
|
- pyproject.toml
|
||||||
# And when we change this workflow itself...
|
# And when we change this workflow itself...
|
||||||
- .github/workflows/build-binaries.yml
|
- .github/workflows/release.yaml
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
@@ -25,7 +23,6 @@ concurrency:
|
|||||||
|
|
||||||
env:
|
env:
|
||||||
PACKAGE_NAME: ruff
|
PACKAGE_NAME: ruff
|
||||||
MODULE_NAME: ruff
|
|
||||||
PYTHON_VERSION: "3.11"
|
PYTHON_VERSION: "3.11"
|
||||||
CARGO_INCREMENTAL: 0
|
CARGO_INCREMENTAL: 0
|
||||||
CARGO_NET_RETRY: 10
|
CARGO_NET_RETRY: 10
|
||||||
@@ -34,12 +31,11 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
sdist:
|
sdist:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -53,8 +49,8 @@ jobs:
|
|||||||
- name: "Test sdist"
|
- name: "Test sdist"
|
||||||
run: |
|
run: |
|
||||||
pip install dist/${{ env.PACKAGE_NAME }}-*.tar.gz --force-reinstall
|
pip install dist/${{ env.PACKAGE_NAME }}-*.tar.gz --force-reinstall
|
||||||
${{ env.MODULE_NAME }} --help
|
ruff --help
|
||||||
python -m ${{ env.MODULE_NAME }} --help
|
python -m ruff --help
|
||||||
- name: "Upload sdist"
|
- name: "Upload sdist"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
@@ -62,12 +58,11 @@ jobs:
|
|||||||
path: dist
|
path: dist
|
||||||
|
|
||||||
macos-x86_64:
|
macos-x86_64:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
runs-on: macos-latest
|
||||||
runs-on: macos-12
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -79,6 +74,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
target: x86_64
|
target: x86_64
|
||||||
args: --release --locked --out dist
|
args: --release --locked --out dist
|
||||||
|
- name: "Test wheel - x86_64"
|
||||||
|
run: |
|
||||||
|
pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
||||||
|
ruff --help
|
||||||
|
python -m ruff --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
@@ -86,43 +86,36 @@ jobs:
|
|||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
run: |
|
run: |
|
||||||
TARGET=x86_64-apple-darwin
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-x86_64-apple-darwin.tar.gz
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
tar czvf $ARCHIVE_FILE -C target/x86_64-apple-darwin/release ruff
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-macos-x86_64
|
name: binaries-macos-x86_64
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
macos-aarch64:
|
macos-universal:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
runs-on: macos-latest
|
||||||
runs-on: macos-14
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
architecture: arm64
|
architecture: x64
|
||||||
- name: "Prep README.md"
|
- name: "Prep README.md"
|
||||||
run: python scripts/transform_readme.py --target pypi
|
run: python scripts/transform_readme.py --target pypi
|
||||||
- name: "Build wheels - aarch64"
|
- name: "Build wheels - universal2"
|
||||||
uses: PyO3/maturin-action@v1
|
uses: PyO3/maturin-action@v1
|
||||||
with:
|
with:
|
||||||
target: aarch64
|
args: --release --locked --target universal2-apple-darwin --out dist
|
||||||
args: --release --locked --out dist
|
- name: "Test wheel - universal2"
|
||||||
- name: "Test wheel - aarch64"
|
|
||||||
run: |
|
run: |
|
||||||
pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
pip install dist/${{ env.PACKAGE_NAME }}-*universal2.whl --force-reinstall
|
||||||
ruff --help
|
ruff --help
|
||||||
python -m ruff --help
|
python -m ruff --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
@@ -132,24 +125,18 @@ jobs:
|
|||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
run: |
|
run: |
|
||||||
TARGET=aarch64-apple-darwin
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-aarch64-apple-darwin.tar.gz
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
tar czvf $ARCHIVE_FILE -C target/aarch64-apple-darwin/release ruff
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-aarch64-apple-darwin
|
name: binaries-aarch64-apple-darwin
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
windows:
|
windows:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -163,7 +150,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -175,16 +162,13 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
target: ${{ matrix.platform.target }}
|
target: ${{ matrix.platform.target }}
|
||||||
args: --release --locked --out dist
|
args: --release --locked --out dist
|
||||||
env:
|
|
||||||
# aarch64 build fails, see https://github.com/PyO3/maturin/issues/2110
|
|
||||||
XWIN_VERSION: 16
|
|
||||||
- name: "Test wheel"
|
- name: "Test wheel"
|
||||||
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
|
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
python -m pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
python -m pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
||||||
${{ env.MODULE_NAME }} --help
|
ruff --help
|
||||||
python -m ${{ env.MODULE_NAME }} --help
|
python -m ruff --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
@@ -193,19 +177,18 @@ jobs:
|
|||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
ARCHIVE_FILE=ruff-${{ matrix.platform.target }}.zip
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-${{ matrix.platform.target }}.zip
|
||||||
7z a $ARCHIVE_FILE ./target/${{ matrix.platform.target }}/release/ruff.exe
|
7z a $ARCHIVE_FILE ./target/${{ matrix.platform.target }}/release/ruff.exe
|
||||||
sha256sum $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
sha256sum $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-${{ matrix.platform.target }}
|
name: binaries-${{ matrix.platform.target }}
|
||||||
path: |
|
path: |
|
||||||
*.zip
|
*.zip
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
linux:
|
linux:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -215,7 +198,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -232,36 +215,27 @@ jobs:
|
|||||||
if: ${{ startsWith(matrix.target, 'x86_64') }}
|
if: ${{ startsWith(matrix.target, 'x86_64') }}
|
||||||
run: |
|
run: |
|
||||||
pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
pip install dist/${{ env.PACKAGE_NAME }}-*.whl --force-reinstall
|
||||||
${{ env.MODULE_NAME }} --help
|
ruff --help
|
||||||
python -m ${{ env.MODULE_NAME }} --help
|
python -m ruff --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: wheels-${{ matrix.target }}
|
name: wheels-${{ matrix.target }}
|
||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
shell: bash
|
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-${{ matrix.target }}.tar.gz
|
||||||
|
tar czvf $ARCHIVE_FILE -C target/${{ matrix.target }}/release ruff
|
||||||
TARGET=${{ matrix.target }}
|
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-${{ matrix.target }}
|
name: binaries-${{ matrix.target }}
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
linux-cross:
|
linux-cross:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -283,13 +257,11 @@ jobs:
|
|||||||
arch: ppc64
|
arch: ppc64
|
||||||
# see https://github.com/astral-sh/ruff/issues/10073
|
# see https://github.com/astral-sh/ruff/issues/10073
|
||||||
maturin_docker_options: -e JEMALLOC_SYS_WITH_LG_PAGE=16
|
maturin_docker_options: -e JEMALLOC_SYS_WITH_LG_PAGE=16
|
||||||
- target: arm-unknown-linux-musleabihf
|
|
||||||
arch: arm
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -306,8 +278,8 @@ jobs:
|
|||||||
if: matrix.platform.arch != 'ppc64'
|
if: matrix.platform.arch != 'ppc64'
|
||||||
name: Test wheel
|
name: Test wheel
|
||||||
with:
|
with:
|
||||||
arch: ${{ matrix.platform.arch == 'arm' && 'armv6' || matrix.platform.arch }}
|
arch: ${{ matrix.platform.arch }}
|
||||||
distro: ${{ matrix.platform.arch == 'arm' && 'bullseye' || 'ubuntu20.04' }}
|
distro: ubuntu20.04
|
||||||
githubToken: ${{ github.token }}
|
githubToken: ${{ github.token }}
|
||||||
install: |
|
install: |
|
||||||
apt-get update
|
apt-get update
|
||||||
@@ -322,28 +294,19 @@ jobs:
|
|||||||
name: wheels-${{ matrix.platform.target }}
|
name: wheels-${{ matrix.platform.target }}
|
||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
shell: bash
|
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-${{ matrix.platform.target }}.tar.gz
|
||||||
|
tar czvf $ARCHIVE_FILE -C target/${{ matrix.platform.target }}/release ruff
|
||||||
TARGET=${{ matrix.platform.target }}
|
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-${{ matrix.platform.target }}
|
name: binaries-${{ matrix.platform.target }}
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
musllinux:
|
musllinux:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -353,7 +316,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -376,35 +339,26 @@ jobs:
|
|||||||
apk add python3
|
apk add python3
|
||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
.venv/bin/pip3 install ${{ env.PACKAGE_NAME }} --no-index --find-links dist/ --force-reinstall
|
.venv/bin/pip3 install ${{ env.PACKAGE_NAME }} --no-index --find-links dist/ --force-reinstall
|
||||||
.venv/bin/${{ env.MODULE_NAME }} --help
|
.venv/bin/ruff check --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: wheels-${{ matrix.target }}
|
name: wheels-${{ matrix.target }}
|
||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
shell: bash
|
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-${{ matrix.target }}.tar.gz
|
||||||
|
tar czvf $ARCHIVE_FILE -C target/${{ matrix.target }}/release ruff
|
||||||
TARGET=${{ matrix.target }}
|
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-${{ matrix.target }}
|
name: binaries-${{ matrix.target }}
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
musllinux-cross:
|
musllinux-cross:
|
||||||
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-build') }}
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -418,7 +372,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
ref: ${{ inputs.sha }}
|
||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
@@ -442,29 +396,204 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
.venv/bin/pip3 install ${{ env.PACKAGE_NAME }} --no-index --find-links dist/ --force-reinstall
|
.venv/bin/pip3 install ${{ env.PACKAGE_NAME }} --no-index --find-links dist/ --force-reinstall
|
||||||
.venv/bin/${{ env.MODULE_NAME }} --help
|
.venv/bin/ruff check --help
|
||||||
- name: "Upload wheels"
|
- name: "Upload wheels"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: wheels-${{ matrix.platform.target }}
|
name: wheels-${{ matrix.platform.target }}
|
||||||
path: dist
|
path: dist
|
||||||
- name: "Archive binary"
|
- name: "Archive binary"
|
||||||
shell: bash
|
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
ARCHIVE_FILE=ruff-${{ inputs.tag }}-${{ matrix.platform.target }}.tar.gz
|
||||||
|
tar czvf $ARCHIVE_FILE -C target/${{ matrix.platform.target }}/release ruff
|
||||||
TARGET=${{ matrix.platform.target }}
|
|
||||||
ARCHIVE_NAME=ruff-$TARGET
|
|
||||||
ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz
|
|
||||||
|
|
||||||
mkdir -p $ARCHIVE_NAME
|
|
||||||
cp target/$TARGET/release/ruff $ARCHIVE_NAME/ruff
|
|
||||||
tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
|
|
||||||
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
|
||||||
- name: "Upload binary"
|
- name: "Upload binary"
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: artifacts-${{ matrix.platform.target }}
|
name: binaries-${{ matrix.platform.target }}
|
||||||
path: |
|
path: |
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.sha256
|
*.sha256
|
||||||
|
|
||||||
|
validate-tag:
|
||||||
|
name: Validate tag
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# If you don't set an input tag, it's a dry run (no uploads).
|
||||||
|
if: ${{ inputs.tag }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: main # We checkout the main branch to check for the commit
|
||||||
|
- name: Check main branch
|
||||||
|
if: ${{ inputs.sha }}
|
||||||
|
run: |
|
||||||
|
# Fetch the main branch since a shallow checkout is used by default
|
||||||
|
git fetch origin main --unshallow
|
||||||
|
if ! git branch --contains ${{ inputs.sha }} | grep -E '(^|\s)main$'; then
|
||||||
|
echo "The specified sha is not on the main branch" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
- name: Check tag consistency
|
||||||
|
run: |
|
||||||
|
# Switch to the commit we want to release
|
||||||
|
git checkout ${{ inputs.sha }}
|
||||||
|
version=$(grep "version = " pyproject.toml | sed -e 's/version = "\(.*\)"/\1/g')
|
||||||
|
if [ "${{ inputs.tag }}" != "${version}" ]; then
|
||||||
|
echo "The input tag does not match the version from pyproject.toml:" >&2
|
||||||
|
echo "${{ inputs.tag }}" >&2
|
||||||
|
echo "${version}" >&2
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "Releasing ${version}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
upload-release:
|
||||||
|
name: Upload to PyPI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs:
|
||||||
|
- macos-universal
|
||||||
|
- macos-x86_64
|
||||||
|
- windows
|
||||||
|
- linux
|
||||||
|
- linux-cross
|
||||||
|
- musllinux
|
||||||
|
- musllinux-cross
|
||||||
|
- validate-tag
|
||||||
|
# If you don't set an input tag, it's a dry run (no uploads).
|
||||||
|
if: ${{ inputs.tag }}
|
||||||
|
environment:
|
||||||
|
name: release
|
||||||
|
permissions:
|
||||||
|
# For pypi trusted publishing
|
||||||
|
id-token: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: wheels-*
|
||||||
|
path: wheels
|
||||||
|
merge-multiple: true
|
||||||
|
- name: Publish to PyPi
|
||||||
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
with:
|
||||||
|
skip-existing: true
|
||||||
|
packages-dir: wheels
|
||||||
|
verbose: true
|
||||||
|
|
||||||
|
tag-release:
|
||||||
|
name: Tag release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: upload-release
|
||||||
|
# If you don't set an input tag, it's a dry run (no uploads).
|
||||||
|
if: ${{ inputs.tag }}
|
||||||
|
permissions:
|
||||||
|
# For git tag
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: ${{ inputs.sha }}
|
||||||
|
- name: git tag
|
||||||
|
run: |
|
||||||
|
git config user.email "hey@astral.sh"
|
||||||
|
git config user.name "Ruff Release CI"
|
||||||
|
git tag -m "v${{ inputs.tag }}" "v${{ inputs.tag }}"
|
||||||
|
# If there is duplicate tag, this will fail. The publish to pypi action will have been a noop (due to skip
|
||||||
|
# existing), so we make a non-destructive exit here
|
||||||
|
git push --tags
|
||||||
|
|
||||||
|
publish-release:
|
||||||
|
name: Publish to GitHub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: tag-release
|
||||||
|
# If you don't set an input tag, it's a dry run (no uploads).
|
||||||
|
if: ${{ inputs.tag }}
|
||||||
|
permissions:
|
||||||
|
# For GitHub release publishing
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: binaries-*
|
||||||
|
path: binaries
|
||||||
|
merge-multiple: true
|
||||||
|
- name: "Publish to GitHub"
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
draft: true
|
||||||
|
files: binaries/*
|
||||||
|
tag_name: v${{ inputs.tag }}
|
||||||
|
|
||||||
|
docker-publish:
|
||||||
|
# This action doesn't need to wait on any other task, it's easy to re-tag if something failed and we're validating
|
||||||
|
# the tag here also
|
||||||
|
name: Push Docker image ghcr.io/astral-sh/ruff
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
environment:
|
||||||
|
name: release
|
||||||
|
permissions:
|
||||||
|
# For the docker push
|
||||||
|
packages: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: ${{ inputs.sha }}
|
||||||
|
|
||||||
|
- uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ghcr.io/astral-sh/ruff
|
||||||
|
|
||||||
|
- name: Check tag consistency
|
||||||
|
# Unlike validate-tag we don't check if the commit is on the main branch, but it seems good enough since we can
|
||||||
|
# change docker tags
|
||||||
|
if: ${{ inputs.tag }}
|
||||||
|
run: |
|
||||||
|
version=$(grep "version = " pyproject.toml | sed -e 's/version = "\(.*\)"/\1/g')
|
||||||
|
if [ "${{ inputs.tag }}" != "${version}" ]; then
|
||||||
|
echo "The input tag does not match the version from pyproject.toml:" >&2
|
||||||
|
echo "${{ inputs.tag }}" >&2
|
||||||
|
echo "${version}" >&2
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "Releasing ${version}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: "Build and push Docker image"
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
# Reuse the builder
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
push: ${{ inputs.tag != '' }}
|
||||||
|
tags: ghcr.io/astral-sh/ruff:latest,ghcr.io/astral-sh/ruff:${{ inputs.tag || 'dry-run' }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
|
||||||
|
# After the release has been published, we update downstream repositories
|
||||||
|
# This is separate because if this fails the release is still fine, we just need to do some manual workflow triggers
|
||||||
|
update-dependents:
|
||||||
|
name: Update dependents
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: publish-release
|
||||||
|
steps:
|
||||||
|
- name: "Update pre-commit mirror"
|
||||||
|
uses: actions/github-script@v7
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.RUFF_PRE_COMMIT_PAT }}
|
||||||
|
script: |
|
||||||
|
github.rest.actions.createWorkflowDispatch({
|
||||||
|
owner: 'astral-sh',
|
||||||
|
repo: 'ruff-pre-commit',
|
||||||
|
workflow_id: 'main.yml',
|
||||||
|
ref: 'main',
|
||||||
|
})
|
||||||
267
.github/workflows/release.yml
vendored
267
.github/workflows/release.yml
vendored
@@ -1,267 +0,0 @@
|
|||||||
# Copyright 2022-2024, axodotdev
|
|
||||||
# SPDX-License-Identifier: MIT or Apache-2.0
|
|
||||||
#
|
|
||||||
# CI that:
|
|
||||||
#
|
|
||||||
# * checks for a Git Tag that looks like a release
|
|
||||||
# * builds artifacts with cargo-dist (archives, installers, hashes)
|
|
||||||
# * uploads those artifacts to temporary workflow zip
|
|
||||||
# * on success, uploads the artifacts to a GitHub Release
|
|
||||||
#
|
|
||||||
# Note that the GitHub Release will be created with a generated
|
|
||||||
# title/body based on your changelogs.
|
|
||||||
|
|
||||||
name: Release
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
# This task will run whenever you workflow_dispatch with a tag that looks like a version
|
|
||||||
# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc.
|
|
||||||
# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where
|
|
||||||
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
|
|
||||||
# must be a Cargo-style SemVer Version (must have at least major.minor.patch).
|
|
||||||
#
|
|
||||||
# If PACKAGE_NAME is specified, then the announcement will be for that
|
|
||||||
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
|
|
||||||
#
|
|
||||||
# If PACKAGE_NAME isn't specified, then the announcement will be for all
|
|
||||||
# (cargo-dist-able) packages in the workspace with that version (this mode is
|
|
||||||
# intended for workspaces with only one dist-able package, or with all dist-able
|
|
||||||
# packages versioned/released in lockstep).
|
|
||||||
#
|
|
||||||
# If you push multiple tags at once, separate instances of this workflow will
|
|
||||||
# spin up, creating an independent announcement for each one. However, GitHub
|
|
||||||
# will hard limit this to 3 tags per commit, as it will assume more tags is a
|
|
||||||
# mistake.
|
|
||||||
#
|
|
||||||
# If there's a prerelease-style suffix to the version, then the release(s)
|
|
||||||
# will be marked as a prerelease.
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
tag:
|
|
||||||
description: Release Tag
|
|
||||||
required: true
|
|
||||||
default: dry-run
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
# Run 'cargo dist plan' (or host) to determine what tasks we need to do
|
|
||||||
plan:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
outputs:
|
|
||||||
val: ${{ steps.plan.outputs.manifest }}
|
|
||||||
tag: ${{ (inputs.tag != 'dry-run' && inputs.tag) || '' }}
|
|
||||||
tag-flag: ${{ inputs.tag && inputs.tag != 'dry-run' && format('--tag={0}', inputs.tag) || '' }}
|
|
||||||
publishing: ${{ inputs.tag && inputs.tag != 'dry-run' }}
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: Install cargo-dist
|
|
||||||
# we specify bash to get pipefail; it guards against the `curl` command
|
|
||||||
# failing. otherwise `sh` won't catch that `curl` returned non-0
|
|
||||||
shell: bash
|
|
||||||
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh"
|
|
||||||
# sure would be cool if github gave us proper conditionals...
|
|
||||||
# so here's a doubly-nested ternary-via-truthiness to try to provide the best possible
|
|
||||||
# functionality based on whether this is a pull_request, and whether it's from a fork.
|
|
||||||
# (PRs run on the *source* but secrets are usually on the *target* -- that's *good*
|
|
||||||
# but also really annoying to build CI around when it needs secrets to work right.)
|
|
||||||
- id: plan
|
|
||||||
run: |
|
|
||||||
cargo dist ${{ (inputs.tag && inputs.tag != 'dry-run' && format('host --steps=create --tag={0}', inputs.tag)) || 'plan' }} --output-format=json > plan-dist-manifest.json
|
|
||||||
echo "cargo dist ran successfully"
|
|
||||||
cat plan-dist-manifest.json
|
|
||||||
echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT"
|
|
||||||
- name: "Upload dist-manifest.json"
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: artifacts-plan-dist-manifest
|
|
||||||
path: plan-dist-manifest.json
|
|
||||||
|
|
||||||
custom-build-binaries:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
if: ${{ needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload' || inputs.tag == 'dry-run' }}
|
|
||||||
uses: ./.github/workflows/build-binaries.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
|
|
||||||
custom-build-docker:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
if: ${{ needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload' || inputs.tag == 'dry-run' }}
|
|
||||||
uses: ./.github/workflows/build-docker.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
permissions:
|
|
||||||
packages: write
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
# Build and package all the platform-agnostic(ish) things
|
|
||||||
build-global-artifacts:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- custom-build-binaries
|
|
||||||
- custom-build-docker
|
|
||||||
runs-on: "ubuntu-20.04"
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: Install cargo-dist
|
|
||||||
shell: bash
|
|
||||||
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh"
|
|
||||||
# Get all the local artifacts for the global tasks to use (for e.g. checksums)
|
|
||||||
- name: Fetch local artifacts
|
|
||||||
uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
pattern: artifacts-*
|
|
||||||
path: target/distrib/
|
|
||||||
merge-multiple: true
|
|
||||||
- id: cargo-dist
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json
|
|
||||||
echo "cargo dist ran successfully"
|
|
||||||
|
|
||||||
# Parse out what we just built and upload it to scratch storage
|
|
||||||
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
|
|
||||||
jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT"
|
|
||||||
echo "EOF" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
|
|
||||||
- name: "Upload artifacts"
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: artifacts-build-global
|
|
||||||
path: |
|
|
||||||
${{ steps.cargo-dist.outputs.paths }}
|
|
||||||
${{ env.BUILD_MANIFEST_NAME }}
|
|
||||||
# Determines if we should publish/announce
|
|
||||||
host:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- custom-build-binaries
|
|
||||||
- custom-build-docker
|
|
||||||
- build-global-artifacts
|
|
||||||
# Only run if we're "publishing", and only if local and global didn't fail (skipped is fine)
|
|
||||||
if: ${{ always() && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.custom-build-binaries.result == 'skipped' || needs.custom-build-binaries.result == 'success') && (needs.custom-build-docker.result == 'skipped' || needs.custom-build-docker.result == 'success') }}
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
runs-on: "ubuntu-20.04"
|
|
||||||
outputs:
|
|
||||||
val: ${{ steps.host.outputs.manifest }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: Install cargo-dist
|
|
||||||
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.14.0/cargo-dist-installer.sh | sh"
|
|
||||||
# Fetch artifacts from scratch-storage
|
|
||||||
- name: Fetch artifacts
|
|
||||||
uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
pattern: artifacts-*
|
|
||||||
path: target/distrib/
|
|
||||||
merge-multiple: true
|
|
||||||
# This is a harmless no-op for GitHub Releases, hosting for that happens in "announce"
|
|
||||||
- id: host
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
cargo dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json
|
|
||||||
echo "artifacts uploaded and released successfully"
|
|
||||||
cat dist-manifest.json
|
|
||||||
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
|
|
||||||
- name: "Upload dist-manifest.json"
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
# Overwrite the previous copy
|
|
||||||
name: artifacts-dist-manifest
|
|
||||||
path: dist-manifest.json
|
|
||||||
|
|
||||||
custom-publish-pypi:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- host
|
|
||||||
if: ${{ !fromJson(needs.plan.outputs.val).announcement_is_prerelease || fromJson(needs.plan.outputs.val).publish_prereleases }}
|
|
||||||
uses: ./.github/workflows/publish-pypi.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
# publish jobs get escalated permissions
|
|
||||||
permissions:
|
|
||||||
id-token: write
|
|
||||||
packages: write
|
|
||||||
|
|
||||||
# Create a GitHub Release while uploading all files to it
|
|
||||||
announce:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- host
|
|
||||||
- custom-publish-pypi
|
|
||||||
# use "always() && ..." to allow us to wait for all publish jobs while
|
|
||||||
# still allowing individual publish jobs to skip themselves (for prereleases).
|
|
||||||
# "host" however must run to completion, no skipping allowed!
|
|
||||||
if: ${{ always() && needs.host.result == 'success' && (needs.custom-publish-pypi.result == 'skipped' || needs.custom-publish-pypi.result == 'success') }}
|
|
||||||
runs-on: "ubuntu-20.04"
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: "Download GitHub Artifacts"
|
|
||||||
uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
pattern: artifacts-*
|
|
||||||
path: artifacts
|
|
||||||
merge-multiple: true
|
|
||||||
- name: Cleanup
|
|
||||||
run: |
|
|
||||||
# Remove the granular manifests
|
|
||||||
rm -f artifacts/*-dist-manifest.json
|
|
||||||
- name: Create GitHub Release
|
|
||||||
uses: ncipollo/release-action@v1
|
|
||||||
with:
|
|
||||||
tag: ${{ needs.plan.outputs.tag }}
|
|
||||||
name: ${{ fromJson(needs.host.outputs.val).announcement_title }}
|
|
||||||
body: ${{ fromJson(needs.host.outputs.val).announcement_github_body }}
|
|
||||||
prerelease: ${{ fromJson(needs.host.outputs.val).announcement_is_prerelease }}
|
|
||||||
artifacts: "artifacts/*"
|
|
||||||
|
|
||||||
custom-notify-dependents:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- announce
|
|
||||||
uses: ./.github/workflows/notify-dependents.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
|
|
||||||
custom-publish-docs:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- announce
|
|
||||||
uses: ./.github/workflows/publish-docs.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
|
|
||||||
custom-publish-playground:
|
|
||||||
needs:
|
|
||||||
- plan
|
|
||||||
- announce
|
|
||||||
uses: ./.github/workflows/publish-playground.yml
|
|
||||||
with:
|
|
||||||
plan: ${{ needs.plan.outputs.val }}
|
|
||||||
secrets: inherit
|
|
||||||
80
.github/workflows/sync_typeshed.yaml
vendored
80
.github/workflows/sync_typeshed.yaml
vendored
@@ -1,80 +0,0 @@
|
|||||||
name: Sync typeshed
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
schedule:
|
|
||||||
# Run on the 1st and the 15th of every month:
|
|
||||||
- cron: "0 0 1,15 * *"
|
|
||||||
|
|
||||||
env:
|
|
||||||
FORCE_COLOR: 1
|
|
||||||
GH_TOKEN: ${{ github.token }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
sync:
|
|
||||||
name: Sync typeshed
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 20
|
|
||||||
# Don't run the cron job on forks:
|
|
||||||
if: ${{ github.repository == 'astral-sh/ruff' || github.event_name != 'schedule' }}
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
pull-requests: write
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
name: Checkout Ruff
|
|
||||||
with:
|
|
||||||
path: ruff
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
name: Checkout typeshed
|
|
||||||
with:
|
|
||||||
repository: python/typeshed
|
|
||||||
path: typeshed
|
|
||||||
- name: Setup git
|
|
||||||
run: |
|
|
||||||
git config --global user.name typeshedbot
|
|
||||||
git config --global user.email '<>'
|
|
||||||
- name: Sync typeshed
|
|
||||||
id: sync
|
|
||||||
run: |
|
|
||||||
rm -rf ruff/crates/red_knot_module_resolver/vendor/typeshed
|
|
||||||
mkdir ruff/crates/red_knot_module_resolver/vendor/typeshed
|
|
||||||
cp typeshed/README.md ruff/crates/red_knot_module_resolver/vendor/typeshed
|
|
||||||
cp typeshed/LICENSE ruff/crates/red_knot_module_resolver/vendor/typeshed
|
|
||||||
cp -r typeshed/stdlib ruff/crates/red_knot_module_resolver/vendor/typeshed/stdlib
|
|
||||||
rm -rf ruff/crates/red_knot_module_resolver/vendor/typeshed/stdlib/@tests
|
|
||||||
git -C typeshed rev-parse HEAD > ruff/crates/red_knot_module_resolver/vendor/typeshed/source_commit.txt
|
|
||||||
- name: Commit the changes
|
|
||||||
id: commit
|
|
||||||
if: ${{ steps.sync.outcome == 'success' }}
|
|
||||||
run: |
|
|
||||||
cd ruff
|
|
||||||
git checkout -b typeshedbot/sync-typeshed
|
|
||||||
git add .
|
|
||||||
git diff --staged --quiet || git commit -m "Sync typeshed. Source commit: https://github.com/python/typeshed/commit/$(git -C ../typeshed rev-parse HEAD)"
|
|
||||||
- name: Create a PR
|
|
||||||
if: ${{ steps.sync.outcome == 'success' && steps.commit.outcome == 'success' }}
|
|
||||||
run: |
|
|
||||||
cd ruff
|
|
||||||
git push --force origin typeshedbot/sync-typeshed
|
|
||||||
gh pr list --repo $GITHUB_REPOSITORY --head typeshedbot/sync-typeshed --json id --jq length | grep 1 && exit 0 # exit if there is existing pr
|
|
||||||
gh pr create --title "Sync vendored typeshed stubs" --body "Close and reopen this PR to trigger CI" --label "internal"
|
|
||||||
|
|
||||||
create-issue-on-failure:
|
|
||||||
name: Create an issue if the typeshed sync failed
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: [sync]
|
|
||||||
if: ${{ github.repository == 'astral-sh/ruff' && always() && github.event_name == 'schedule' && needs.sync.result == 'failure' }}
|
|
||||||
permissions:
|
|
||||||
issues: write
|
|
||||||
steps:
|
|
||||||
- uses: actions/github-script@v7
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
script: |
|
|
||||||
await github.rest.issues.create({
|
|
||||||
owner: "astral-sh",
|
|
||||||
repo: "ruff",
|
|
||||||
title: `Automated typeshed sync failed on ${new Date().toDateString()}`,
|
|
||||||
body: "Runs are listed here: https://github.com/astral-sh/ruff/actions/workflows/sync_typeshed.yaml",
|
|
||||||
})
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -92,7 +92,6 @@ coverage.xml
|
|||||||
.hypothesis/
|
.hypothesis/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
cover/
|
cover/
|
||||||
repos/
|
|
||||||
|
|
||||||
# Translations
|
# Translations
|
||||||
*.mo
|
*.mo
|
||||||
|
|||||||
@@ -17,4 +17,4 @@ MD013: false
|
|||||||
# MD024/no-duplicate-heading
|
# MD024/no-duplicate-heading
|
||||||
MD024:
|
MD024:
|
||||||
# Allow when nested under different parents e.g. CHANGELOG.md
|
# Allow when nested under different parents e.g. CHANGELOG.md
|
||||||
siblings_only: true
|
allow_different_nesting: true
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ fail_fast: true
|
|||||||
|
|
||||||
exclude: |
|
exclude: |
|
||||||
(?x)^(
|
(?x)^(
|
||||||
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/.*|
|
||||||
@@ -14,7 +13,7 @@ exclude: |
|
|||||||
|
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.18
|
rev: v0.15
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
@@ -32,7 +31,7 @@ repos:
|
|||||||
)$
|
)$
|
||||||
|
|
||||||
- repo: https://github.com/igorshubovych/markdownlint-cli
|
- repo: https://github.com/igorshubovych/markdownlint-cli
|
||||||
rev: v0.41.0
|
rev: v0.37.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: markdownlint-fix
|
- id: markdownlint-fix
|
||||||
exclude: |
|
exclude: |
|
||||||
@@ -42,7 +41,7 @@ repos:
|
|||||||
)$
|
)$
|
||||||
|
|
||||||
- repo: https://github.com/crate-ci/typos
|
- repo: https://github.com/crate-ci/typos
|
||||||
rev: v1.22.9
|
rev: v1.16.22
|
||||||
hooks:
|
hooks:
|
||||||
- id: typos
|
- id: typos
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ repos:
|
|||||||
pass_filenames: false # This makes it a lot faster
|
pass_filenames: false # This makes it a lot faster
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.4.10
|
rev: v0.1.4
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
- id: ruff
|
- id: ruff
|
||||||
@@ -71,7 +70,7 @@ repos:
|
|||||||
|
|
||||||
# Prettier
|
# Prettier
|
||||||
- repo: https://github.com/pre-commit/mirrors-prettier
|
- repo: https://github.com/pre-commit/mirrors-prettier
|
||||||
rev: v3.1.0
|
rev: v3.0.3
|
||||||
hooks:
|
hooks:
|
||||||
- id: prettier
|
- id: prettier
|
||||||
types: [yaml]
|
types: [yaml]
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
# Auto-generated by `cargo-dist`.
|
|
||||||
.github/workflows/release.yml
|
|
||||||
5
.vscode/extensions.json
vendored
5
.vscode/extensions.json
vendored
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"recommendations": [
|
|
||||||
"rust-lang.rust-analyzer"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"rust-analyzer.check.extraArgs": [
|
|
||||||
"--all-features"
|
|
||||||
],
|
|
||||||
"rust-analyzer.check.command": "clippy",
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,5 @@
|
|||||||
# Breaking Changes
|
# Breaking Changes
|
||||||
|
|
||||||
## 0.5.0
|
|
||||||
|
|
||||||
- Follow the XDG specification to discover user-level configurations on macOS (same as on other Unix platforms)
|
|
||||||
- Selecting `ALL` now excludes deprecated rules
|
|
||||||
- The released archives now include an extra level of nesting, which can be removed with `--strip-components=1` when untarring.
|
|
||||||
- The release artifact's file name no longer includes the version tag. This enables users to install via `/latest` URLs on GitHub.
|
|
||||||
|
|
||||||
## 0.3.0
|
## 0.3.0
|
||||||
|
|
||||||
### Ruff 2024.2 style
|
### Ruff 2024.2 style
|
||||||
|
|||||||
751
CHANGELOG.md
751
CHANGELOG.md
@@ -1,743 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 0.5.0
|
|
||||||
|
|
||||||
Check out the [blog post](https://astral.sh/blog/ruff-v0.5.0) for a migration guide and overview of the changes!
|
|
||||||
|
|
||||||
### Breaking changes
|
|
||||||
|
|
||||||
See also, the "Remapped rules" section which may result in disabled rules.
|
|
||||||
|
|
||||||
- Follow the XDG specification to discover user-level configurations on macOS (same as on other Unix platforms)
|
|
||||||
- Selecting `ALL` now excludes deprecated rules
|
|
||||||
- The released archives now include an extra level of nesting, which can be removed with `--strip-components=1` when untarring.
|
|
||||||
- The release artifact's file name no longer includes the version tag. This enables users to install via `/latest` URLs on GitHub.
|
|
||||||
- The diagnostic ranges for some `flake8-bandit` rules were modified ([#10667](https://github.com/astral-sh/ruff/pull/10667)).
|
|
||||||
|
|
||||||
### Deprecations
|
|
||||||
|
|
||||||
The following rules are now deprecated:
|
|
||||||
|
|
||||||
- [`syntax-error`](https://docs.astral.sh/ruff/rules/syntax-error/) (`E999`): Syntax errors are now always shown
|
|
||||||
|
|
||||||
### Remapped rules
|
|
||||||
|
|
||||||
The following rules have been remapped to new rule codes:
|
|
||||||
|
|
||||||
- [`blocking-http-call-in-async-function`](https://docs.astral.sh/ruff/rules/blocking-http-call-in-async-function/): `ASYNC100` to `ASYNC210`
|
|
||||||
- [`open-sleep-or-subprocess-in-async-function`](https://docs.astral.sh/ruff/rules/open-sleep-or-subprocess-in-async-function/): `ASYNC101` split into `ASYNC220`, `ASYNC221`, `ASYNC230`, and `ASYNC251`
|
|
||||||
- [`blocking-os-call-in-async-function`](https://docs.astral.sh/ruff/rules/blocking-os-call-in-async-function/): `ASYNC102` has been merged into `ASYNC220` and `ASYNC221`
|
|
||||||
- [`trio-timeout-without-await`](https://docs.astral.sh/ruff/rules/trio-timeout-without-await/): `TRIO100` to `ASYNC100`
|
|
||||||
- [`trio-sync-call`](https://docs.astral.sh/ruff/rules/trio-sync-call/): `TRIO105` to `ASYNC105`
|
|
||||||
- [`trio-async-function-with-timeout`](https://docs.astral.sh/ruff/rules/trio-async-function-with-timeout/): `TRIO109` to `ASYNC109`
|
|
||||||
- [`trio-unneeded-sleep`](https://docs.astral.sh/ruff/rules/trio-unneeded-sleep/): `TRIO110` to `ASYNC110`
|
|
||||||
- [`trio-zero-sleep-call`](https://docs.astral.sh/ruff/rules/trio-zero-sleep-call/): `TRIO115` to `ASYNC115`
|
|
||||||
- [`repeated-isinstance-calls`](https://docs.astral.sh/ruff/rules/repeated-isinstance-calls/): `PLR1701` to `SIM101`
|
|
||||||
|
|
||||||
### Stabilization
|
|
||||||
|
|
||||||
The following rules have been stabilized and are no longer in preview:
|
|
||||||
|
|
||||||
- [`mutable-fromkeys-value`](https://docs.astral.sh/ruff/rules/mutable-fromkeys-value/) (`RUF024`)
|
|
||||||
- [`default-factory-kwarg`](https://docs.astral.sh/ruff/rules/default-factory-kwarg/) (`RUF026`)
|
|
||||||
- [`django-extra`](https://docs.astral.sh/ruff/rules/django-extra/) (`S610`)
|
|
||||||
- [`manual-dict-comprehension`](https://docs.astral.sh/ruff/rules/manual-dict-comprehension/) (`PERF403`)
|
|
||||||
- [`print-empty-string`](https://docs.astral.sh/ruff/rules/print-empty-string/) (`FURB105`)
|
|
||||||
- [`readlines-in-for`](https://docs.astral.sh/ruff/rules/readlines-in-for/) (`FURB129`)
|
|
||||||
- [`if-expr-min-max`](https://docs.astral.sh/ruff/rules/if-expr-min-max/) (`FURB136`)
|
|
||||||
- [`bit-count`](https://docs.astral.sh/ruff/rules/bit-count/) (`FURB161`)
|
|
||||||
- [`redundant-log-base`](https://docs.astral.sh/ruff/rules/redundant-log-base/) (`FURB163`)
|
|
||||||
- [`regex-flag-alias`](https://docs.astral.sh/ruff/rules/regex-flag-alias/) (`FURB167`)
|
|
||||||
- [`isinstance-type-none`](https://docs.astral.sh/ruff/rules/isinstance-type-none/) (`FURB168`)
|
|
||||||
- [`type-none-comparison`](https://docs.astral.sh/ruff/rules/type-none-comparison/) (`FURB169`)
|
|
||||||
- [`implicit-cwd`](https://docs.astral.sh/ruff/rules/implicit-cwd/) (`FURB177`)
|
|
||||||
- [`hashlib-digest-hex`](https://docs.astral.sh/ruff/rules/hashlib-digest-hex/) (`FURB181`)
|
|
||||||
- [`list-reverse-copy`](https://docs.astral.sh/ruff/rules/list-reverse-copy/) (`FURB187`)
|
|
||||||
- [`bad-open-mode`](https://docs.astral.sh/ruff/rules/bad-open-mode/) (`PLW1501`)
|
|
||||||
- [`empty-comment`](https://docs.astral.sh/ruff/rules/empty-comment/) (`PLR2044`)
|
|
||||||
- [`global-at-module-level`](https://docs.astral.sh/ruff/rules/global-at-module-level/) (`PLW0604`)
|
|
||||||
- [`misplaced-bare-raise`](https://docs.astral.sh/ruff/rules/misplaced-bare-raise%60/) (`PLE0744`)
|
|
||||||
- [`non-ascii-import-name`](https://docs.astral.sh/ruff/rules/non-ascii-import-name/) (`PLC2403`)
|
|
||||||
- [`non-ascii-name`](https://docs.astral.sh/ruff/rules/non-ascii-name/) (`PLC2401`)
|
|
||||||
- [`nonlocal-and-global`](https://docs.astral.sh/ruff/rules/nonlocal-and-global/) (`PLE0115`)
|
|
||||||
- [`potential-index-error`](https://docs.astral.sh/ruff/rules/potential-index-error/) (`PLE0643`)
|
|
||||||
- [`redeclared-assigned-name`](https://docs.astral.sh/ruff/rules/redeclared-assigned-name/) (`PLW0128`)
|
|
||||||
- [`redefined-argument-from-local`](https://docs.astral.sh/ruff/rules/redefined-argument-from-local/) (`PLR1704`)
|
|
||||||
- [`repeated-keyword-argument`](https://docs.astral.sh/ruff/rules/repeated-keyword-argument/) (`PLE1132`)
|
|
||||||
- [`super-without-brackets`](https://docs.astral.sh/ruff/rules/super-without-brackets/) (`PLW0245`)
|
|
||||||
- [`unnecessary-list-index-lookup`](https://docs.astral.sh/ruff/rules/unnecessary-list-index-lookup/) (`PLR1736`)
|
|
||||||
- [`useless-exception-statement`](https://docs.astral.sh/ruff/rules/useless-exception-statement/) (`PLW0133`)
|
|
||||||
- [`useless-with-lock`](https://docs.astral.sh/ruff/rules/useless-with-lock/) (`PLW2101`)
|
|
||||||
|
|
||||||
The following behaviors have been stabilized:
|
|
||||||
|
|
||||||
- [`is-literal`](https://docs.astral.sh/ruff/rules/is-literal/) (`F632`) now warns for identity checks against list, set or dictionary literals
|
|
||||||
- [`needless-bool`](https://docs.astral.sh/ruff/rules/needless-bool/) (`SIM103`) now detects `if` expressions with implicit `else` branches
|
|
||||||
- [`module-import-not-at-top-of-file`](https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file/) (`E402`) now allows `os.environ` modifications between import statements
|
|
||||||
- [`type-comparison`](https://docs.astral.sh/ruff/rules/type-comparison/) (`E721`) now allows idioms such as `type(x) is int`
|
|
||||||
- [`yoda-condition`](https://docs.astral.sh/ruff/rules/yoda-conditions/) (`SIM300`) now flags a wider range of expressions
|
|
||||||
|
|
||||||
### Removals
|
|
||||||
|
|
||||||
The following deprecated settings have been removed:
|
|
||||||
|
|
||||||
- `output-format=text`; use `output-format=concise` or `output-format=full`
|
|
||||||
- `tab-size`; use `indent-width`
|
|
||||||
|
|
||||||
The following deprecated CLI options have been removed:
|
|
||||||
|
|
||||||
- `--show-source`; use `--output-format=full`
|
|
||||||
- `--no-show-source`; use `--output-format=concise`
|
|
||||||
|
|
||||||
The following deprecated CLI commands have been removed:
|
|
||||||
|
|
||||||
- `ruff <path>`; use `ruff check <path>`
|
|
||||||
- `ruff --clean`; use `ruff clean`
|
|
||||||
- `ruff --generate-shell-completion`; use `ruff generate-shell-completion`
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`ruff`\] Add `assert-with-print-message` rule ([#11981](https://github.com/astral-sh/ruff/pull/11981))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Use rule name rather than message in `--statistics` ([#11697](https://github.com/astral-sh/ruff/pull/11697))
|
|
||||||
- Use the output format `full` by default ([#12010](https://github.com/astral-sh/ruff/pull/12010))
|
|
||||||
- Don't log syntax errors to the console ([#11902](https://github.com/astral-sh/ruff/pull/11902))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`ruff`\] Fix false positives if `gettext` is imported using an alias (`RUF027`) ([#12025](https://github.com/astral-sh/ruff/pull/12025))
|
|
||||||
- \[`numpy`\] Update `trapz` and `in1d` deprecation (`NPY201`) ([#11948](https://github.com/astral-sh/ruff/pull/11948))
|
|
||||||
- \[`flake8-bandit`\] Modify diagnostic ranges for shell-related rules ([#10667](https://github.com/astral-sh/ruff/pull/10667))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Closing an untitled, unsaved notebook document no longer throws an error ([#11942](https://github.com/astral-sh/ruff/pull/11942))
|
|
||||||
- Support the usage of tildes and environment variables in `logFile` ([#11945](https://github.com/astral-sh/ruff/pull/11945))
|
|
||||||
- Add option to configure whether to show syntax errors ([#12059](https://github.com/astral-sh/ruff/pull/12059))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`pycodestyle`\] Avoid `E203` for f-string debug expression ([#12024](https://github.com/astral-sh/ruff/pull/12024))
|
|
||||||
- \[`pep8-naming`\] Match import-name ignores against both name and alias (`N812`, `N817`) ([#12033](https://github.com/astral-sh/ruff/pull/12033))
|
|
||||||
- \[`pyflakes`\] Detect assignments that shadow definitions (`F811`) ([#11961](https://github.com/astral-sh/ruff/pull/11961))
|
|
||||||
|
|
||||||
### Parser
|
|
||||||
|
|
||||||
- Emit a syntax error for an empty type parameter list ([#12030](https://github.com/astral-sh/ruff/pull/12030))
|
|
||||||
- Avoid consuming the newline for unterminated strings ([#12067](https://github.com/astral-sh/ruff/pull/12067))
|
|
||||||
- Do not include the newline in the unterminated string range ([#12017](https://github.com/astral-sh/ruff/pull/12017))
|
|
||||||
- Use the correct range to highlight line continuation errors ([#12016](https://github.com/astral-sh/ruff/pull/12016))
|
|
||||||
- Consider 2-character EOL before line continuations ([#12035](https://github.com/astral-sh/ruff/pull/12035))
|
|
||||||
- Consider line continuation character for re-lexing ([#12008](https://github.com/astral-sh/ruff/pull/12008))
|
|
||||||
|
|
||||||
### Other changes
|
|
||||||
|
|
||||||
- Upgrade the Unicode table used for measuring the line-length ([#11194](https://github.com/astral-sh/ruff/pull/11194))
|
|
||||||
- Remove the deprecation error message for the nursery selector ([#10172](https://github.com/astral-sh/ruff/pull/10172))
|
|
||||||
|
|
||||||
## 0.4.10
|
|
||||||
|
|
||||||
### Parser
|
|
||||||
|
|
||||||
- Implement re-lexing logic for better error recovery ([#11845](https://github.com/astral-sh/ruff/pull/11845))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-copyright`\] Update `CPY001` to check the first 4096 bytes instead of 1024 ([#11927](https://github.com/astral-sh/ruff/pull/11927))
|
|
||||||
- \[`pycodestyle`\] Update `E999` to show all syntax errors instead of just the first one ([#11900](https://github.com/astral-sh/ruff/pull/11900))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Add tracing setup guide to Helix documentation ([#11883](https://github.com/astral-sh/ruff/pull/11883))
|
|
||||||
- Add tracing setup guide to Neovim documentation ([#11884](https://github.com/astral-sh/ruff/pull/11884))
|
|
||||||
- Defer notebook cell deletion to avoid an error message ([#11864](https://github.com/astral-sh/ruff/pull/11864))
|
|
||||||
|
|
||||||
### Security
|
|
||||||
|
|
||||||
- Guard against malicious ecosystem comment artifacts ([#11879](https://github.com/astral-sh/ruff/pull/11879))
|
|
||||||
|
|
||||||
## 0.4.9
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pylint`\] Implement `consider-dict-items` (`C0206`) ([#11688](https://github.com/astral-sh/ruff/pull/11688))
|
|
||||||
- \[`refurb`\] Implement `repeated-global` (`FURB154`) ([#11187](https://github.com/astral-sh/ruff/pull/11187))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`pycodestyle`\] Adapt fix for `E203` to work identical to `ruff format` ([#10999](https://github.com/astral-sh/ruff/pull/10999))
|
|
||||||
|
|
||||||
### Formatter
|
|
||||||
|
|
||||||
- Fix formatter instability for lines only consisting of zero-width characters ([#11748](https://github.com/astral-sh/ruff/pull/11748))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Add supported commands in server capabilities ([#11850](https://github.com/astral-sh/ruff/pull/11850))
|
|
||||||
- Use real file path when available in `ruff server` ([#11800](https://github.com/astral-sh/ruff/pull/11800))
|
|
||||||
- Improve error message when a command is run on an unavailable document ([#11823](https://github.com/astral-sh/ruff/pull/11823))
|
|
||||||
- Introduce the `ruff.printDebugInformation` command ([#11831](https://github.com/astral-sh/ruff/pull/11831))
|
|
||||||
- Tracing system now respects log level and trace level, with options to log to a file ([#11747](https://github.com/astral-sh/ruff/pull/11747))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Handle non-printable characters in diff view ([#11687](https://github.com/astral-sh/ruff/pull/11687))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`refurb`\] Avoid suggesting starmap when arguments are used outside call (`FURB140`) ([#11830](https://github.com/astral-sh/ruff/pull/11830))
|
|
||||||
- \[`flake8-bugbear`\] Avoid panic in `B909` when checking large loop blocks ([#11772](https://github.com/astral-sh/ruff/pull/11772))
|
|
||||||
- \[`refurb`\] Fix misbehavior of `operator.itemgetter` when getter param is a tuple (`FURB118`) ([#11774](https://github.com/astral-sh/ruff/pull/11774))
|
|
||||||
|
|
||||||
## 0.4.8
|
|
||||||
|
|
||||||
### Performance
|
|
||||||
|
|
||||||
- Linter performance has been improved by around 10% on some microbenchmarks by refactoring the lexer and parser to maintain synchronicity between them ([#11457](https://github.com/astral-sh/ruff/pull/11457))
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-bugbear`\] Implement `return-in-generator` (`B901`) ([#11644](https://github.com/astral-sh/ruff/pull/11644))
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI063` ([#11699](https://github.com/astral-sh/ruff/pull/11699))
|
|
||||||
- \[`pygrep_hooks`\] Check blanket ignores via file-level pragmas (`PGH004`) ([#11540](https://github.com/astral-sh/ruff/pull/11540))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`pyupgrade`\] Update `UP035` for Python 3.13 and the latest version of `typing_extensions` ([#11693](https://github.com/astral-sh/ruff/pull/11693))
|
|
||||||
- \[`numpy`\] Update `NPY001` rule for NumPy 2.0 ([#11735](https://github.com/astral-sh/ruff/pull/11735))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Formatting a document with syntax problems no longer spams a visible error popup ([#11745](https://github.com/astral-sh/ruff/pull/11745))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Add RDJson support for `--output-format` flag ([#11682](https://github.com/astral-sh/ruff/pull/11682))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`pyupgrade`\] Write empty string in lieu of panic when fixing `UP032` ([#11696](https://github.com/astral-sh/ruff/pull/11696))
|
|
||||||
- \[`flake8-simplify`\] Simplify double negatives in `SIM103` ([#11684](https://github.com/astral-sh/ruff/pull/11684))
|
|
||||||
- Ensure the expression generator adds a newline before `type` statements ([#11720](https://github.com/astral-sh/ruff/pull/11720))
|
|
||||||
- Respect per-file ignores for blanket and redirected noqa rules ([#11728](https://github.com/astral-sh/ruff/pull/11728))
|
|
||||||
|
|
||||||
## 0.4.7
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI064` ([#11325](https://github.com/astral-sh/ruff/pull/11325))
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI066` ([#11541](https://github.com/astral-sh/ruff/pull/11541))
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI057` ([#11486](https://github.com/astral-sh/ruff/pull/11486))
|
|
||||||
- \[`pyflakes`\] Enable `F822` in `__init__.py` files by default ([#11370](https://github.com/astral-sh/ruff/pull/11370))
|
|
||||||
|
|
||||||
### Formatter
|
|
||||||
|
|
||||||
- Fix incorrect placement of trailing stub function comments ([#11632](https://github.com/astral-sh/ruff/pull/11632))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Respect file exclusions in `ruff server` ([#11590](https://github.com/astral-sh/ruff/pull/11590))
|
|
||||||
- Add support for documents not exist on disk ([#11588](https://github.com/astral-sh/ruff/pull/11588))
|
|
||||||
- Add Vim and Kate setup guide for `ruff server` ([#11615](https://github.com/astral-sh/ruff/pull/11615))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid removing newlines between docstring headers and rST blocks ([#11609](https://github.com/astral-sh/ruff/pull/11609))
|
|
||||||
- Infer indentation with imports when logical indent is absent ([#11608](https://github.com/astral-sh/ruff/pull/11608))
|
|
||||||
- Use char index rather than position for indent slice ([#11645](https://github.com/astral-sh/ruff/pull/11645))
|
|
||||||
- \[`flake8-comprehension`\] Strip parentheses around generators in `C400` ([#11607](https://github.com/astral-sh/ruff/pull/11607))
|
|
||||||
- Mark `repeated-isinstance-calls` as unsafe on Python 3.10 and later ([#11622](https://github.com/astral-sh/ruff/pull/11622))
|
|
||||||
|
|
||||||
## 0.4.6
|
|
||||||
|
|
||||||
### Breaking changes
|
|
||||||
|
|
||||||
- Use project-relative paths when calculating GitLab fingerprints ([#11532](https://github.com/astral-sh/ruff/pull/11532))
|
|
||||||
- Bump minimum supported Windows version to Windows 10 ([#11613](https://github.com/astral-sh/ruff/pull/11613))
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-async`\] Sleep with >24 hour interval should usually sleep forever (`ASYNC116`) ([#11498](https://github.com/astral-sh/ruff/pull/11498))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`numpy`\] Add missing functions to NumPy 2.0 migration rule ([#11528](https://github.com/astral-sh/ruff/pull/11528))
|
|
||||||
- \[`mccabe`\] Consider irrefutable pattern similar to `if .. else` for `C901` ([#11565](https://github.com/astral-sh/ruff/pull/11565))
|
|
||||||
- Consider `match`-`case` statements for `C901`, `PLR0912`, and `PLR0915` ([#11521](https://github.com/astral-sh/ruff/pull/11521))
|
|
||||||
- Remove empty strings when converting to f-string (`UP032`) ([#11524](https://github.com/astral-sh/ruff/pull/11524))
|
|
||||||
- \[`flake8-bandit`\] `request-without-timeout` should warn for `requests.request` ([#11548](https://github.com/astral-sh/ruff/pull/11548))
|
|
||||||
- \[`flake8-self`\] Ignore sunder accesses in `flake8-self` rules ([#11546](https://github.com/astral-sh/ruff/pull/11546))
|
|
||||||
- \[`pyupgrade`\] Lint for `TypeAliasType` usages (`UP040`) ([#11530](https://github.com/astral-sh/ruff/pull/11530))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Respect excludes in `ruff server` configuration discovery ([#11551](https://github.com/astral-sh/ruff/pull/11551))
|
|
||||||
- Use default settings if initialization options is empty or not provided ([#11566](https://github.com/astral-sh/ruff/pull/11566))
|
|
||||||
- `ruff server` correctly treats `.pyi` files as stub files ([#11535](https://github.com/astral-sh/ruff/pull/11535))
|
|
||||||
- `ruff server` searches for configuration in parent directories ([#11537](https://github.com/astral-sh/ruff/pull/11537))
|
|
||||||
- `ruff server`: An empty code action filter no longer returns notebook source actions ([#11526](https://github.com/astral-sh/ruff/pull/11526))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`flake8-logging-format`\] Fix autofix title in `logging-warn` (`G010`) ([#11514](https://github.com/astral-sh/ruff/pull/11514))
|
|
||||||
- \[`refurb`\] Avoid recommending `operator.itemgetter` with dependence on lambda arguments ([#11574](https://github.com/astral-sh/ruff/pull/11574))
|
|
||||||
- \[`flake8-simplify`\] Avoid recommending context manager in `__enter__` implementations ([#11575](https://github.com/astral-sh/ruff/pull/11575))
|
|
||||||
- Create intermediary directories for `--output-file` ([#11550](https://github.com/astral-sh/ruff/pull/11550))
|
|
||||||
- Propagate reads on global variables ([#11584](https://github.com/astral-sh/ruff/pull/11584))
|
|
||||||
- Treat all `singledispatch` arguments as runtime-required ([#11523](https://github.com/astral-sh/ruff/pull/11523))
|
|
||||||
|
|
||||||
## 0.4.5
|
|
||||||
|
|
||||||
### Ruff's language server is now in Beta
|
|
||||||
|
|
||||||
`v0.4.5` marks the official Beta release of `ruff server`, an integrated language server built into Ruff.
|
|
||||||
`ruff server` supports the same feature set as `ruff-lsp`, powering linting, formatting, and
|
|
||||||
code fixes in Ruff's editor integrations -- but with superior performance and
|
|
||||||
no installation required. We'd love your feedback!
|
|
||||||
|
|
||||||
You can enable `ruff server` in the [VS Code extension](https://github.com/astral-sh/ruff-vscode?tab=readme-ov-file#enabling-the-rust-based-language-server) today.
|
|
||||||
|
|
||||||
To read more about this exciting milestone, check out our [blog post](https://astral.sh/blog/ruff-v0.4.5)!
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-future-annotations`\] Reword `future-rewritable-type-annotation` (`FA100`) message ([#11381](https://github.com/astral-sh/ruff/pull/11381))
|
|
||||||
- \[`isort`\] Expanded the set of standard-library modules to include `_string`, etc. ([#11374](https://github.com/astral-sh/ruff/pull/11374))
|
|
||||||
- \[`pycodestyle`\] Consider soft keywords for `E27` rules ([#11446](https://github.com/astral-sh/ruff/pull/11446))
|
|
||||||
- \[`pyflakes`\] Recommend adding unused import bindings to `__all__` ([#11314](https://github.com/astral-sh/ruff/pull/11314))
|
|
||||||
- \[`pyflakes`\] Update documentation and deprecate `ignore_init_module_imports` ([#11436](https://github.com/astral-sh/ruff/pull/11436))
|
|
||||||
- \[`pyupgrade`\] Mark quotes as unnecessary for non-evaluated annotations ([#11485](https://github.com/astral-sh/ruff/pull/11485))
|
|
||||||
|
|
||||||
### Formatter
|
|
||||||
|
|
||||||
- Avoid multiline quotes warning with `quote-style = preserve` ([#11490](https://github.com/astral-sh/ruff/pull/11490))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Support Jupyter Notebook files ([#11206](https://github.com/astral-sh/ruff/pull/11206))
|
|
||||||
- Support `noqa` comment code actions ([#11276](https://github.com/astral-sh/ruff/pull/11276))
|
|
||||||
- Fix automatic configuration reloading ([#11492](https://github.com/astral-sh/ruff/pull/11492))
|
|
||||||
- Fix several issues with configuration in Neovim and Helix ([#11497](https://github.com/astral-sh/ruff/pull/11497))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Add `--output-format` as a CLI option for `ruff config` ([#11438](https://github.com/astral-sh/ruff/pull/11438))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid `PLE0237` for property with setter ([#11377](https://github.com/astral-sh/ruff/pull/11377))
|
|
||||||
- Avoid `TCH005` for `if` stmt with `elif`/`else` block ([#11376](https://github.com/astral-sh/ruff/pull/11376))
|
|
||||||
- Avoid flagging `__future__` annotations as required for non-evaluated type annotations ([#11414](https://github.com/astral-sh/ruff/pull/11414))
|
|
||||||
- Check for ruff executable in 'bin' directory as installed by 'pip install --target'. ([#11450](https://github.com/astral-sh/ruff/pull/11450))
|
|
||||||
- Sort edits prior to deduplicating in quotation fix ([#11452](https://github.com/astral-sh/ruff/pull/11452))
|
|
||||||
- Treat escaped newline as valid sequence ([#11465](https://github.com/astral-sh/ruff/pull/11465))
|
|
||||||
- \[`flake8-pie`\] Preserve parentheses in `unnecessary-dict-kwargs` ([#11372](https://github.com/astral-sh/ruff/pull/11372))
|
|
||||||
- \[`pylint`\] Ignore `__slots__` with dynamic values ([#11488](https://github.com/astral-sh/ruff/pull/11488))
|
|
||||||
- \[`pylint`\] Remove `try` body from branch counting ([#11487](https://github.com/astral-sh/ruff/pull/11487))
|
|
||||||
- \[`refurb`\] Respect operator precedence in `FURB110` ([#11464](https://github.com/astral-sh/ruff/pull/11464))
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
- Add `--preview` to the README ([#11395](https://github.com/astral-sh/ruff/pull/11395))
|
|
||||||
- Add Python 3.13 to list of allowed Python versions ([#11411](https://github.com/astral-sh/ruff/pull/11411))
|
|
||||||
- Simplify Neovim setup documentation ([#11489](https://github.com/astral-sh/ruff/pull/11489))
|
|
||||||
- Update CONTRIBUTING.md to reflect the new parser ([#11434](https://github.com/astral-sh/ruff/pull/11434))
|
|
||||||
- Update server documentation with new migration guide ([#11499](https://github.com/astral-sh/ruff/pull/11499))
|
|
||||||
- \[`pycodestyle`\] Clarify motivation for `E713` and `E714` ([#11483](https://github.com/astral-sh/ruff/pull/11483))
|
|
||||||
- \[`pyflakes`\] Update docs to describe WAI behavior (F541) ([#11362](https://github.com/astral-sh/ruff/pull/11362))
|
|
||||||
- \[`pylint`\] Clearly indicate what is counted as a branch ([#11423](https://github.com/astral-sh/ruff/pull/11423))
|
|
||||||
|
|
||||||
## 0.4.4
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pycodestyle`\] Ignore end-of-line comments when determining blank line rules ([#11342](https://github.com/astral-sh/ruff/pull/11342))
|
|
||||||
- \[`pylint`\] Detect `pathlib.Path.open` calls in `unspecified-encoding` (`PLW1514`) ([#11288](https://github.com/astral-sh/ruff/pull/11288))
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI059` (`generic-not-last-base-class`) ([#11233](https://github.com/astral-sh/ruff/pull/11233))
|
|
||||||
- \[`flake8-pyi`\] Implement `PYI062` (`duplicate-literal-member`) ([#11269](https://github.com/astral-sh/ruff/pull/11269))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-boolean-trap`\] Allow passing booleans as positional-only arguments in code such as `set(True)` ([#11287](https://github.com/astral-sh/ruff/pull/11287))
|
|
||||||
- \[`flake8-bugbear`\] Ignore enum classes in `cached-instance-method` (`B019`) ([#11312](https://github.com/astral-sh/ruff/pull/11312))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Expand tildes when resolving Ruff server configuration file ([#11283](https://github.com/astral-sh/ruff/pull/11283))
|
|
||||||
- Fix `ruff server` hanging after Neovim closes ([#11291](https://github.com/astral-sh/ruff/pull/11291))
|
|
||||||
- Editor settings are used by default if no file-based configuration exists ([#11266](https://github.com/astral-sh/ruff/pull/11266))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`pylint`\] Consider `with` statements for `too-many-branches` (`PLR0912`) ([#11321](https://github.com/astral-sh/ruff/pull/11321))
|
|
||||||
- \[`flake8-blind-except`, `tryceratops`\] Respect logged and re-raised expressions in nested statements (`BLE001`, `TRY201`) ([#11301](https://github.com/astral-sh/ruff/pull/11301))
|
|
||||||
- Recognise assignments such as `__all__ = builtins.list(["foo", "bar"])` as valid `__all__` definitions ([#11335](https://github.com/astral-sh/ruff/pull/11335))
|
|
||||||
|
|
||||||
## 0.4.3
|
|
||||||
|
|
||||||
### Enhancements
|
|
||||||
|
|
||||||
- Add support for PEP 696 syntax ([#11120](https://github.com/astral-sh/ruff/pull/11120))
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`refurb`\] Use function range for `reimplemented-operator` diagnostics ([#11271](https://github.com/astral-sh/ruff/pull/11271))
|
|
||||||
- \[`refurb`\] Ignore methods in `reimplemented-operator` (`FURB118`) ([#11270](https://github.com/astral-sh/ruff/pull/11270))
|
|
||||||
- \[`refurb`\] Implement `fstring-number-format` (`FURB116`) ([#10921](https://github.com/astral-sh/ruff/pull/10921))
|
|
||||||
- \[`ruff`\] Implement `redirected-noqa` (`RUF101`) ([#11052](https://github.com/astral-sh/ruff/pull/11052))
|
|
||||||
- \[`pyflakes`\] Distinguish between first-party and third-party imports for fix suggestions ([#11168](https://github.com/astral-sh/ruff/pull/11168))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-bugbear`\] Ignore non-abstract class attributes when enforcing `B024` ([#11210](https://github.com/astral-sh/ruff/pull/11210))
|
|
||||||
- \[`flake8-logging`\] Include inline instantiations when detecting loggers ([#11154](https://github.com/astral-sh/ruff/pull/11154))
|
|
||||||
- \[`pylint`\] Also emit `PLR0206` for properties with variadic parameters ([#11200](https://github.com/astral-sh/ruff/pull/11200))
|
|
||||||
- \[`ruff`\] Detect duplicate codes as part of `unused-noqa` (`RUF100`) ([#10850](https://github.com/astral-sh/ruff/pull/10850))
|
|
||||||
|
|
||||||
### Formatter
|
|
||||||
|
|
||||||
- Avoid multiline expression if format specifier is present ([#11123](https://github.com/astral-sh/ruff/pull/11123))
|
|
||||||
|
|
||||||
### LSP
|
|
||||||
|
|
||||||
- Write `ruff server` setup guide for Helix ([#11183](https://github.com/astral-sh/ruff/pull/11183))
|
|
||||||
- `ruff server` no longer hangs after shutdown ([#11222](https://github.com/astral-sh/ruff/pull/11222))
|
|
||||||
- `ruff server` reads from a configuration TOML file in the user configuration directory if no local configuration exists ([#11225](https://github.com/astral-sh/ruff/pull/11225))
|
|
||||||
- `ruff server` respects `per-file-ignores` configuration ([#11224](https://github.com/astral-sh/ruff/pull/11224))
|
|
||||||
- `ruff server`: Support a custom TOML configuration file ([#11140](https://github.com/astral-sh/ruff/pull/11140))
|
|
||||||
- `ruff server`: Support setting to prioritize project configuration over editor configuration ([#11086](https://github.com/astral-sh/ruff/pull/11086))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid debug assertion around NFKC renames ([#11249](https://github.com/astral-sh/ruff/pull/11249))
|
|
||||||
- \[`pyflakes`\] Prioritize `redefined-while-unused` over `unused-import` ([#11173](https://github.com/astral-sh/ruff/pull/11173))
|
|
||||||
- \[`ruff`\] Respect `async` expressions in comprehension bodies ([#11219](https://github.com/astral-sh/ruff/pull/11219))
|
|
||||||
- \[`pygrep_hooks`\] Fix `blanket-noqa` panic when last line has noqa with no newline (`PGH004`) ([#11108](https://github.com/astral-sh/ruff/pull/11108))
|
|
||||||
- \[`perflint`\] Ignore list-copy recommendations for async `for` loops ([#11250](https://github.com/astral-sh/ruff/pull/11250))
|
|
||||||
- \[`pyflakes`\] Improve `invalid-print-syntax` documentation ([#11171](https://github.com/astral-sh/ruff/pull/11171))
|
|
||||||
|
|
||||||
### Performance
|
|
||||||
|
|
||||||
- Avoid allocations for isort module names ([#11251](https://github.com/astral-sh/ruff/pull/11251))
|
|
||||||
- Build a separate ARM wheel for macOS ([#11149](https://github.com/astral-sh/ruff/pull/11149))
|
|
||||||
|
|
||||||
### Windows
|
|
||||||
|
|
||||||
- Increase the minimum requirement to Windows 10.
|
|
||||||
|
|
||||||
## 0.4.2
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-pyi`\] Allow for overloaded `__exit__` and `__aexit__` definitions (`PYI036`) ([#11057](https://github.com/astral-sh/ruff/pull/11057))
|
|
||||||
- \[`pyupgrade`\] Catch usages of `"%s" % var` and provide an unsafe fix (`UP031`) ([#11019](https://github.com/astral-sh/ruff/pull/11019))
|
|
||||||
- \[`refurb`\] Implement new rule that suggests min/max over `sorted()` (`FURB192`) ([#10868](https://github.com/astral-sh/ruff/pull/10868))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- Fix an issue with missing diagnostics for Neovim and Helix ([#11092](https://github.com/astral-sh/ruff/pull/11092))
|
|
||||||
- Implement hover documentation for `noqa` codes ([#11096](https://github.com/astral-sh/ruff/pull/11096))
|
|
||||||
- Introduce common Ruff configuration options with new server settings ([#11062](https://github.com/astral-sh/ruff/pull/11062))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Use `macos-12` for building release wheels to enable macOS 11 compatibility ([#11146](https://github.com/astral-sh/ruff/pull/11146))
|
|
||||||
- \[`flake8-blind-expect`\] Allow raise from in `BLE001` ([#11131](https://github.com/astral-sh/ruff/pull/11131))
|
|
||||||
- \[`flake8-pyi`\] Allow simple assignments to `None` in enum class scopes (`PYI026`) ([#11128](https://github.com/astral-sh/ruff/pull/11128))
|
|
||||||
- \[`flake8-simplify`\] Avoid raising `SIM911` for non-`zip` attribute calls ([#11126](https://github.com/astral-sh/ruff/pull/11126))
|
|
||||||
- \[`refurb`\] Avoid `operator.itemgetter` suggestion for single-item tuple ([#11095](https://github.com/astral-sh/ruff/pull/11095))
|
|
||||||
- \[`ruff`\] Respect per-file-ignores for `RUF100` with no other diagnostics ([#11058](https://github.com/astral-sh/ruff/pull/11058))
|
|
||||||
- \[`ruff`\] Fix async comprehension false positive (`RUF029`) ([#11070](https://github.com/astral-sh/ruff/pull/11070))
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
- \[`flake8-bugbear`\] Document explicitly disabling strict zip (`B905`) ([#11040](https://github.com/astral-sh/ruff/pull/11040))
|
|
||||||
- \[`flake8-type-checking`\] Mention `lint.typing-modules` in `TCH001`, `TCH002`, and `TCH003` ([#11144](https://github.com/astral-sh/ruff/pull/11144))
|
|
||||||
- \[`isort`\] Improve documentation around custom `isort` sections ([#11050](https://github.com/astral-sh/ruff/pull/11050))
|
|
||||||
- \[`pylint`\] Fix documentation oversight for `invalid-X-returns` ([#11094](https://github.com/astral-sh/ruff/pull/11094))
|
|
||||||
|
|
||||||
### Performance
|
|
||||||
|
|
||||||
- Use `matchit` to resolve per-file settings ([#11111](https://github.com/astral-sh/ruff/pull/11111))
|
|
||||||
|
|
||||||
## 0.4.1
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pylint`\] Implement `invalid-hash-returned` (`PLE0309`) ([#10961](https://github.com/astral-sh/ruff/pull/10961))
|
|
||||||
- \[`pylint`\] Implement `invalid-index-returned` (`PLE0305`) ([#10962](https://github.com/astral-sh/ruff/pull/10962))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`pylint`\] Allow `NoReturn`-like functions for `__str__`, `__len__`, etc. (`PLE0307`) ([#11017](https://github.com/astral-sh/ruff/pull/11017))
|
|
||||||
- Parser: Use empty range when there's "gap" in token source ([#11032](https://github.com/astral-sh/ruff/pull/11032))
|
|
||||||
- \[`ruff`\] Ignore stub functions in `unused-async` (`RUF029`) ([#11026](https://github.com/astral-sh/ruff/pull/11026))
|
|
||||||
- Parser: Expect indented case block instead of match stmt ([#11033](https://github.com/astral-sh/ruff/pull/11033))
|
|
||||||
|
|
||||||
## 0.4.0
|
|
||||||
|
|
||||||
### A new, hand-written parser
|
|
||||||
|
|
||||||
Ruff's new parser is **>2x faster**, which translates to a **20-40% speedup** for all linting and formatting invocations.
|
|
||||||
There's a lot to say about this exciting change, so check out the [blog post](https://astral.sh/blog/ruff-v0.4.0) for more details!
|
|
||||||
|
|
||||||
See [#10036](https://github.com/astral-sh/ruff/pull/10036) for implementation details.
|
|
||||||
|
|
||||||
### A new language server in Rust
|
|
||||||
|
|
||||||
With this release, we also want to highlight our new language server. `ruff server` is a Rust-powered language
|
|
||||||
server that comes built-in with Ruff. It can be used with any editor that supports the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) (LSP).
|
|
||||||
It uses a multi-threaded, lock-free architecture inspired by `rust-analyzer` and it will open the door for a lot
|
|
||||||
of exciting features. It’s also faster than our previous [Python-based language server](https://github.com/astral-sh/ruff-lsp)
|
|
||||||
-- but you probably guessed that already.
|
|
||||||
|
|
||||||
`ruff server` is only in alpha, but it has a lot of features that you can try out today:
|
|
||||||
|
|
||||||
- Lints Python files automatically and shows quick-fixes when available
|
|
||||||
- Formats Python files, with support for range formatting
|
|
||||||
- Comes with commands for quickly performing actions: `ruff.applyAutofix`, `ruff.applyFormat`, and `ruff.applyOrganizeImports`
|
|
||||||
- Supports `source.fixAll` and `source.organizeImports` source actions
|
|
||||||
- Automatically reloads your project configuration when you change it
|
|
||||||
|
|
||||||
To setup `ruff server` with your editor, refer to the [README.md](https://github.com/astral-sh/ruff/blob/main/crates/ruff_server/README.md).
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pycodestyle`\] Do not trigger `E3` rules on `def`s following a function/method with a dummy body ([#10704](https://github.com/astral-sh/ruff/pull/10704))
|
|
||||||
- \[`pylint`\] Implement `invalid-bytes-returned` (`E0308`) ([#10959](https://github.com/astral-sh/ruff/pull/10959))
|
|
||||||
- \[`pylint`\] Implement `invalid-length-returned` (`E0303`) ([#10963](https://github.com/astral-sh/ruff/pull/10963))
|
|
||||||
- \[`pylint`\] Implement `self-cls-assignment` (`W0642`) ([#9267](https://github.com/astral-sh/ruff/pull/9267))
|
|
||||||
- \[`pylint`\] Omit stubs from `invalid-bool` and `invalid-str-return-type` ([#11008](https://github.com/astral-sh/ruff/pull/11008))
|
|
||||||
- \[`ruff`\] New rule `unused-async` (`RUF029`) to detect unneeded `async` keywords on functions ([#9966](https://github.com/astral-sh/ruff/pull/9966))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-bandit`\] Allow `urllib.request.urlopen` calls with static `Request` argument (`S310`) ([#10964](https://github.com/astral-sh/ruff/pull/10964))
|
|
||||||
- \[`flake8-bugbear`\] Treat `raise NotImplemented`-only bodies as stub functions (`B006`) ([#10990](https://github.com/astral-sh/ruff/pull/10990))
|
|
||||||
- \[`flake8-slots`\] Respect same-file `Enum` subclasses (`SLOT000`) ([#11006](https://github.com/astral-sh/ruff/pull/11006))
|
|
||||||
- \[`pylint`\] Support inverted comparisons (`PLR1730`) ([#10920](https://github.com/astral-sh/ruff/pull/10920))
|
|
||||||
|
|
||||||
### Linter
|
|
||||||
|
|
||||||
- Improve handling of builtin symbols in linter rules ([#10919](https://github.com/astral-sh/ruff/pull/10919))
|
|
||||||
- Improve display of rules in `--show-settings` ([#11003](https://github.com/astral-sh/ruff/pull/11003))
|
|
||||||
- Improve inference capabilities of the `BuiltinTypeChecker` ([#10976](https://github.com/astral-sh/ruff/pull/10976))
|
|
||||||
- Resolve classes and functions relative to script name ([#10965](https://github.com/astral-sh/ruff/pull/10965))
|
|
||||||
- Improve performance of `RuleTable::any_enabled` ([#10971](https://github.com/astral-sh/ruff/pull/10971))
|
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
*This section is devoted to updates for our new language server, written in Rust.*
|
|
||||||
|
|
||||||
- Enable ruff-specific source actions ([#10916](https://github.com/astral-sh/ruff/pull/10916))
|
|
||||||
- Refreshes diagnostics for open files when file configuration is changed ([#10988](https://github.com/astral-sh/ruff/pull/10988))
|
|
||||||
- Important errors are now shown as popups ([#10951](https://github.com/astral-sh/ruff/pull/10951))
|
|
||||||
- Introduce settings for directly configuring the linter and formatter ([#10984](https://github.com/astral-sh/ruff/pull/10984))
|
|
||||||
- Resolve configuration for each document individually ([#10950](https://github.com/astral-sh/ruff/pull/10950))
|
|
||||||
- Write a setup guide for Neovim ([#10987](https://github.com/astral-sh/ruff/pull/10987))
|
|
||||||
|
|
||||||
### Configuration
|
|
||||||
|
|
||||||
- Add `RUFF_OUTPUT_FILE` environment variable support ([#10992](https://github.com/astral-sh/ruff/pull/10992))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid `non-augmented-assignment` for reversed, non-commutative operators (`PLR6104`) ([#10909](https://github.com/astral-sh/ruff/pull/10909))
|
|
||||||
- Limit commutative non-augmented-assignments to primitive data types (`PLR6104`) ([#10912](https://github.com/astral-sh/ruff/pull/10912))
|
|
||||||
- Respect `per-file-ignores` for `RUF100` on blanket `# noqa` ([#10908](https://github.com/astral-sh/ruff/pull/10908))
|
|
||||||
- Consider `if` expression for parenthesized with items parsing ([#11010](https://github.com/astral-sh/ruff/pull/11010))
|
|
||||||
- Consider binary expr for parenthesized with items parsing ([#11012](https://github.com/astral-sh/ruff/pull/11012))
|
|
||||||
- Reset `FOR_TARGET` context for all kinds of parentheses ([#11009](https://github.com/astral-sh/ruff/pull/11009))
|
|
||||||
|
|
||||||
## 0.3.7
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-bugbear`\] Implement `loop-iterator-mutation` (`B909`) ([#9578](https://github.com/astral-sh/ruff/pull/9578))
|
|
||||||
- \[`pylint`\] Implement rule to prefer augmented assignment (`PLR6104`) ([#9932](https://github.com/astral-sh/ruff/pull/9932))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid TOCTOU errors in cache initialization ([#10884](https://github.com/astral-sh/ruff/pull/10884))
|
|
||||||
- \[`pylint`\] Recode `nan-comparison` rule to `W0177` ([#10894](https://github.com/astral-sh/ruff/pull/10894))
|
|
||||||
- \[`pylint`\] Reverse min-max logic in `if-stmt-min-max` ([#10890](https://github.com/astral-sh/ruff/pull/10890))
|
|
||||||
|
|
||||||
## 0.3.6
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pylint`\] Implement `bad-staticmethod-argument` (`PLW0211`) ([#10781](https://github.com/astral-sh/ruff/pull/10781))
|
|
||||||
- \[`pylint`\] Implement `if-stmt-min-max` (`PLR1730`, `PLR1731`) ([#10002](https://github.com/astral-sh/ruff/pull/10002))
|
|
||||||
- \[`pyupgrade`\] Replace `str,Enum` multiple inheritance with `StrEnum` `UP042` ([#10713](https://github.com/astral-sh/ruff/pull/10713))
|
|
||||||
- \[`refurb`\] Implement `if-expr-instead-of-or-operator` (`FURB110`) ([#10687](https://github.com/astral-sh/ruff/pull/10687))
|
|
||||||
- \[`refurb`\] Implement `int-on-sliced-str` (`FURB166`) ([#10650](https://github.com/astral-sh/ruff/pull/10650))
|
|
||||||
- \[`refurb`\] Implement `write-whole-file` (`FURB103`) ([#10802](https://github.com/astral-sh/ruff/pull/10802))
|
|
||||||
- \[`refurb`\] Support `itemgetter` in `reimplemented-operator` (`FURB118`) ([#10526](https://github.com/astral-sh/ruff/pull/10526))
|
|
||||||
- \[`flake8_comprehensions`\] Add `sum`/`min`/`max` to unnecessary comprehension check (`C419`) ([#10759](https://github.com/astral-sh/ruff/pull/10759))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`pydocstyle`\] Require capitalizing docstrings where the first sentence is a single word (`D403`) ([#10776](https://github.com/astral-sh/ruff/pull/10776))
|
|
||||||
- \[`pycodestyle`\] Ignore annotated lambdas in class scopes (`E731`) ([#10720](https://github.com/astral-sh/ruff/pull/10720))
|
|
||||||
- \[`flake8-pyi`\] Various improvements to PYI034 ([#10807](https://github.com/astral-sh/ruff/pull/10807))
|
|
||||||
- \[`flake8-slots`\] Flag subclasses of call-based `typing.NamedTuple`s as well as subclasses of `collections.namedtuple()` (`SLOT002`) ([#10808](https://github.com/astral-sh/ruff/pull/10808))
|
|
||||||
- \[`pyflakes`\] Allow forward references in class bases in stub files (`F821`) ([#10779](https://github.com/astral-sh/ruff/pull/10779))
|
|
||||||
- \[`pygrep-hooks`\] Improve `blanket-noqa` error message (`PGH004`) ([#10851](https://github.com/astral-sh/ruff/pull/10851))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Support `FORCE_COLOR` env var ([#10839](https://github.com/astral-sh/ruff/pull/10839))
|
|
||||||
|
|
||||||
### Configuration
|
|
||||||
|
|
||||||
- Support negated patterns in `[extend-]per-file-ignores` ([#10852](https://github.com/astral-sh/ruff/pull/10852))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`flake8-import-conventions`\] Accept non-aliased (but correct) import in `unconventional-import-alias` (`ICN001`) ([#10729](https://github.com/astral-sh/ruff/pull/10729))
|
|
||||||
- \[`flake8-quotes`\] Add semantic model flag when inside f-string replacement field ([#10766](https://github.com/astral-sh/ruff/pull/10766))
|
|
||||||
- \[`pep8-naming`\] Recursively resolve `TypeDicts` for N815 violations ([#10719](https://github.com/astral-sh/ruff/pull/10719))
|
|
||||||
- \[`flake8-quotes`\] Respect `Q00*` ignores in `flake8-quotes` rules ([#10728](https://github.com/astral-sh/ruff/pull/10728))
|
|
||||||
- \[`flake8-simplify`\] Show negated condition in `needless-bool` diagnostics (`SIM103`) ([#10854](https://github.com/astral-sh/ruff/pull/10854))
|
|
||||||
- \[`ruff`\] Use within-scope shadowed bindings in `asyncio-dangling-task` (`RUF006`) ([#10793](https://github.com/astral-sh/ruff/pull/10793))
|
|
||||||
- \[`flake8-pytest-style`\] Fix single-tuple conversion in `pytest-parametrize-values-wrong-type` (`PT007`) ([#10862](https://github.com/astral-sh/ruff/pull/10862))
|
|
||||||
- \[`flake8-return`\] Ignore assignments to annotated variables in `unnecessary-assign` (`RET504`) ([#10741](https://github.com/astral-sh/ruff/pull/10741))
|
|
||||||
- \[`refurb`\] Do not allow any keyword arguments for `read-whole-file` in `rb` mode (`FURB101`) ([#10803](https://github.com/astral-sh/ruff/pull/10803))
|
|
||||||
- \[`pylint`\] Don't recommend decorating staticmethods with `@singledispatch` (`PLE1519`, `PLE1520`) ([#10637](https://github.com/astral-sh/ruff/pull/10637))
|
|
||||||
- \[`pydocstyle`\] Use section name range for all section-related docstring diagnostics ([#10740](https://github.com/astral-sh/ruff/pull/10740))
|
|
||||||
- Respect `# noqa` directives on `__all__` openers ([#10798](https://github.com/astral-sh/ruff/pull/10798))
|
|
||||||
|
|
||||||
## 0.3.5
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`pylint`\] Implement `modified-iterating-set` (`E4703`) ([#10473](https://github.com/astral-sh/ruff/pull/10473))
|
|
||||||
- \[`refurb`\] Implement `for-loop-set-mutations` (`FURB142`) ([#10583](https://github.com/astral-sh/ruff/pull/10583))
|
|
||||||
- \[`refurb`\] Implement `unnecessary-from-float` (`FURB164`) ([#10647](https://github.com/astral-sh/ruff/pull/10647))
|
|
||||||
- \[`refurb`\] Implement `verbose-decimal-constructor` (`FURB157`) ([#10533](https://github.com/astral-sh/ruff/pull/10533))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-comprehensions`\] Handled special case for `C401` which also matches `C416` ([#10596](https://github.com/astral-sh/ruff/pull/10596))
|
|
||||||
- \[`flake8-pyi`\] Mark `unaliased-collections-abc-set-import` fix as "safe" for more cases in stub files (`PYI025`) ([#10547](https://github.com/astral-sh/ruff/pull/10547))
|
|
||||||
- \[`numpy`\] Add `row_stack` to NumPy 2.0 migration rule ([#10646](https://github.com/astral-sh/ruff/pull/10646))
|
|
||||||
- \[`pycodestyle`\] Allow cell magics before an import (`E402`) ([#10545](https://github.com/astral-sh/ruff/pull/10545))
|
|
||||||
- \[`pycodestyle`\] Avoid blank line rules for the first logical line in cell ([#10291](https://github.com/astral-sh/ruff/pull/10291))
|
|
||||||
|
|
||||||
### Configuration
|
|
||||||
|
|
||||||
- Respected nested namespace packages ([#10541](https://github.com/astral-sh/ruff/pull/10541))
|
|
||||||
- \[`flake8-boolean-trap`\] Add setting for user defined allowed boolean trap ([#10531](https://github.com/astral-sh/ruff/pull/10531))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Correctly handle references in `__all__` definitions when renaming symbols in autofixes ([#10527](https://github.com/astral-sh/ruff/pull/10527))
|
|
||||||
- Track ranges of names inside `__all__` definitions ([#10525](https://github.com/astral-sh/ruff/pull/10525))
|
|
||||||
- \[`flake8-bugbear`\] Avoid false positive for usage after `continue` (`B031`) ([#10539](https://github.com/astral-sh/ruff/pull/10539))
|
|
||||||
- \[`flake8-copyright`\] Accept commas in default copyright pattern ([#9498](https://github.com/astral-sh/ruff/pull/9498))
|
|
||||||
- \[`flake8-datetimez`\] Allow f-strings with `%z` for `DTZ007` ([#10651](https://github.com/astral-sh/ruff/pull/10651))
|
|
||||||
- \[`flake8-pytest-style`\] Fix `PT014` autofix for last item in list ([#10532](https://github.com/astral-sh/ruff/pull/10532))
|
|
||||||
- \[`flake8-quotes`\] Ignore `Q000`, `Q001` when string is inside forward ref ([#10585](https://github.com/astral-sh/ruff/pull/10585))
|
|
||||||
- \[`isort`\] Always place non-relative imports after relative imports ([#10669](https://github.com/astral-sh/ruff/pull/10669))
|
|
||||||
- \[`isort`\] Respect Unicode characters in import sorting ([#10529](https://github.com/astral-sh/ruff/pull/10529))
|
|
||||||
- \[`pyflakes`\] Fix F821 false negatives when `from __future__ import annotations` is active (attempt 2) ([#10524](https://github.com/astral-sh/ruff/pull/10524))
|
|
||||||
- \[`pyflakes`\] Make `unnecessary-lambda` an always-unsafe fix ([#10668](https://github.com/astral-sh/ruff/pull/10668))
|
|
||||||
- \[`pylint`\] Fixed false-positive on the rule `PLW1641` (`eq-without-hash`) ([#10566](https://github.com/astral-sh/ruff/pull/10566))
|
|
||||||
- \[`ruff`\] Fix panic in unused `# noqa` removal with multi-byte space (`RUF100`) ([#10682](https://github.com/astral-sh/ruff/pull/10682))
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
- Add PR title format to `CONTRIBUTING.md` ([#10665](https://github.com/astral-sh/ruff/pull/10665))
|
|
||||||
- Fix list markup to include blank lines required ([#10591](https://github.com/astral-sh/ruff/pull/10591))
|
|
||||||
- Put `flake8-logging` next to the other flake8 plugins in registry ([#10587](https://github.com/astral-sh/ruff/pull/10587))
|
|
||||||
- \[`flake8-bandit`\] Update warning message for rule `S305` to address insecure block cipher mode use ([#10602](https://github.com/astral-sh/ruff/pull/10602))
|
|
||||||
- \[`flake8-bugbear`\] Document use of anonymous assignment in `useless-expression` ([#10551](https://github.com/astral-sh/ruff/pull/10551))
|
|
||||||
- \[`flake8-datetimez`\] Clarify error messages and docs for `DTZ` rules ([#10621](https://github.com/astral-sh/ruff/pull/10621))
|
|
||||||
- \[`pycodestyle`\] Use same before vs. after numbers for `space-around-operator` ([#10640](https://github.com/astral-sh/ruff/pull/10640))
|
|
||||||
- \[`ruff`\] Change `quadratic-list-summation` docs to use `iadd` consistently ([#10666](https://github.com/astral-sh/ruff/pull/10666))
|
|
||||||
|
|
||||||
## 0.3.4
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-simplify`\] Detect implicit `else` cases in `needless-bool` (`SIM103`) ([#10414](https://github.com/astral-sh/ruff/pull/10414))
|
|
||||||
- \[`pylint`\] Implement `nan-comparison` (`PLW0117`) ([#10401](https://github.com/astral-sh/ruff/pull/10401))
|
|
||||||
- \[`pylint`\] Implement `nonlocal-and-global` (`E115`) ([#10407](https://github.com/astral-sh/ruff/pull/10407))
|
|
||||||
- \[`pylint`\] Implement `singledispatchmethod-function` (`PLE5120`) ([#10428](https://github.com/astral-sh/ruff/pull/10428))
|
|
||||||
- \[`refurb`\] Implement `list-reverse-copy` (`FURB187`) ([#10212](https://github.com/astral-sh/ruff/pull/10212))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8-pytest-style`\] Add automatic fix for `pytest-parametrize-values-wrong-type` (`PT007`) ([#10461](https://github.com/astral-sh/ruff/pull/10461))
|
|
||||||
- \[`pycodestyle`\] Allow SPDX license headers to exceed the line length (`E501`) ([#10481](https://github.com/astral-sh/ruff/pull/10481))
|
|
||||||
|
|
||||||
### Formatter
|
|
||||||
|
|
||||||
- Fix unstable formatting for trailing subscript end-of-line comment ([#10492](https://github.com/astral-sh/ruff/pull/10492))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- Avoid code comment detection in PEP 723 script tags ([#10464](https://github.com/astral-sh/ruff/pull/10464))
|
|
||||||
- Avoid incorrect tuple transformation in single-element case (`C409`) ([#10491](https://github.com/astral-sh/ruff/pull/10491))
|
|
||||||
- Bug fix: Prevent fully defined links [`name`](link) from being reformatted ([#10442](https://github.com/astral-sh/ruff/pull/10442))
|
|
||||||
- Consider raw source code for `W605` ([#10480](https://github.com/astral-sh/ruff/pull/10480))
|
|
||||||
- Docs: Link inline settings when not part of options section ([#10499](https://github.com/astral-sh/ruff/pull/10499))
|
|
||||||
- Don't treat annotations as redefinitions in `.pyi` files ([#10512](https://github.com/astral-sh/ruff/pull/10512))
|
|
||||||
- Fix `E231` bug: Inconsistent catch compared to pycodestyle, such as when dict nested in list ([#10469](https://github.com/astral-sh/ruff/pull/10469))
|
|
||||||
- Fix pylint upstream categories not showing in docs ([#10441](https://github.com/astral-sh/ruff/pull/10441))
|
|
||||||
- Add missing `Options` references to blank line docs ([#10498](https://github.com/astral-sh/ruff/pull/10498))
|
|
||||||
- 'Revert "F821: Fix false negatives in .py files when `from __future__ import annotations` is active (#10362)"' ([#10513](https://github.com/astral-sh/ruff/pull/10513))
|
|
||||||
- Apply NFKC normalization to unicode identifiers in the lexer ([#10412](https://github.com/astral-sh/ruff/pull/10412))
|
|
||||||
- Avoid failures due to non-deterministic binding ordering ([#10478](https://github.com/astral-sh/ruff/pull/10478))
|
|
||||||
- \[`flake8-bugbear`\] Allow tuples of exceptions (`B030`) ([#10437](https://github.com/astral-sh/ruff/pull/10437))
|
|
||||||
- \[`flake8-quotes`\] Avoid syntax errors due to invalid quotes (`Q000, Q002`) ([#10199](https://github.com/astral-sh/ruff/pull/10199))
|
|
||||||
|
|
||||||
## 0.3.3
|
|
||||||
|
|
||||||
### Preview features
|
|
||||||
|
|
||||||
- \[`flake8-bandit`\]: Implement `S610` rule ([#10316](https://github.com/astral-sh/ruff/pull/10316))
|
|
||||||
- \[`pycodestyle`\] Implement `blank-line-at-end-of-file` (`W391`) ([#10243](https://github.com/astral-sh/ruff/pull/10243))
|
|
||||||
- \[`pycodestyle`\] Implement `redundant-backslash` (`E502`) ([#10292](https://github.com/astral-sh/ruff/pull/10292))
|
|
||||||
- \[`pylint`\] - implement `redeclared-assigned-name` (`W0128`) ([#9268](https://github.com/astral-sh/ruff/pull/9268))
|
|
||||||
|
|
||||||
### Rule changes
|
|
||||||
|
|
||||||
- \[`flake8_comprehensions`\] Handled special case for `C400` which also matches `C416` ([#10419](https://github.com/astral-sh/ruff/pull/10419))
|
|
||||||
- \[`flake8-bandit`\] Implement upstream updates for `S311`, `S324` and `S605` ([#10313](https://github.com/astral-sh/ruff/pull/10313))
|
|
||||||
- \[`pyflakes`\] Remove `F401` fix for `__init__` imports by default and allow opt-in to unsafe fix ([#10365](https://github.com/astral-sh/ruff/pull/10365))
|
|
||||||
- \[`pylint`\] Implement `invalid-bool-return-type` (`E304`) ([#10377](https://github.com/astral-sh/ruff/pull/10377))
|
|
||||||
- \[`pylint`\] Include builtin warnings in useless-exception-statement (`PLW0133`) ([#10394](https://github.com/astral-sh/ruff/pull/10394))
|
|
||||||
|
|
||||||
### CLI
|
|
||||||
|
|
||||||
- Add message on success to `ruff check` ([#8631](https://github.com/astral-sh/ruff/pull/8631))
|
|
||||||
|
|
||||||
### Bug fixes
|
|
||||||
|
|
||||||
- \[`PIE970`\] Allow trailing ellipsis in `typing.TYPE_CHECKING` ([#10413](https://github.com/astral-sh/ruff/pull/10413))
|
|
||||||
- Avoid `TRIO115` if the argument is a variable ([#10376](https://github.com/astral-sh/ruff/pull/10376))
|
|
||||||
- \[`F811`\] Avoid removing shadowed imports that point to different symbols ([#10387](https://github.com/astral-sh/ruff/pull/10387))
|
|
||||||
- Fix `F821` and `F822` false positives in `.pyi` files ([#10341](https://github.com/astral-sh/ruff/pull/10341))
|
|
||||||
- Fix `F821` false negatives in `.py` files when `from __future__ import annotations` is active ([#10362](https://github.com/astral-sh/ruff/pull/10362))
|
|
||||||
- Fix case where `Indexer` fails to identify continuation preceded by newline #10351 ([#10354](https://github.com/astral-sh/ruff/pull/10354))
|
|
||||||
- Sort hash maps in `Settings` display ([#10370](https://github.com/astral-sh/ruff/pull/10370))
|
|
||||||
- Track conditional deletions in the semantic model ([#10415](https://github.com/astral-sh/ruff/pull/10415))
|
|
||||||
- \[`C413`\] Wrap expressions in parentheses when negating ([#10346](https://github.com/astral-sh/ruff/pull/10346))
|
|
||||||
- \[`pycodestyle`\] Do not ignore lines before the first logical line in blank lines rules. ([#10382](https://github.com/astral-sh/ruff/pull/10382))
|
|
||||||
- \[`pycodestyle`\] Do not trigger `E225` and `E275` when the next token is a ')' ([#10315](https://github.com/astral-sh/ruff/pull/10315))
|
|
||||||
- \[`pylint`\] Avoid false-positive slot non-assignment for `__dict__` (`PLE0237`) ([#10348](https://github.com/astral-sh/ruff/pull/10348))
|
|
||||||
- Gate f-string struct size test for Rustc \< 1.76 ([#10371](https://github.com/astral-sh/ruff/pull/10371))
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
- Use `ruff.toml` format in README ([#10393](https://github.com/astral-sh/ruff/pull/10393))
|
|
||||||
- \[`RUF008`\] Make it clearer that a mutable default in a dataclass is only valid if it is typed as a ClassVar ([#10395](https://github.com/astral-sh/ruff/pull/10395))
|
|
||||||
- \[`pylint`\] Extend docs and test in `invalid-str-return-type` (`E307`) ([#10400](https://github.com/astral-sh/ruff/pull/10400))
|
|
||||||
- Remove `.` from `check` and `format` commands ([#10217](https://github.com/astral-sh/ruff/pull/10217))
|
|
||||||
|
|
||||||
## 0.3.2
|
## 0.3.2
|
||||||
|
|
||||||
### Preview features
|
### Preview features
|
||||||
@@ -755,7 +17,7 @@ To setup `ruff server` with your editor, refer to the [README.md](https://github
|
|||||||
- Fix unstable `with` items formatting ([#10274](https://github.com/astral-sh/ruff/pull/10274))
|
- Fix unstable `with` items formatting ([#10274](https://github.com/astral-sh/ruff/pull/10274))
|
||||||
- Avoid repeating function calls in f-string conversions ([#10265](https://github.com/astral-sh/ruff/pull/10265))
|
- Avoid repeating function calls in f-string conversions ([#10265](https://github.com/astral-sh/ruff/pull/10265))
|
||||||
- Fix E203 false positive for slices in format strings ([#10280](https://github.com/astral-sh/ruff/pull/10280))
|
- Fix E203 false positive for slices in format strings ([#10280](https://github.com/astral-sh/ruff/pull/10280))
|
||||||
- Fix incorrect `Parameter` range for `*args` and `**kwargs` ([#10283](https://github.com/astral-sh/ruff/pull/10283))
|
- Fix incorrect `Parameter` range for `*args` and `**kwargs` ([#10283](https://github.com/astral-sh/ruff/pull/10283))
|
||||||
- Treat `typing.Annotated` subscripts as type definitions ([#10285](https://github.com/astral-sh/ruff/pull/10285))
|
- Treat `typing.Annotated` subscripts as type definitions ([#10285](https://github.com/astral-sh/ruff/pull/10285))
|
||||||
|
|
||||||
## 0.3.1
|
## 0.3.1
|
||||||
@@ -863,7 +125,8 @@ This release introduces the Ruff 2024.2 style, stabilizing the following changes
|
|||||||
Highlights include:
|
Highlights include:
|
||||||
|
|
||||||
- Initial support formatting f-strings (in `--preview`).
|
- Initial support formatting f-strings (in `--preview`).
|
||||||
- Support for overriding arbitrary configuration options via the CLI through an expanded `--config` argument (e.g., `--config "lint.isort.combine-as-imports=false"`).
|
- Support for overriding arbitrary configuration options via the CLI through an expanded `--config`
|
||||||
|
argument (e.g., `--config "lint.isort.combine-as-imports=false"`).
|
||||||
- Significant performance improvements in Ruff's lexer, parser, and lint rules.
|
- Significant performance improvements in Ruff's lexer, parser, and lint rules.
|
||||||
|
|
||||||
### Preview features
|
### Preview features
|
||||||
@@ -1511,7 +774,7 @@ docstrings via the `docstring-code-format` setting.
|
|||||||
- \[`pylint`\] Default `max-positional-args` to `max-args` ([#8998](https://github.com/astral-sh/ruff/pull/8998))
|
- \[`pylint`\] Default `max-positional-args` to `max-args` ([#8998](https://github.com/astral-sh/ruff/pull/8998))
|
||||||
- \[`pylint`\] Add `allow-dunder-method-names` setting for `bad-dunder-method-name` (`PLW3201`) ([#8812](https://github.com/astral-sh/ruff/pull/8812))
|
- \[`pylint`\] Add `allow-dunder-method-names` setting for `bad-dunder-method-name` (`PLW3201`) ([#8812](https://github.com/astral-sh/ruff/pull/8812))
|
||||||
- \[`isort`\] Add support for `from-first` setting ([#8663](https://github.com/astral-sh/ruff/pull/8663))
|
- \[`isort`\] Add support for `from-first` setting ([#8663](https://github.com/astral-sh/ruff/pull/8663))
|
||||||
- \[`isort`\] Add support for `length-sort` settings ([#8841](https://github.com/astral-sh/ruff/pull/8841))
|
- \[`isort`\] Add support for `length-sort` settings ([#8841](https://github.com/astral-sh/ruff/pull/8841))
|
||||||
|
|
||||||
### Bug fixes
|
### Bug fixes
|
||||||
|
|
||||||
@@ -1640,7 +903,7 @@ docstrings via the `docstring-code-format` setting.
|
|||||||
- \[`flake8-trio`\] Implement `TRIO115` ([#8486](https://github.com/astral-sh/ruff/pull/8486))
|
- \[`flake8-trio`\] Implement `TRIO115` ([#8486](https://github.com/astral-sh/ruff/pull/8486))
|
||||||
- \[`refurb`\] Implement `type-none-comparison` (`FURB169`) ([#8487](https://github.com/astral-sh/ruff/pull/8487))
|
- \[`refurb`\] Implement `type-none-comparison` (`FURB169`) ([#8487](https://github.com/astral-sh/ruff/pull/8487))
|
||||||
- Flag all comparisons against builtin types in `E721` ([#8491](https://github.com/astral-sh/ruff/pull/8491))
|
- Flag all comparisons against builtin types in `E721` ([#8491](https://github.com/astral-sh/ruff/pull/8491))
|
||||||
- Make `SIM118` fix as safe when the expression is a known dictionary ([#8525](https://github.com/astral-sh/ruff/pull/8525))
|
- Make `SIM118` fix as safe when the expression is a known dictionary ([#8525](https://github.com/astral-sh/ruff/pull/8525))
|
||||||
|
|
||||||
### Formatter
|
### Formatter
|
||||||
|
|
||||||
@@ -1808,7 +1071,7 @@ Try it today with `ruff format`! [Check out the blog post](https://astral.sh/blo
|
|||||||
- Add `backports.strenum` to `deprecated-imports` ([#8113](https://github.com/astral-sh/ruff/pull/8113))
|
- Add `backports.strenum` to `deprecated-imports` ([#8113](https://github.com/astral-sh/ruff/pull/8113))
|
||||||
- Update `SIM112` to ignore `https_proxy`, `http_proxy`, and `no_proxy` ([#8140](https://github.com/astral-sh/ruff/pull/8140))
|
- Update `SIM112` to ignore `https_proxy`, `http_proxy`, and `no_proxy` ([#8140](https://github.com/astral-sh/ruff/pull/8140))
|
||||||
- Update fix for `literal-membership` (`PLR6201`) to be unsafe ([#8097](https://github.com/astral-sh/ruff/pull/8097))
|
- Update fix for `literal-membership` (`PLR6201`) to be unsafe ([#8097](https://github.com/astral-sh/ruff/pull/8097))
|
||||||
- Update fix for `mutable-argument-defaults` (`B006`) to be unsafe ([#8108](https://github.com/astral-sh/ruff/pull/8108))
|
- Update fix for `mutable-argument-defaults` (`B006`) to be unsafe ([#8108](https://github.com/astral-sh/ruff/pull/8108))
|
||||||
|
|
||||||
### Formatter
|
### Formatter
|
||||||
|
|
||||||
@@ -1936,7 +1199,7 @@ Read Ruff's new [versioning policy](https://docs.astral.sh/ruff/versioning/).
|
|||||||
- \[`refurb`\] Add `single-item-membership-test` (`FURB171`) ([#7815](https://github.com/astral-sh/ruff/pull/7815))
|
- \[`refurb`\] Add `single-item-membership-test` (`FURB171`) ([#7815](https://github.com/astral-sh/ruff/pull/7815))
|
||||||
- \[`pylint`\] Add `and-or-ternary` (`R1706`) ([#7811](https://github.com/astral-sh/ruff/pull/7811))
|
- \[`pylint`\] Add `and-or-ternary` (`R1706`) ([#7811](https://github.com/astral-sh/ruff/pull/7811))
|
||||||
|
|
||||||
*New rules are added in [preview](https://docs.astral.sh/ruff/preview/).*
|
_New rules are added in [preview](https://docs.astral.sh/ruff/preview/)._
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
|
|||||||
@@ -33,18 +33,27 @@ Welcome! We're happy to have you here. Thank you in advance for your contributio
|
|||||||
|
|
||||||
## The Basics
|
## The Basics
|
||||||
|
|
||||||
Ruff welcomes contributions in the form of pull requests.
|
Ruff welcomes contributions in the form of Pull Requests.
|
||||||
|
|
||||||
For small changes (e.g., bug fixes), feel free to submit a PR.
|
For small changes (e.g., bug fixes), feel free to submit a PR.
|
||||||
|
|
||||||
For larger changes (e.g., new lint rules, new functionality, new configuration options), consider
|
For larger changes (e.g., new lint rules, new functionality, new configuration options), consider
|
||||||
creating an [**issue**](https://github.com/astral-sh/ruff/issues) outlining your proposed change.
|
creating an [**issue**](https://github.com/astral-sh/ruff/issues) outlining your proposed change.
|
||||||
You can also join us on [Discord](https://discord.com/invite/astral-sh) to discuss your idea with the
|
You can also join us on [**Discord**](https://discord.com/invite/astral-sh) to discuss your idea with the
|
||||||
community. We've labeled [beginner-friendly tasks](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)
|
community. We've labeled [beginner-friendly tasks](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)
|
||||||
in the issue tracker, along with [bugs](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
|
in the issue tracker, along with [bugs](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
|
||||||
and [improvements](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3Aaccepted)
|
and [improvements](https://github.com/astral-sh/ruff/issues?q=is%3Aissue+is%3Aopen+label%3Aaccepted)
|
||||||
that are ready for contributions.
|
that are ready for contributions.
|
||||||
|
|
||||||
|
If you're looking for a place to start, we recommend implementing a new lint rule (see:
|
||||||
|
[_Adding a new lint rule_](#example-adding-a-new-lint-rule), which will allow you to learn from and
|
||||||
|
pattern-match against the examples in the existing codebase. Many lint rules are inspired by
|
||||||
|
existing Python plugins, which can be used as a reference implementation.
|
||||||
|
|
||||||
|
As a concrete example: consider taking on one of the rules from the [`flake8-pyi`](https://github.com/astral-sh/ruff/issues/848)
|
||||||
|
plugin, and looking to the originating [Python source](https://github.com/PyCQA/flake8-pyi) for
|
||||||
|
guidance.
|
||||||
|
|
||||||
If you have suggestions on how we might improve the contributing documentation, [let us know](https://github.com/astral-sh/ruff/discussions/5693)!
|
If you have suggestions on how we might improve the contributing documentation, [let us know](https://github.com/astral-sh/ruff/discussions/5693)!
|
||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
@@ -98,11 +107,9 @@ RUFF_UPDATE_SCHEMA=1 cargo test # Rust testing and updating ruff.schema.json
|
|||||||
pre-commit run --all-files --show-diff-on-failure # Rust and Python formatting, Markdown and Python linting, etc.
|
pre-commit run --all-files --show-diff-on-failure # Rust and Python formatting, Markdown and Python linting, etc.
|
||||||
```
|
```
|
||||||
|
|
||||||
These checks will run on GitHub Actions when you open your pull request, but running them locally
|
These checks will run on GitHub Actions when you open your Pull Request, but running them locally
|
||||||
will save you time and expedite the merge process.
|
will save you time and expedite the merge process.
|
||||||
|
|
||||||
If you're using VS Code, you can also install the recommended [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) extension to get these checks while editing.
|
|
||||||
|
|
||||||
Note that many code changes also require updating the snapshot tests, which is done interactively
|
Note that many code changes also require updating the snapshot tests, which is done interactively
|
||||||
after running `cargo test` like so:
|
after running `cargo test` like so:
|
||||||
|
|
||||||
@@ -110,14 +117,7 @@ after running `cargo test` like so:
|
|||||||
cargo insta review
|
cargo insta review
|
||||||
```
|
```
|
||||||
|
|
||||||
If your pull request relates to a specific lint rule, include the category and rule code in the
|
Your Pull Request will be reviewed by a maintainer, which may involve a few rounds of iteration
|
||||||
title, as in the following examples:
|
|
||||||
|
|
||||||
- \[`flake8-bugbear`\] Avoid false positive for usage after `continue` (`B031`)
|
|
||||||
- \[`flake8-simplify`\] Detect implicit `else` cases in `needless-bool` (`SIM103`)
|
|
||||||
- \[`pycodestyle`\] Implement `redundant-backslash` (`E502`)
|
|
||||||
|
|
||||||
Your pull request will be reviewed by a maintainer, which may involve a few rounds of iteration
|
|
||||||
prior to merging.
|
prior to merging.
|
||||||
|
|
||||||
### Project Structure
|
### Project Structure
|
||||||
@@ -125,8 +125,8 @@ prior to merging.
|
|||||||
Ruff is structured as a monorepo with a [flat crate structure](https://matklad.github.io/2021/08/22/large-rust-workspaces.html),
|
Ruff is structured as a monorepo with a [flat crate structure](https://matklad.github.io/2021/08/22/large-rust-workspaces.html),
|
||||||
such that all crates are contained in a flat `crates` directory.
|
such that all crates are contained in a flat `crates` directory.
|
||||||
|
|
||||||
The vast majority of the code, including all lint rules, lives in the `ruff_linter` crate (located
|
The vast majority of the code, including all lint rules, lives in the `ruff` crate (located at
|
||||||
at `crates/ruff_linter`). As a contributor, that's the crate that'll be most relevant to you.
|
`crates/ruff_linter`). As a contributor, that's the crate that'll be most relevant to you.
|
||||||
|
|
||||||
At the time of writing, the repository includes the following crates:
|
At the time of writing, the repository includes the following crates:
|
||||||
|
|
||||||
@@ -199,14 +199,11 @@ and calling out to lint rule analyzer functions as it goes.
|
|||||||
If you need to inspect the AST, you can run `cargo dev print-ast` with a Python file. Grep
|
If you need to inspect the AST, you can run `cargo dev print-ast` with a Python file. Grep
|
||||||
for the `Diagnostic::new` invocations to understand how other, similar rules are implemented.
|
for the `Diagnostic::new` invocations to understand how other, similar rules are implemented.
|
||||||
|
|
||||||
Once you're satisfied with your code, add tests for your rule
|
Once you're satisfied with your code, add tests for your rule. See [rule testing](#rule-testing-fixtures-and-snapshots)
|
||||||
(see: [rule testing](#rule-testing-fixtures-and-snapshots)), and regenerate the documentation and
|
for more details.
|
||||||
associated assets (like our JSON Schema) with `cargo dev generate-all`.
|
|
||||||
|
|
||||||
Finally, submit a pull request, and include the category, rule name, and rule code in the title, as
|
Finally, regenerate the documentation and other generated assets (like our JSON Schema) with:
|
||||||
in:
|
`cargo dev generate-all`.
|
||||||
|
|
||||||
> \[`pycodestyle`\] Implement `redundant-backslash` (`E502`)
|
|
||||||
|
|
||||||
#### Rule naming convention
|
#### Rule naming convention
|
||||||
|
|
||||||
@@ -346,21 +343,22 @@ even patch releases may contain [non-backwards-compatible changes](https://semve
|
|||||||
1. Run `cargo check`. This should update the lock file with new versions.
|
1. Run `cargo check`. This should update the lock file with new versions.
|
||||||
1. Create a pull request with the changelog and version updates
|
1. Create a pull request with the changelog and version updates
|
||||||
1. Merge the PR
|
1. Merge the PR
|
||||||
1. Run the [release workflow](https://github.com/astral-sh/ruff/actions/workflows/release.yml) with:
|
1. Run the [release workflow](https://github.com/astral-sh/ruff/actions/workflows/release.yaml) with:
|
||||||
- The new version number (without starting `v`)
|
- The new version number (without starting `v`)
|
||||||
|
- The commit hash of the merged release pull request on `main`
|
||||||
1. The release workflow will do the following:
|
1. The release workflow will do the following:
|
||||||
1. Build all the assets. If this fails (even though we tested in step 4), we haven't tagged or
|
1. Build all the assets. If this fails (even though we tested in step 4), we haven't tagged or
|
||||||
uploaded anything, you can restart after pushing a fix. If you just need to rerun the build,
|
uploaded anything, you can restart after pushing a fix.
|
||||||
make sure you're [re-running all the failed
|
|
||||||
jobs](https://docs.github.com/en/actions/managing-workflow-runs/re-running-workflows-and-jobs#re-running-failed-jobs-in-a-workflow) and not just a single failed job.
|
|
||||||
1. Upload to PyPI.
|
1. Upload to PyPI.
|
||||||
1. Create and push the Git tag (as extracted from `pyproject.toml`). We create the Git tag only
|
1. Create and push the Git tag (as extracted from `pyproject.toml`). We create the Git tag only
|
||||||
after building the wheels and uploading to PyPI, since we can't delete or modify the tag ([#4468](https://github.com/astral-sh/ruff/issues/4468)).
|
after building the wheels and uploading to PyPI, since we can't delete or modify the tag ([#4468](https://github.com/astral-sh/ruff/issues/4468)).
|
||||||
1. Attach artifacts to draft GitHub release
|
1. Attach artifacts to draft GitHub release
|
||||||
1. Trigger downstream repositories. This can fail non-catastrophically, as we can run any
|
1. Trigger downstream repositories. This can fail non-catastrophically, as we can run any
|
||||||
downstream jobs manually if needed.
|
downstream jobs manually if needed.
|
||||||
1. Verify the GitHub release:
|
1. Publish the GitHub release
|
||||||
1. The Changelog should match the content of `CHANGELOG.md`
|
1. Open the draft release in the GitHub release section
|
||||||
|
1. Copy the changelog for the release into the GitHub release
|
||||||
|
- See previous releases for formatting of section headers
|
||||||
1. Append the contributors from the `bump.sh` script
|
1. Append the contributors from the `bump.sh` script
|
||||||
1. If needed, [update the schemastore](https://github.com/astral-sh/ruff/blob/main/scripts/update_schemastore.py).
|
1. If needed, [update the schemastore](https://github.com/astral-sh/ruff/blob/main/scripts/update_schemastore.py).
|
||||||
1. One can determine if an update is needed when
|
1. One can determine if an update is needed when
|
||||||
@@ -638,11 +636,11 @@ Otherwise, follow the instructions from the linux section.
|
|||||||
`cargo dev` is a shortcut for `cargo run --package ruff_dev --bin ruff_dev`. You can run some useful
|
`cargo dev` is a shortcut for `cargo run --package ruff_dev --bin ruff_dev`. You can run some useful
|
||||||
utils with it:
|
utils with it:
|
||||||
|
|
||||||
- `cargo dev print-ast <file>`: Print the AST of a python file using Ruff's
|
- `cargo dev print-ast <file>`: Print the AST of a python file using the
|
||||||
[Python parser](https://github.com/astral-sh/ruff/tree/main/crates/ruff_python_parser).
|
[RustPython parser](https://github.com/astral-sh/ruff/tree/main/crates/ruff_python_parser) that is
|
||||||
For `if True: pass # comment`, you can see the syntax tree, the byte offsets for start and
|
mainly used in Ruff. For `if True: pass # comment`, you can see the syntax tree, the byte offsets
|
||||||
stop of each node and also how the `:` token, the comment and whitespace are not represented
|
for start and stop of each node and also how the `:` token, the comment and whitespace are not
|
||||||
anymore:
|
represented anymore:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
[
|
[
|
||||||
@@ -815,8 +813,8 @@ To understand Ruff's import categorization system, we first need to define two c
|
|||||||
"project root".)
|
"project root".)
|
||||||
- "Package root": The top-most directory defining the Python package that includes a given Python
|
- "Package root": The top-most directory defining the Python package that includes a given Python
|
||||||
file. To find the package root for a given Python file, traverse up its parent directories until
|
file. To find the package root for a given Python file, traverse up its parent directories until
|
||||||
you reach a parent directory that doesn't contain an `__init__.py` file (and isn't in a subtree
|
you reach a parent directory that doesn't contain an `__init__.py` file (and isn't marked as
|
||||||
marked as a [namespace package](https://docs.astral.sh/ruff/settings/#namespace-packages)); take the directory
|
a [namespace package](https://docs.astral.sh/ruff/settings/#namespace-packages)); take the directory
|
||||||
just before that, i.e., the first directory in the package.
|
just before that, i.e., the first directory in the package.
|
||||||
|
|
||||||
For example, given:
|
For example, given:
|
||||||
|
|||||||
1358
Cargo.lock
generated
1358
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
167
Cargo.toml
167
Cargo.toml
@@ -4,7 +4,7 @@ resolver = "2"
|
|||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.71"
|
||||||
homepage = "https://docs.astral.sh/ruff"
|
homepage = "https://docs.astral.sh/ruff"
|
||||||
documentation = "https://docs.astral.sh/ruff"
|
documentation = "https://docs.astral.sh/ruff"
|
||||||
repository = "https://github.com/astral-sh/ruff"
|
repository = "https://github.com/astral-sh/ruff"
|
||||||
@@ -12,146 +12,109 @@ authors = ["Charlie Marsh <charlie.r.marsh@gmail.com>"]
|
|||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
ruff = { path = "crates/ruff" }
|
aho-corasick = { version = "1.1.2" }
|
||||||
ruff_cache = { path = "crates/ruff_cache" }
|
|
||||||
ruff_db = { path = "crates/ruff_db" }
|
|
||||||
ruff_diagnostics = { path = "crates/ruff_diagnostics" }
|
|
||||||
ruff_formatter = { path = "crates/ruff_formatter" }
|
|
||||||
ruff_index = { path = "crates/ruff_index" }
|
|
||||||
ruff_linter = { path = "crates/ruff_linter" }
|
|
||||||
ruff_macros = { path = "crates/ruff_macros" }
|
|
||||||
ruff_notebook = { path = "crates/ruff_notebook" }
|
|
||||||
ruff_python_ast = { path = "crates/ruff_python_ast" }
|
|
||||||
ruff_python_codegen = { path = "crates/ruff_python_codegen" }
|
|
||||||
ruff_python_formatter = { path = "crates/ruff_python_formatter" }
|
|
||||||
ruff_python_index = { path = "crates/ruff_python_index" }
|
|
||||||
ruff_python_literal = { path = "crates/ruff_python_literal" }
|
|
||||||
ruff_python_parser = { path = "crates/ruff_python_parser" }
|
|
||||||
ruff_python_semantic = { path = "crates/ruff_python_semantic" }
|
|
||||||
ruff_python_stdlib = { path = "crates/ruff_python_stdlib" }
|
|
||||||
ruff_python_trivia = { path = "crates/ruff_python_trivia" }
|
|
||||||
ruff_server = { path = "crates/ruff_server" }
|
|
||||||
ruff_source_file = { path = "crates/ruff_source_file" }
|
|
||||||
ruff_text_size = { path = "crates/ruff_text_size" }
|
|
||||||
ruff_workspace = { path = "crates/ruff_workspace" }
|
|
||||||
|
|
||||||
red_knot_module_resolver = { path = "crates/red_knot_module_resolver" }
|
|
||||||
|
|
||||||
aho-corasick = { version = "1.1.3" }
|
|
||||||
annotate-snippets = { version = "0.9.2", features = ["color"] }
|
annotate-snippets = { version = "0.9.2", features = ["color"] }
|
||||||
anyhow = { version = "1.0.80" }
|
anyhow = { version = "1.0.80" }
|
||||||
argfile = { version = "0.2.0" }
|
argfile = { version = "0.1.6" }
|
||||||
|
assert_cmd = { version = "2.0.13" }
|
||||||
bincode = { version = "1.3.3" }
|
bincode = { version = "1.3.3" }
|
||||||
bitflags = { version = "2.5.0" }
|
bitflags = { version = "2.4.1" }
|
||||||
bstr = { version = "1.9.1" }
|
bstr = { version = "1.9.1" }
|
||||||
cachedir = { version = "0.3.1" }
|
cachedir = { version = "0.3.1" }
|
||||||
camino = { version = "1.1.7" }
|
|
||||||
chrono = { version = "0.4.35", default-features = false, features = ["clock"] }
|
chrono = { version = "0.4.35", default-features = false, features = ["clock"] }
|
||||||
clap = { version = "4.5.3", features = ["derive"] }
|
clap = { version = "4.5.2", features = ["derive"] }
|
||||||
clap_complete_command = { version = "0.5.1" }
|
clap_complete_command = { version = "0.5.1" }
|
||||||
clearscreen = { version = "3.0.0" }
|
clearscreen = { version = "2.0.0" }
|
||||||
codspeed-criterion-compat = { version = "2.6.0", default-features = false }
|
codspeed-criterion-compat = { version = "2.4.0", default-features = false }
|
||||||
colored = { version = "2.1.0" }
|
colored = { version = "2.1.0" }
|
||||||
|
configparser = { version = "3.0.3" }
|
||||||
console_error_panic_hook = { version = "0.1.7" }
|
console_error_panic_hook = { version = "0.1.7" }
|
||||||
console_log = { version = "1.0.0" }
|
console_log = { version = "1.0.0" }
|
||||||
countme = { version = "3.0.1" }
|
countme = { version = "3.0.1" }
|
||||||
criterion = { version = "0.5.1", default-features = false }
|
criterion = { version = "0.5.1", default-features = false }
|
||||||
crossbeam = { version = "0.8.4" }
|
crossbeam = { version = "0.8.4" }
|
||||||
dashmap = { version = "5.5.3" }
|
dirs = { version = "5.0.0" }
|
||||||
drop_bomb = { version = "0.1.5" }
|
drop_bomb = { version = "0.1.5" }
|
||||||
env_logger = { version = "0.11.0" }
|
env_logger = { version = "0.10.1" }
|
||||||
etcetera = { version = "0.8.0" }
|
|
||||||
fern = { version = "0.6.1" }
|
fern = { version = "0.6.1" }
|
||||||
filetime = { version = "0.2.23" }
|
filetime = { version = "0.2.23" }
|
||||||
|
fs-err = { version = "2.11.0" }
|
||||||
glob = { version = "0.3.1" }
|
glob = { version = "0.3.1" }
|
||||||
globset = { version = "0.4.14" }
|
globset = { version = "0.4.14" }
|
||||||
hashbrown = "0.14.3"
|
hexf-parse = { version = "0.2.1" }
|
||||||
ignore = { version = "0.4.22" }
|
ignore = { version = "0.4.22" }
|
||||||
imara-diff = { version = "0.1.5" }
|
imara-diff = { version = "0.1.5" }
|
||||||
imperative = { version = "1.0.4" }
|
imperative = { version = "1.0.4" }
|
||||||
indexmap = { version = "2.2.6" }
|
|
||||||
indicatif = { version = "0.17.8" }
|
indicatif = { version = "0.17.8" }
|
||||||
indoc = { version = "2.0.4" }
|
indoc = { version = "2.0.4" }
|
||||||
insta = { version = "1.35.1" }
|
insta = { version = "1.35.1", feature = ["filters", "glob"] }
|
||||||
insta-cmd = { version = "0.6.0" }
|
insta-cmd = { version = "0.4.0" }
|
||||||
is-macro = { version = "0.3.5" }
|
is-macro = { version = "0.3.5" }
|
||||||
is-wsl = { version = "0.4.0" }
|
is-wsl = { version = "0.4.0" }
|
||||||
itertools = { version = "0.13.0" }
|
itertools = { version = "0.12.1" }
|
||||||
js-sys = { version = "0.3.69" }
|
js-sys = { version = "0.3.69" }
|
||||||
jod-thread = { version = "0.1.2" }
|
jod-thread = { version = "0.1.2" }
|
||||||
|
lalrpop-util = { version = "0.20.0", default-features = false }
|
||||||
|
lexical-parse-float = { version = "0.8.0", features = ["format"] }
|
||||||
libc = { version = "0.2.153" }
|
libc = { version = "0.2.153" }
|
||||||
libcst = { version = "1.1.0", default-features = false }
|
libcst = { version = "1.1.0", default-features = false }
|
||||||
log = { version = "0.4.17" }
|
log = { version = "0.4.17" }
|
||||||
lsp-server = { version = "0.7.6" }
|
lsp-server = { version = "0.7.6" }
|
||||||
lsp-types = { git = "https://github.com/astral-sh/lsp-types.git", rev = "3512a9f", features = [
|
lsp-types = { version = "0.95.0", features = ["proposed"] }
|
||||||
"proposed",
|
|
||||||
] }
|
|
||||||
matchit = { version = "0.8.1" }
|
|
||||||
memchr = { version = "2.7.1" }
|
memchr = { version = "2.7.1" }
|
||||||
mimalloc = { version = "0.1.39" }
|
mimalloc = { version = "0.1.39" }
|
||||||
natord = { version = "1.0.9" }
|
natord = { version = "1.0.9" }
|
||||||
notify = { version = "6.1.1" }
|
notify = { version = "6.1.1" }
|
||||||
once_cell = { version = "1.19.0" }
|
once_cell = { version = "1.19.0" }
|
||||||
path-absolutize = { version = "3.1.1" }
|
path-absolutize = { version = "3.1.1" }
|
||||||
path-slash = { version = "0.2.1" }
|
|
||||||
pathdiff = { version = "0.2.1" }
|
pathdiff = { version = "0.2.1" }
|
||||||
parking_lot = "0.12.1"
|
pep440_rs = { version = "0.4.0", features = ["serde"] }
|
||||||
pep440_rs = { version = "0.6.0", features = ["serde"] }
|
|
||||||
pretty_assertions = "1.3.0"
|
pretty_assertions = "1.3.0"
|
||||||
proc-macro2 = { version = "1.0.79" }
|
proc-macro2 = { version = "1.0.78" }
|
||||||
pyproject-toml = { version = "0.9.0" }
|
pyproject-toml = { version = "0.9.0" }
|
||||||
quick-junit = { version = "0.4.0" }
|
quick-junit = { version = "0.3.5" }
|
||||||
quote = { version = "1.0.23" }
|
quote = { version = "1.0.23" }
|
||||||
rand = { version = "0.8.5" }
|
rand = { version = "0.8.5" }
|
||||||
rayon = { version = "1.10.0" }
|
rayon = { version = "1.8.1" }
|
||||||
regex = { version = "1.10.2" }
|
regex = { version = "1.10.2" }
|
||||||
rustc-hash = { version = "2.0.0" }
|
result-like = { version = "0.5.0" }
|
||||||
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "f706aa2d32d473ee633a77c1af01d180c85da308" }
|
rustc-hash = { version = "1.1.0" }
|
||||||
schemars = { version = "0.8.16" }
|
schemars = { version = "0.8.16" }
|
||||||
seahash = { version = "4.1.0" }
|
seahash = { version = "4.1.0" }
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde-wasm-bindgen = { version = "0.6.4" }
|
serde-wasm-bindgen = { version = "0.6.4" }
|
||||||
serde_json = { version = "1.0.113" }
|
serde_json = { version = "1.0.113" }
|
||||||
serde_test = { version = "1.0.152" }
|
serde_test = { version = "1.0.152" }
|
||||||
serde_with = { version = "3.6.0", default-features = false, features = [
|
serde_with = { version = "3.6.0", default-features = false, features = ["macros"] }
|
||||||
"macros",
|
|
||||||
] }
|
|
||||||
shellexpand = { version = "3.0.0" }
|
shellexpand = { version = "3.0.0" }
|
||||||
|
shlex = { version = "1.3.0" }
|
||||||
similar = { version = "2.4.0", features = ["inline"] }
|
similar = { version = "2.4.0", features = ["inline"] }
|
||||||
smallvec = { version = "1.13.2" }
|
smallvec = { version = "1.13.1" }
|
||||||
smol_str = { version = "0.2.2" }
|
|
||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
strum = { version = "0.26.0", features = ["strum_macros"] }
|
strum = { version = "0.25.0", features = ["strum_macros"] }
|
||||||
strum_macros = { version = "0.26.0" }
|
strum_macros = { version = "0.25.3" }
|
||||||
syn = { version = "2.0.55" }
|
syn = { version = "2.0.51" }
|
||||||
tempfile = { version = "3.9.0" }
|
tempfile = { version = "3.9.0" }
|
||||||
test-case = { version = "3.3.1" }
|
test-case = { version = "3.3.1" }
|
||||||
thiserror = { version = "1.0.58" }
|
thiserror = { version = "1.0.57" }
|
||||||
tikv-jemallocator = { version = "0.5.0" }
|
tikv-jemallocator = { version = "0.5.0" }
|
||||||
toml = { version = "0.8.11" }
|
toml = { version = "0.8.9" }
|
||||||
tracing = { version = "0.1.40" }
|
tracing = { version = "0.1.40" }
|
||||||
tracing-indicatif = { version = "0.3.6" }
|
tracing-indicatif = { version = "0.3.6" }
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||||
tracing-tree = { version = "0.3.0" }
|
tracing-tree = { version = "0.2.4" }
|
||||||
typed-arena = { version = "2.0.2" }
|
typed-arena = { version = "2.0.2" }
|
||||||
unic-ucd-category = { version = "0.9" }
|
unic-ucd-category = { version = "0.9" }
|
||||||
unicode-ident = { version = "1.0.12" }
|
unicode-ident = { version = "1.0.12" }
|
||||||
unicode-width = { version = "0.1.11" }
|
unicode-width = { version = "0.1.11" }
|
||||||
unicode_names2 = { version = "1.2.2" }
|
unicode_names2 = { version = "1.2.2" }
|
||||||
unicode-normalization = { version = "0.1.23" }
|
|
||||||
ureq = { version = "2.9.6" }
|
ureq = { version = "2.9.6" }
|
||||||
url = { version = "2.5.0" }
|
url = { version = "2.5.0" }
|
||||||
uuid = { version = "1.6.1", features = [
|
uuid = { version = "1.6.1", features = ["v4", "fast-rng", "macro-diagnostics", "js"] }
|
||||||
"v4",
|
|
||||||
"fast-rng",
|
|
||||||
"macro-diagnostics",
|
|
||||||
"js",
|
|
||||||
] }
|
|
||||||
walkdir = { version = "2.3.2" }
|
walkdir = { version = "2.3.2" }
|
||||||
wasm-bindgen = { version = "0.2.92" }
|
wasm-bindgen = { version = "0.2.92" }
|
||||||
wasm-bindgen-test = { version = "0.3.42" }
|
wasm-bindgen-test = { version = "0.3.40" }
|
||||||
wild = { version = "2" }
|
wild = { version = "2" }
|
||||||
zip = { version = "0.6.6", default-features = false, features = ["zstd"] }
|
|
||||||
|
|
||||||
[workspace.lints.rust]
|
[workspace.lints.rust]
|
||||||
unsafe_code = "warn"
|
unsafe_code = "warn"
|
||||||
@@ -164,7 +127,6 @@ char_lit_as_u8 = "allow"
|
|||||||
collapsible_else_if = "allow"
|
collapsible_else_if = "allow"
|
||||||
collapsible_if = "allow"
|
collapsible_if = "allow"
|
||||||
implicit_hasher = "allow"
|
implicit_hasher = "allow"
|
||||||
map_unwrap_or = "allow"
|
|
||||||
match_same_arms = "allow"
|
match_same_arms = "allow"
|
||||||
missing_errors_doc = "allow"
|
missing_errors_doc = "allow"
|
||||||
missing_panics_doc = "allow"
|
missing_panics_doc = "allow"
|
||||||
@@ -219,60 +181,3 @@ opt-level = 1
|
|||||||
[profile.profiling]
|
[profile.profiling]
|
||||||
inherits = "release"
|
inherits = "release"
|
||||||
debug = 1
|
debug = 1
|
||||||
|
|
||||||
# The profile that 'cargo dist' will build with.
|
|
||||||
[profile.dist]
|
|
||||||
inherits = "release"
|
|
||||||
|
|
||||||
# Config for 'cargo dist'
|
|
||||||
[workspace.metadata.dist]
|
|
||||||
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
|
|
||||||
cargo-dist-version = "0.14.0"
|
|
||||||
# CI backends to support
|
|
||||||
ci = ["github"]
|
|
||||||
# The installers to generate for each app
|
|
||||||
installers = ["shell", "powershell"]
|
|
||||||
# The archive format to use for windows builds (defaults .zip)
|
|
||||||
windows-archive = ".zip"
|
|
||||||
# The archive format to use for non-windows builds (defaults .tar.xz)
|
|
||||||
unix-archive = ".tar.gz"
|
|
||||||
# Target platforms to build apps for (Rust target-triple syntax)
|
|
||||||
targets = [
|
|
||||||
"aarch64-apple-darwin",
|
|
||||||
"aarch64-pc-windows-msvc",
|
|
||||||
"aarch64-unknown-linux-gnu",
|
|
||||||
"aarch64-unknown-linux-musl",
|
|
||||||
"arm-unknown-linux-musleabihf",
|
|
||||||
"armv7-unknown-linux-gnueabihf",
|
|
||||||
"armv7-unknown-linux-musleabihf",
|
|
||||||
"i686-pc-windows-msvc",
|
|
||||||
"i686-unknown-linux-gnu",
|
|
||||||
"i686-unknown-linux-musl",
|
|
||||||
"powerpc64-unknown-linux-gnu",
|
|
||||||
"powerpc64le-unknown-linux-gnu",
|
|
||||||
"s390x-unknown-linux-gnu",
|
|
||||||
"x86_64-apple-darwin",
|
|
||||||
"x86_64-pc-windows-msvc",
|
|
||||||
"x86_64-unknown-linux-gnu",
|
|
||||||
"x86_64-unknown-linux-musl",
|
|
||||||
]
|
|
||||||
# Whether to auto-include files like READMEs, LICENSEs, and CHANGELOGs (default true)
|
|
||||||
auto-includes = false
|
|
||||||
# Whether cargo-dist should create a Github Release or use an existing draft
|
|
||||||
create-release = true
|
|
||||||
# Publish jobs to run in CI
|
|
||||||
pr-run-mode = "skip"
|
|
||||||
# Whether CI should trigger releases with dispatches instead of tag pushes
|
|
||||||
dispatch-releases = true
|
|
||||||
# Whether CI should include auto-generated code to build local artifacts
|
|
||||||
build-local-artifacts = false
|
|
||||||
# Local artifacts jobs to run in CI
|
|
||||||
local-artifacts-jobs = ["./build-binaries", "./build-docker"]
|
|
||||||
# Publish jobs to run in CI
|
|
||||||
publish-jobs = ["./publish-pypi"]
|
|
||||||
# Announcement jobs to run in CI
|
|
||||||
post-announce-jobs = ["./notify-dependents", "./publish-docs", "./publish-playground"]
|
|
||||||
# Skip checking whether the specified configuration files are up to date
|
|
||||||
allow-dirty = ["ci"]
|
|
||||||
# Whether to install an updater program
|
|
||||||
install-updater = false
|
|
||||||
|
|||||||
26
README.md
26
README.md
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
[](https://github.com/astral-sh/ruff)
|
[](https://github.com/astral-sh/ruff)
|
||||||
[](https://pypi.python.org/pypi/ruff)
|
[](https://pypi.python.org/pypi/ruff)
|
||||||
[](https://github.com/astral-sh/ruff/blob/main/LICENSE)
|
[](https://pypi.python.org/pypi/ruff)
|
||||||
[](https://pypi.python.org/pypi/ruff)
|
[](https://pypi.python.org/pypi/ruff)
|
||||||
[](https://github.com/astral-sh/ruff/actions)
|
[](https://github.com/astral-sh/ruff/actions)
|
||||||
[](https://discord.com/invite/astral-sh)
|
[](https://discord.com/invite/astral-sh)
|
||||||
@@ -28,11 +28,11 @@ An extremely fast Python linter and code formatter, written in Rust.
|
|||||||
- ⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black)
|
- ⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black)
|
||||||
- 🐍 Installable via `pip`
|
- 🐍 Installable via `pip`
|
||||||
- 🛠️ `pyproject.toml` support
|
- 🛠️ `pyproject.toml` support
|
||||||
- 🤝 Python 3.13 compatibility
|
- 🤝 Python 3.12 compatibility
|
||||||
- ⚖️ Drop-in parity with [Flake8](https://docs.astral.sh/ruff/faq/#how-does-ruff-compare-to-flake8), isort, and Black
|
- ⚖️ Drop-in parity with [Flake8](https://docs.astral.sh/ruff/faq/#how-does-ruff-compare-to-flake8), isort, and Black
|
||||||
- 📦 Built-in caching, to avoid re-analyzing unchanged files
|
- 📦 Built-in caching, to avoid re-analyzing unchanged files
|
||||||
- 🔧 Fix support, for automatic error correction (e.g., automatically remove unused imports)
|
- 🔧 Fix support, for automatic error correction (e.g., automatically remove unused imports)
|
||||||
- 📏 Over [800 built-in rules](https://docs.astral.sh/ruff/rules/), with native re-implementations
|
- 📏 Over [700 built-in rules](https://docs.astral.sh/ruff/rules/), with native re-implementations
|
||||||
of popular Flake8 plugins, like flake8-bugbear
|
of popular Flake8 plugins, like flake8-bugbear
|
||||||
- ⌨️ First-party [editor integrations](https://docs.astral.sh/ruff/integrations/) for
|
- ⌨️ First-party [editor integrations](https://docs.astral.sh/ruff/integrations/) for
|
||||||
[VS Code](https://github.com/astral-sh/ruff-vscode) and [more](https://github.com/astral-sh/ruff-lsp)
|
[VS Code](https://github.com/astral-sh/ruff-vscode) and [more](https://github.com/astral-sh/ruff-lsp)
|
||||||
@@ -50,7 +50,6 @@ times faster than any individual tool.
|
|||||||
Ruff is extremely actively developed and used in major open-source projects like:
|
Ruff is extremely actively developed and used in major open-source projects like:
|
||||||
|
|
||||||
- [Apache Airflow](https://github.com/apache/airflow)
|
- [Apache Airflow](https://github.com/apache/airflow)
|
||||||
- [Apache Superset](https://github.com/apache/superset)
|
|
||||||
- [FastAPI](https://github.com/tiangolo/fastapi)
|
- [FastAPI](https://github.com/tiangolo/fastapi)
|
||||||
- [Hugging Face](https://github.com/huggingface/transformers)
|
- [Hugging Face](https://github.com/huggingface/transformers)
|
||||||
- [Pandas](https://github.com/pandas-dev/pandas)
|
- [Pandas](https://github.com/pandas-dev/pandas)
|
||||||
@@ -152,7 +151,7 @@ Ruff can also be used as a [pre-commit](https://pre-commit.com/) hook via [`ruff
|
|||||||
```yaml
|
```yaml
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# Ruff version.
|
# Ruff version.
|
||||||
rev: v0.5.0
|
rev: v0.3.2
|
||||||
hooks:
|
hooks:
|
||||||
# Run the linter.
|
# Run the linter.
|
||||||
- id: ruff
|
- id: ruff
|
||||||
@@ -266,11 +265,6 @@ The remaining configuration options can be provided through a catch-all `--confi
|
|||||||
ruff check --config "lint.per-file-ignores = {'some_file.py' = ['F841']}"
|
ruff check --config "lint.per-file-ignores = {'some_file.py' = ['F841']}"
|
||||||
```
|
```
|
||||||
|
|
||||||
To opt in to the latest lint rules, formatter style changes, interface updates, and more, enable
|
|
||||||
[preview mode](https://docs.astral.sh/ruff/rules/) by setting `preview = true` in your configuration
|
|
||||||
file or passing `--preview` on the command line. Preview mode enables a collection of unstable
|
|
||||||
features that may change prior to stabilization.
|
|
||||||
|
|
||||||
See `ruff help` for more on Ruff's top-level commands, or `ruff help check` and `ruff help format`
|
See `ruff help` for more on Ruff's top-level commands, or `ruff help check` and `ruff help format`
|
||||||
for more on the linting and formatting commands, respectively.
|
for more on the linting and formatting commands, respectively.
|
||||||
|
|
||||||
@@ -278,7 +272,7 @@ for more on the linting and formatting commands, respectively.
|
|||||||
|
|
||||||
<!-- Begin section: Rules -->
|
<!-- Begin section: Rules -->
|
||||||
|
|
||||||
**Ruff supports over 800 lint rules**, many of which are inspired by popular tools like Flake8,
|
**Ruff supports over 700 lint rules**, many of which are inspired by popular tools like Flake8,
|
||||||
isort, pyupgrade, and others. Regardless of the rule's origin, Ruff re-implements every rule in
|
isort, pyupgrade, and others. Regardless of the rule's origin, Ruff re-implements every rule in
|
||||||
Rust as a first-party feature.
|
Rust as a first-party feature.
|
||||||
|
|
||||||
@@ -334,6 +328,7 @@ quality tools, including:
|
|||||||
- [flake8-super](https://pypi.org/project/flake8-super/)
|
- [flake8-super](https://pypi.org/project/flake8-super/)
|
||||||
- [flake8-tidy-imports](https://pypi.org/project/flake8-tidy-imports/)
|
- [flake8-tidy-imports](https://pypi.org/project/flake8-tidy-imports/)
|
||||||
- [flake8-todos](https://pypi.org/project/flake8-todos/)
|
- [flake8-todos](https://pypi.org/project/flake8-todos/)
|
||||||
|
- [flake8-trio](https://pypi.org/project/flake8-trio/)
|
||||||
- [flake8-type-checking](https://pypi.org/project/flake8-type-checking/)
|
- [flake8-type-checking](https://pypi.org/project/flake8-type-checking/)
|
||||||
- [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/)
|
- [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/)
|
||||||
- [flynt](https://pypi.org/project/flynt/) ([#2102](https://github.com/astral-sh/ruff/issues/2102))
|
- [flynt](https://pypi.org/project/flynt/) ([#2102](https://github.com/astral-sh/ruff/issues/2102))
|
||||||
@@ -407,7 +402,6 @@ Ruff is used by a number of major open-source projects and companies, including:
|
|||||||
- [Dagster](https://github.com/dagster-io/dagster)
|
- [Dagster](https://github.com/dagster-io/dagster)
|
||||||
- Databricks ([MLflow](https://github.com/mlflow/mlflow))
|
- Databricks ([MLflow](https://github.com/mlflow/mlflow))
|
||||||
- [FastAPI](https://github.com/tiangolo/fastapi)
|
- [FastAPI](https://github.com/tiangolo/fastapi)
|
||||||
- [Godot](https://github.com/godotengine/godot)
|
|
||||||
- [Gradio](https://github.com/gradio-app/gradio)
|
- [Gradio](https://github.com/gradio-app/gradio)
|
||||||
- [Great Expectations](https://github.com/great-expectations/great_expectations)
|
- [Great Expectations](https://github.com/great-expectations/great_expectations)
|
||||||
- [HTTPX](https://github.com/encode/httpx)
|
- [HTTPX](https://github.com/encode/httpx)
|
||||||
@@ -430,18 +424,15 @@ Ruff is used by a number of major open-source projects and companies, including:
|
|||||||
- Microsoft ([Semantic Kernel](https://github.com/microsoft/semantic-kernel),
|
- Microsoft ([Semantic Kernel](https://github.com/microsoft/semantic-kernel),
|
||||||
[ONNX Runtime](https://github.com/microsoft/onnxruntime),
|
[ONNX Runtime](https://github.com/microsoft/onnxruntime),
|
||||||
[LightGBM](https://github.com/microsoft/LightGBM))
|
[LightGBM](https://github.com/microsoft/LightGBM))
|
||||||
- Modern Treasury ([Python SDK](https://github.com/Modern-Treasury/modern-treasury-python))
|
- Modern Treasury ([Python SDK](https://github.com/Modern-Treasury/modern-treasury-python-sdk))
|
||||||
- Mozilla ([Firefox](https://github.com/mozilla/gecko-dev))
|
- Mozilla ([Firefox](https://github.com/mozilla/gecko-dev))
|
||||||
- [Mypy](https://github.com/python/mypy)
|
- [Mypy](https://github.com/python/mypy)
|
||||||
- [Nautobot](https://github.com/nautobot/nautobot)
|
|
||||||
- Netflix ([Dispatch](https://github.com/Netflix/dispatch))
|
- Netflix ([Dispatch](https://github.com/Netflix/dispatch))
|
||||||
- [Neon](https://github.com/neondatabase/neon)
|
- [Neon](https://github.com/neondatabase/neon)
|
||||||
- [Nokia](https://nokia.com/)
|
|
||||||
- [NoneBot](https://github.com/nonebot/nonebot2)
|
- [NoneBot](https://github.com/nonebot/nonebot2)
|
||||||
- [NumPyro](https://github.com/pyro-ppl/numpyro)
|
- [NumPyro](https://github.com/pyro-ppl/numpyro)
|
||||||
- [ONNX](https://github.com/onnx/onnx)
|
- [ONNX](https://github.com/onnx/onnx)
|
||||||
- [OpenBB](https://github.com/OpenBB-finance/OpenBBTerminal)
|
- [OpenBB](https://github.com/OpenBB-finance/OpenBBTerminal)
|
||||||
- [Open Wine Components](https://github.com/Open-Wine-Components/umu-launcher)
|
|
||||||
- [PDM](https://github.com/pdm-project/pdm)
|
- [PDM](https://github.com/pdm-project/pdm)
|
||||||
- [PaddlePaddle](https://github.com/PaddlePaddle/Paddle)
|
- [PaddlePaddle](https://github.com/PaddlePaddle/Paddle)
|
||||||
- [Pandas](https://github.com/pandas-dev/pandas)
|
- [Pandas](https://github.com/pandas-dev/pandas)
|
||||||
@@ -469,7 +460,6 @@ Ruff is used by a number of major open-source projects and companies, including:
|
|||||||
- [Sphinx](https://github.com/sphinx-doc/sphinx)
|
- [Sphinx](https://github.com/sphinx-doc/sphinx)
|
||||||
- [Stable Baselines3](https://github.com/DLR-RM/stable-baselines3)
|
- [Stable Baselines3](https://github.com/DLR-RM/stable-baselines3)
|
||||||
- [Starlette](https://github.com/encode/starlette)
|
- [Starlette](https://github.com/encode/starlette)
|
||||||
- [Streamlit](https://github.com/streamlit/streamlit)
|
|
||||||
- [The Algorithms](https://github.com/TheAlgorithms/Python)
|
- [The Algorithms](https://github.com/TheAlgorithms/Python)
|
||||||
- [Vega-Altair](https://github.com/altair-viz/altair)
|
- [Vega-Altair](https://github.com/altair-viz/altair)
|
||||||
- WordPress ([Openverse](https://github.com/WordPress/openverse))
|
- WordPress ([Openverse](https://github.com/WordPress/openverse))
|
||||||
@@ -507,7 +497,7 @@ If you're using Ruff, consider adding the Ruff badge to your project's `README.m
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This repository is licensed under the [MIT License](https://github.com/astral-sh/ruff/blob/main/LICENSE)
|
MIT
|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<a target="_blank" href="https://astral.sh" style="background:none">
|
<a target="_blank" href="https://astral.sh" style="background:none">
|
||||||
|
|||||||
12
_typos.toml
12
_typos.toml
@@ -1,21 +1,11 @@
|
|||||||
[files]
|
[files]
|
||||||
# https://github.com/crate-ci/typos/issues/868
|
# https://github.com/crate-ci/typos/issues/868
|
||||||
extend-exclude = ["crates/red_knot_module_resolver/vendor/**/*", "**/resources/**/*", "**/snapshots/**/*"]
|
extend-exclude = ["**/resources/**/*", "**/snapshots/**/*"]
|
||||||
|
|
||||||
[default.extend-words]
|
[default.extend-words]
|
||||||
"arange" = "arange" # e.g. `numpy.arange`
|
|
||||||
hel = "hel"
|
hel = "hel"
|
||||||
whos = "whos"
|
whos = "whos"
|
||||||
spawnve = "spawnve"
|
spawnve = "spawnve"
|
||||||
ned = "ned"
|
ned = "ned"
|
||||||
pn = "pn" # `import panel as pd` is a thing
|
|
||||||
poit = "poit"
|
poit = "poit"
|
||||||
BA = "BA" # acronym for "Bad Allowed", used in testing.
|
BA = "BA" # acronym for "Bad Allowed", used in testing.
|
||||||
jod = "jod" # e.g., `jod-thread`
|
|
||||||
|
|
||||||
[default]
|
|
||||||
extend-ignore-re = [
|
|
||||||
# Line ignore with trailing "spellchecker:disable-line"
|
|
||||||
"(?Rm)^.*#\\s*spellchecker:disable-line$",
|
|
||||||
"LICENSEs",
|
|
||||||
]
|
|
||||||
|
|||||||
14
clippy.toml
14
clippy.toml
@@ -1,13 +1,7 @@
|
|||||||
doc-valid-idents = [
|
doc-valid-idents = [
|
||||||
"..",
|
|
||||||
"CodeQL",
|
|
||||||
"FastAPI",
|
|
||||||
"IPython",
|
|
||||||
"LangChain",
|
|
||||||
"LibCST",
|
|
||||||
"McCabe",
|
|
||||||
"NumPy",
|
|
||||||
"SCREAMING_SNAKE_CASE",
|
|
||||||
"SQLAlchemy",
|
|
||||||
"StackOverflow",
|
"StackOverflow",
|
||||||
|
"CodeQL",
|
||||||
|
"IPython",
|
||||||
|
"NumPy",
|
||||||
|
"..",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "red_knot"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition.workspace = true
|
|
||||||
rust-version.workspace = true
|
|
||||||
homepage.workspace = true
|
|
||||||
documentation.workspace = true
|
|
||||||
repository.workspace = true
|
|
||||||
authors.workspace = true
|
|
||||||
license.workspace = true
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
red_knot_module_resolver = { workspace = true }
|
|
||||||
|
|
||||||
ruff_python_parser = { workspace = true }
|
|
||||||
ruff_python_ast = { workspace = true }
|
|
||||||
ruff_text_size = { workspace = true }
|
|
||||||
ruff_index = { workspace = true }
|
|
||||||
ruff_notebook = { workspace = true }
|
|
||||||
|
|
||||||
anyhow = { workspace = true }
|
|
||||||
bitflags = { workspace = true }
|
|
||||||
crossbeam = { workspace = true }
|
|
||||||
ctrlc = { version = "3.4.4" }
|
|
||||||
dashmap = { workspace = true }
|
|
||||||
hashbrown = { workspace = true }
|
|
||||||
indexmap = { workspace = true }
|
|
||||||
is-macro = { workspace = true }
|
|
||||||
notify = { workspace = true }
|
|
||||||
parking_lot = { workspace = true }
|
|
||||||
rayon = { workspace = true }
|
|
||||||
rustc-hash = { workspace = true }
|
|
||||||
smol_str = { version = "0.2.1" }
|
|
||||||
tracing = { workspace = true }
|
|
||||||
tracing-subscriber = { workspace = true }
|
|
||||||
tracing-tree = { workspace = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
tempfile = { workspace = true }
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
@@ -1,418 +0,0 @@
|
|||||||
use std::any::type_name;
|
|
||||||
use std::fmt::{Debug, Formatter};
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
|
|
||||||
use ruff_index::{Idx, IndexVec};
|
|
||||||
use ruff_python_ast::visitor::source_order;
|
|
||||||
use ruff_python_ast::visitor::source_order::{SourceOrderVisitor, TraversalSignal};
|
|
||||||
use ruff_python_ast::{
|
|
||||||
AnyNodeRef, AstNode, ExceptHandler, ExceptHandlerExceptHandler, Expr, MatchCase, ModModule,
|
|
||||||
NodeKind, Parameter, Stmt, StmtAnnAssign, StmtAssign, StmtAugAssign, StmtClassDef,
|
|
||||||
StmtFunctionDef, StmtGlobal, StmtImport, StmtImportFrom, StmtNonlocal, StmtTypeAlias,
|
|
||||||
TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, WithItem,
|
|
||||||
};
|
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
|
||||||
|
|
||||||
/// A type agnostic ID that uniquely identifies an AST node in a file.
|
|
||||||
#[ruff_index::newtype_index]
|
|
||||||
pub struct AstId;
|
|
||||||
|
|
||||||
/// A typed ID that uniquely identifies an AST node in a file.
|
|
||||||
///
|
|
||||||
/// This is different from [`AstId`] in that it is a combination of ID and the type of the node the ID identifies.
|
|
||||||
/// Typing the ID prevents mixing IDs of different node types and allows to restrict the API to only accept
|
|
||||||
/// nodes for which an ID has been created (not all AST nodes get an ID).
|
|
||||||
pub struct TypedAstId<N: HasAstId> {
|
|
||||||
erased: AstId,
|
|
||||||
_marker: PhantomData<fn() -> N>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> TypedAstId<N> {
|
|
||||||
/// Upcasts this ID from a more specific node type to a more general node type.
|
|
||||||
pub fn upcast<M: HasAstId>(self) -> TypedAstId<M>
|
|
||||||
where
|
|
||||||
N: Into<M>,
|
|
||||||
{
|
|
||||||
TypedAstId {
|
|
||||||
erased: self.erased,
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Copy for TypedAstId<N> {}
|
|
||||||
impl<N: HasAstId> Clone for TypedAstId<N> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
*self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> PartialEq for TypedAstId<N> {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.erased == other.erased
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Eq for TypedAstId<N> {}
|
|
||||||
impl<N: HasAstId> Hash for TypedAstId<N> {
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
self.erased.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Debug for TypedAstId<N> {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple("TypedAstId")
|
|
||||||
.field(&self.erased)
|
|
||||||
.field(&type_name::<N>())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AstIds {
|
|
||||||
ids: IndexVec<AstId, NodeKey>,
|
|
||||||
reverse: FxHashMap<NodeKey, AstId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AstIds {
|
|
||||||
// TODO rust analyzer doesn't allocate an ID for every node. It only allocates ids for
|
|
||||||
// nodes with a corresponding HIR element, that is nodes that are definitions.
|
|
||||||
pub fn from_module(module: &ModModule) -> Self {
|
|
||||||
let mut visitor = AstIdsVisitor::default();
|
|
||||||
|
|
||||||
// TODO: visit_module?
|
|
||||||
// Make sure we visit the root
|
|
||||||
visitor.create_id(module);
|
|
||||||
visitor.visit_body(&module.body);
|
|
||||||
|
|
||||||
while let Some(deferred) = visitor.deferred.pop() {
|
|
||||||
match deferred {
|
|
||||||
DeferredNode::FunctionDefinition(def) => {
|
|
||||||
def.visit_source_order(&mut visitor);
|
|
||||||
}
|
|
||||||
DeferredNode::ClassDefinition(def) => def.visit_source_order(&mut visitor),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AstIds {
|
|
||||||
ids: visitor.ids,
|
|
||||||
reverse: visitor.reverse,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the ID to the root node.
|
|
||||||
pub fn root(&self) -> NodeKey {
|
|
||||||
self.ids[AstId::new(0)]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`TypedAstId`] for a node.
|
|
||||||
pub fn ast_id<N: HasAstId>(&self, node: &N) -> TypedAstId<N> {
|
|
||||||
let key = node.syntax_node_key();
|
|
||||||
TypedAstId {
|
|
||||||
erased: self.reverse.get(&key).copied().unwrap(),
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`TypedAstId`] for the node identified with the given [`TypedNodeKey`].
|
|
||||||
pub fn ast_id_for_key<N: HasAstId>(&self, node: &TypedNodeKey<N>) -> TypedAstId<N> {
|
|
||||||
let ast_id = self.ast_id_for_node_key(node.inner);
|
|
||||||
|
|
||||||
TypedAstId {
|
|
||||||
erased: ast_id,
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the untyped [`AstId`] for the node identified by the given `node` key.
|
|
||||||
pub fn ast_id_for_node_key(&self, node: NodeKey) -> AstId {
|
|
||||||
self.reverse
|
|
||||||
.get(&node)
|
|
||||||
.copied()
|
|
||||||
.expect("Can't find node in AstIds map.")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`TypedNodeKey`] for the node identified by the given [`TypedAstId`].
|
|
||||||
pub fn key<N: HasAstId>(&self, id: TypedAstId<N>) -> TypedNodeKey<N> {
|
|
||||||
let syntax_key = self.ids[id.erased];
|
|
||||||
|
|
||||||
TypedNodeKey::new(syntax_key).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn node_key<H: HasAstId>(&self, id: TypedAstId<H>) -> NodeKey {
|
|
||||||
self.ids[id.erased]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for AstIds {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let mut map = f.debug_map();
|
|
||||||
for (key, value) in self.ids.iter_enumerated() {
|
|
||||||
map.entry(&key, &value);
|
|
||||||
}
|
|
||||||
|
|
||||||
map.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for AstIds {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.ids == other.ids
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for AstIds {}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct AstIdsVisitor<'a> {
|
|
||||||
ids: IndexVec<AstId, NodeKey>,
|
|
||||||
reverse: FxHashMap<NodeKey, AstId>,
|
|
||||||
deferred: Vec<DeferredNode<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AstIdsVisitor<'a> {
|
|
||||||
fn create_id<A: HasAstId>(&mut self, node: &A) {
|
|
||||||
let node_key = node.syntax_node_key();
|
|
||||||
|
|
||||||
let id = self.ids.push(node_key);
|
|
||||||
self.reverse.insert(node_key, id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> SourceOrderVisitor<'a> for AstIdsVisitor<'a> {
|
|
||||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
|
||||||
match stmt {
|
|
||||||
Stmt::FunctionDef(def) => {
|
|
||||||
self.create_id(def);
|
|
||||||
self.deferred.push(DeferredNode::FunctionDefinition(def));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// TODO defer visiting the assignment body, type alias parameters etc?
|
|
||||||
Stmt::ClassDef(def) => {
|
|
||||||
self.create_id(def);
|
|
||||||
self.deferred.push(DeferredNode::ClassDefinition(def));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Stmt::Expr(_) => {
|
|
||||||
// Skip
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Stmt::Return(_) => {}
|
|
||||||
Stmt::Delete(_) => {}
|
|
||||||
Stmt::Assign(assignment) => self.create_id(assignment),
|
|
||||||
Stmt::AugAssign(assignment) => {
|
|
||||||
self.create_id(assignment);
|
|
||||||
}
|
|
||||||
Stmt::AnnAssign(assignment) => self.create_id(assignment),
|
|
||||||
Stmt::TypeAlias(assignment) => self.create_id(assignment),
|
|
||||||
Stmt::For(_) => {}
|
|
||||||
Stmt::While(_) => {}
|
|
||||||
Stmt::If(_) => {}
|
|
||||||
Stmt::With(_) => {}
|
|
||||||
Stmt::Match(_) => {}
|
|
||||||
Stmt::Raise(_) => {}
|
|
||||||
Stmt::Try(_) => {}
|
|
||||||
Stmt::Assert(_) => {}
|
|
||||||
Stmt::Import(import) => self.create_id(import),
|
|
||||||
Stmt::ImportFrom(import_from) => self.create_id(import_from),
|
|
||||||
Stmt::Global(global) => self.create_id(global),
|
|
||||||
Stmt::Nonlocal(non_local) => self.create_id(non_local),
|
|
||||||
Stmt::Pass(_) => {}
|
|
||||||
Stmt::Break(_) => {}
|
|
||||||
Stmt::Continue(_) => {}
|
|
||||||
Stmt::IpyEscapeCommand(_) => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
source_order::walk_stmt(self, stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_expr(&mut self, _expr: &'a Expr) {}
|
|
||||||
|
|
||||||
fn visit_parameter(&mut self, parameter: &'a Parameter) {
|
|
||||||
self.create_id(parameter);
|
|
||||||
source_order::walk_parameter(self, parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
|
|
||||||
match except_handler {
|
|
||||||
ExceptHandler::ExceptHandler(except_handler) => {
|
|
||||||
self.create_id(except_handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
source_order::walk_except_handler(self, except_handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_with_item(&mut self, with_item: &'a WithItem) {
|
|
||||||
self.create_id(with_item);
|
|
||||||
source_order::walk_with_item(self, with_item);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_match_case(&mut self, match_case: &'a MatchCase) {
|
|
||||||
self.create_id(match_case);
|
|
||||||
source_order::walk_match_case(self, match_case);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_type_param(&mut self, type_param: &'a TypeParam) {
|
|
||||||
self.create_id(type_param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum DeferredNode<'a> {
|
|
||||||
FunctionDefinition(&'a StmtFunctionDef),
|
|
||||||
ClassDefinition(&'a StmtClassDef),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
pub struct TypedNodeKey<N: AstNode> {
|
|
||||||
/// The type erased node key.
|
|
||||||
inner: NodeKey,
|
|
||||||
_marker: PhantomData<fn() -> N>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: AstNode> TypedNodeKey<N> {
|
|
||||||
pub fn from_node(node: &N) -> Self {
|
|
||||||
let inner = NodeKey::from_node(node.as_any_node_ref());
|
|
||||||
Self {
|
|
||||||
inner,
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(node_key: NodeKey) -> Option<Self> {
|
|
||||||
N::can_cast(node_key.kind).then_some(TypedNodeKey {
|
|
||||||
inner: node_key,
|
|
||||||
_marker: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resolve<'a>(&self, root: AnyNodeRef<'a>) -> Option<N::Ref<'a>> {
|
|
||||||
let node_ref = self.inner.resolve(root)?;
|
|
||||||
|
|
||||||
Some(N::cast_ref(node_ref).unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resolve_unwrap<'a>(&self, root: AnyNodeRef<'a>) -> N::Ref<'a> {
|
|
||||||
self.resolve(root).expect("node should resolve")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn erased(&self) -> &NodeKey {
|
|
||||||
&self.inner
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct FindNodeKeyVisitor<'a> {
|
|
||||||
key: NodeKey,
|
|
||||||
result: Option<AnyNodeRef<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> SourceOrderVisitor<'a> for FindNodeKeyVisitor<'a> {
|
|
||||||
fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal {
|
|
||||||
if self.result.is_some() {
|
|
||||||
return TraversalSignal::Skip;
|
|
||||||
}
|
|
||||||
|
|
||||||
if node.range() == self.key.range && node.kind() == self.key.kind {
|
|
||||||
self.result = Some(node);
|
|
||||||
TraversalSignal::Skip
|
|
||||||
} else if node.range().contains_range(self.key.range) {
|
|
||||||
TraversalSignal::Traverse
|
|
||||||
} else {
|
|
||||||
TraversalSignal::Skip
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_body(&mut self, body: &'a [Stmt]) {
|
|
||||||
// TODO it would be more efficient to use binary search instead of linear
|
|
||||||
for stmt in body {
|
|
||||||
if stmt.range().start() > self.key.range.end() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.visit_stmt(stmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO an alternative to this is to have a `NodeId` on each node (in increasing order depending on the position).
|
|
||||||
// This would allow to reduce the size of this to a u32.
|
|
||||||
// What would be nice if we could use an `Arc::weak_ref` here but that only works if we use
|
|
||||||
// `Arc` internally
|
|
||||||
// TODO: Implement the logic to resolve a node, given a db (and the correct file).
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
pub struct NodeKey {
|
|
||||||
kind: NodeKind,
|
|
||||||
range: TextRange,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NodeKey {
|
|
||||||
pub fn from_node(node: AnyNodeRef) -> Self {
|
|
||||||
NodeKey {
|
|
||||||
kind: node.kind(),
|
|
||||||
range: node.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn resolve<'a>(&self, root: AnyNodeRef<'a>) -> Option<AnyNodeRef<'a>> {
|
|
||||||
// We need to do a binary search here. Only traverse into a node if the range is withint the node
|
|
||||||
let mut visitor = FindNodeKeyVisitor {
|
|
||||||
key: *self,
|
|
||||||
result: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if visitor.enter_node(root) == TraversalSignal::Traverse {
|
|
||||||
root.visit_preorder(&mut visitor);
|
|
||||||
}
|
|
||||||
|
|
||||||
visitor.result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Marker trait implemented by AST nodes for which we extract the `AstId`.
|
|
||||||
pub trait HasAstId: AstNode {
|
|
||||||
fn node_key(&self) -> TypedNodeKey<Self>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
TypedNodeKey {
|
|
||||||
inner: self.syntax_node_key(),
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn syntax_node_key(&self) -> NodeKey {
|
|
||||||
NodeKey {
|
|
||||||
kind: self.as_any_node_ref().kind(),
|
|
||||||
range: self.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasAstId for StmtFunctionDef {}
|
|
||||||
impl HasAstId for StmtClassDef {}
|
|
||||||
impl HasAstId for StmtAnnAssign {}
|
|
||||||
impl HasAstId for StmtAugAssign {}
|
|
||||||
impl HasAstId for StmtAssign {}
|
|
||||||
impl HasAstId for StmtTypeAlias {}
|
|
||||||
|
|
||||||
impl HasAstId for ModModule {}
|
|
||||||
|
|
||||||
impl HasAstId for StmtImport {}
|
|
||||||
|
|
||||||
impl HasAstId for StmtImportFrom {}
|
|
||||||
|
|
||||||
impl HasAstId for Parameter {}
|
|
||||||
|
|
||||||
impl HasAstId for TypeParam {}
|
|
||||||
impl HasAstId for Stmt {}
|
|
||||||
impl HasAstId for TypeParamTypeVar {}
|
|
||||||
impl HasAstId for TypeParamTypeVarTuple {}
|
|
||||||
impl HasAstId for TypeParamParamSpec {}
|
|
||||||
impl HasAstId for StmtGlobal {}
|
|
||||||
impl HasAstId for StmtNonlocal {}
|
|
||||||
|
|
||||||
impl HasAstId for ExceptHandlerExceptHandler {}
|
|
||||||
impl HasAstId for WithItem {}
|
|
||||||
impl HasAstId for MatchCase {}
|
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
use std::fmt::Formatter;
|
|
||||||
use std::hash::Hash;
|
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
||||||
|
|
||||||
use crate::db::QueryResult;
|
|
||||||
use dashmap::mapref::entry::Entry;
|
|
||||||
|
|
||||||
use crate::FxDashMap;
|
|
||||||
|
|
||||||
/// Simple key value cache that locks on a per-key level.
|
|
||||||
pub struct KeyValueCache<K, V> {
|
|
||||||
map: FxDashMap<K, V>,
|
|
||||||
statistics: CacheStatistics,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V> KeyValueCache<K, V>
|
|
||||||
where
|
|
||||||
K: Eq + Hash + Clone,
|
|
||||||
V: Clone,
|
|
||||||
{
|
|
||||||
pub fn try_get(&self, key: &K) -> Option<V> {
|
|
||||||
if let Some(existing) = self.map.get(key) {
|
|
||||||
self.statistics.hit();
|
|
||||||
Some(existing.clone())
|
|
||||||
} else {
|
|
||||||
self.statistics.miss();
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<F>(&self, key: &K, compute: F) -> QueryResult<V>
|
|
||||||
where
|
|
||||||
F: FnOnce(&K) -> QueryResult<V>,
|
|
||||||
{
|
|
||||||
Ok(match self.map.entry(key.clone()) {
|
|
||||||
Entry::Occupied(cached) => {
|
|
||||||
self.statistics.hit();
|
|
||||||
|
|
||||||
cached.get().clone()
|
|
||||||
}
|
|
||||||
Entry::Vacant(vacant) => {
|
|
||||||
self.statistics.miss();
|
|
||||||
|
|
||||||
let value = compute(key)?;
|
|
||||||
vacant.insert(value.clone());
|
|
||||||
value
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set(&mut self, key: K, value: V) {
|
|
||||||
self.map.insert(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(&mut self, key: &K) -> Option<V> {
|
|
||||||
self.map.remove(key).map(|(_, value)| value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
self.map.clear();
|
|
||||||
self.map.shrink_to_fit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn statistics(&self) -> Option<Statistics> {
|
|
||||||
self.statistics.to_statistics()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V> Default for KeyValueCache<K, V>
|
|
||||||
where
|
|
||||||
K: Eq + Hash,
|
|
||||||
V: Clone,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
map: FxDashMap::default(),
|
|
||||||
statistics: CacheStatistics::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V> std::fmt::Debug for KeyValueCache<K, V>
|
|
||||||
where
|
|
||||||
K: std::fmt::Debug + Eq + Hash,
|
|
||||||
V: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let mut debug = f.debug_map();
|
|
||||||
|
|
||||||
for entry in &self.map {
|
|
||||||
debug.entry(&entry.value(), &entry.key());
|
|
||||||
}
|
|
||||||
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub struct Statistics {
|
|
||||||
pub hits: usize,
|
|
||||||
pub misses: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Statistics {
|
|
||||||
#[allow(clippy::cast_precision_loss)]
|
|
||||||
pub fn hit_rate(&self) -> Option<f64> {
|
|
||||||
if self.hits + self.misses == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((self.hits as f64) / (self.hits + self.misses) as f64)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
pub type CacheStatistics = DebugStatistics;
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
pub type CacheStatistics = ReleaseStatistics;
|
|
||||||
|
|
||||||
pub trait StatisticsRecorder {
|
|
||||||
fn hit(&self);
|
|
||||||
fn miss(&self);
|
|
||||||
fn to_statistics(&self) -> Option<Statistics>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct DebugStatistics {
|
|
||||||
hits: AtomicUsize,
|
|
||||||
misses: AtomicUsize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StatisticsRecorder for DebugStatistics {
|
|
||||||
// TODO figure out appropriate Ordering
|
|
||||||
fn hit(&self) {
|
|
||||||
self.hits.fetch_add(1, Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn miss(&self) {
|
|
||||||
self.misses.fetch_add(1, Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_statistics(&self) -> Option<Statistics> {
|
|
||||||
let hits = self.hits.load(Ordering::SeqCst);
|
|
||||||
let misses = self.misses.load(Ordering::SeqCst);
|
|
||||||
|
|
||||||
Some(Statistics { hits, misses })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct ReleaseStatistics;
|
|
||||||
|
|
||||||
impl StatisticsRecorder for ReleaseStatistics {
|
|
||||||
#[inline]
|
|
||||||
fn hit(&self) {}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn miss(&self) {}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn to_statistics(&self) -> Option<Statistics> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
use std::sync::atomic::AtomicBool;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
|
||||||
pub struct CancellationTokenSource {
|
|
||||||
signal: Arc<AtomicBool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CancellationTokenSource {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
signal: Arc::new(AtomicBool::new(false)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "trace", skip_all)]
|
|
||||||
pub fn cancel(&self) {
|
|
||||||
self.signal.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_cancelled(&self) -> bool {
|
|
||||||
self.signal.load(std::sync::atomic::Ordering::SeqCst)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn token(&self) -> CancellationToken {
|
|
||||||
CancellationToken {
|
|
||||||
signal: self.signal.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct CancellationToken {
|
|
||||||
signal: Arc<AtomicBool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CancellationToken {
|
|
||||||
/// Returns `true` if cancellation has been requested.
|
|
||||||
pub fn is_cancelled(&self) -> bool {
|
|
||||||
self.signal.load(std::sync::atomic::Ordering::SeqCst)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,248 +0,0 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
pub use jars::{HasJar, HasJars};
|
|
||||||
pub use query::{QueryError, QueryResult};
|
|
||||||
pub use runtime::DbRuntime;
|
|
||||||
pub use storage::JarsStorage;
|
|
||||||
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::lint::{LintSemanticStorage, LintSyntaxStorage};
|
|
||||||
use crate::module::ModuleResolver;
|
|
||||||
use crate::parse::ParsedStorage;
|
|
||||||
use crate::semantic::SemanticIndexStorage;
|
|
||||||
use crate::semantic::TypeStore;
|
|
||||||
use crate::source::SourceStorage;
|
|
||||||
|
|
||||||
mod jars;
|
|
||||||
mod query;
|
|
||||||
mod runtime;
|
|
||||||
mod storage;
|
|
||||||
|
|
||||||
pub trait Database {
|
|
||||||
/// Returns a reference to the runtime of the current worker.
|
|
||||||
fn runtime(&self) -> &DbRuntime;
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the runtime. Only one worker can hold a mutable reference to the runtime.
|
|
||||||
fn runtime_mut(&mut self) -> &mut DbRuntime;
|
|
||||||
|
|
||||||
/// Returns `Ok` if the queries have not been cancelled and `Err(QueryError::Cancelled)` otherwise.
|
|
||||||
fn cancelled(&self) -> QueryResult<()> {
|
|
||||||
self.runtime().cancelled()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if the queries have been cancelled.
|
|
||||||
fn is_cancelled(&self) -> bool {
|
|
||||||
self.runtime().is_cancelled()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Database that supports running queries from multiple threads.
|
|
||||||
pub trait ParallelDatabase: Database + Send {
|
|
||||||
/// Creates a snapshot of the database state that can be used to query the database in another thread.
|
|
||||||
///
|
|
||||||
/// The snapshot is a read-only view of the database but query results are shared between threads.
|
|
||||||
/// All queries will be automatically cancelled when applying any mutations (calling [`HasJars::jars_mut`])
|
|
||||||
/// to the database (not the snapshot, because they're readonly).
|
|
||||||
///
|
|
||||||
/// ## Creating a snapshot
|
|
||||||
///
|
|
||||||
/// Creating a snapshot of the database's jars is cheap but creating a snapshot of
|
|
||||||
/// other state stored on the database might require deep-cloning data. That's why you should
|
|
||||||
/// avoid creating snapshots in a hot function (e.g. don't create a snapshot for each file, instead
|
|
||||||
/// create a snapshot when scheduling the check of an entire program).
|
|
||||||
///
|
|
||||||
/// ## Salsa compatibility
|
|
||||||
/// Salsa prohibits creating a snapshot while running a local query (it's fine if other workers run a query) [[source](https://github.com/salsa-rs/salsa/issues/80)].
|
|
||||||
/// We should avoid creating snapshots while running a query because we might want to adopt Salsa in the future (if we can figure out persistent caching).
|
|
||||||
/// Unfortunately, the infrastructure doesn't provide an automated way of knowing when a query is run, that's
|
|
||||||
/// why we have to "enforce" this constraint manually.
|
|
||||||
#[must_use]
|
|
||||||
fn snapshot(&self) -> Snapshot<Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait DbWithJar<Jar>: Database + HasJar<Jar> {}
|
|
||||||
|
|
||||||
/// Readonly snapshot of a database.
|
|
||||||
///
|
|
||||||
/// ## Dead locks
|
|
||||||
/// A snapshot should always be dropped as soon as it is no longer necessary to run queries.
|
|
||||||
/// Storing the snapshot without running a query or periodically checking if cancellation was requested
|
|
||||||
/// can lead to deadlocks because mutating the [`Database`] requires cancels all pending queries
|
|
||||||
/// and waiting for all [`Snapshot`]s to be dropped.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Snapshot<DB: ?Sized>
|
|
||||||
where
|
|
||||||
DB: ParallelDatabase,
|
|
||||||
{
|
|
||||||
db: DB,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<DB> Snapshot<DB>
|
|
||||||
where
|
|
||||||
DB: ParallelDatabase,
|
|
||||||
{
|
|
||||||
pub fn new(db: DB) -> Self {
|
|
||||||
Snapshot { db }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<DB> std::ops::Deref for Snapshot<DB>
|
|
||||||
where
|
|
||||||
DB: ParallelDatabase,
|
|
||||||
{
|
|
||||||
type Target = DB;
|
|
||||||
|
|
||||||
fn deref(&self) -> &DB {
|
|
||||||
&self.db
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Upcast<T: ?Sized> {
|
|
||||||
fn upcast(&self) -> &T;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Red knot specific databases code.
|
|
||||||
|
|
||||||
pub trait SourceDb: DbWithJar<SourceJar> {
|
|
||||||
// queries
|
|
||||||
fn file_id(&self, path: &std::path::Path) -> FileId;
|
|
||||||
|
|
||||||
fn file_path(&self, file_id: FileId) -> Arc<std::path::Path>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait SemanticDb: SourceDb + DbWithJar<SemanticJar> + Upcast<dyn SourceDb> {}
|
|
||||||
|
|
||||||
pub trait LintDb: SemanticDb + DbWithJar<LintJar> + Upcast<dyn SemanticDb> {}
|
|
||||||
|
|
||||||
pub trait Db: LintDb + Upcast<dyn LintDb> {}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct SourceJar {
|
|
||||||
pub sources: SourceStorage,
|
|
||||||
pub parsed: ParsedStorage,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct SemanticJar {
|
|
||||||
pub module_resolver: ModuleResolver,
|
|
||||||
pub semantic_indices: SemanticIndexStorage,
|
|
||||||
pub type_store: TypeStore,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct LintJar {
|
|
||||||
pub lint_syntax: LintSyntaxStorage,
|
|
||||||
pub lint_semantic: LintSemanticStorage,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) mod tests {
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::db::{
|
|
||||||
Database, DbRuntime, DbWithJar, HasJar, HasJars, JarsStorage, LintDb, LintJar, QueryResult,
|
|
||||||
SourceDb, SourceJar, Upcast,
|
|
||||||
};
|
|
||||||
use crate::files::{FileId, Files};
|
|
||||||
|
|
||||||
use super::{SemanticDb, SemanticJar};
|
|
||||||
|
|
||||||
// This can be a partial database used in a single crate for testing.
|
|
||||||
// It would hold fewer data than the full database.
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub(crate) struct TestDb {
|
|
||||||
files: Files,
|
|
||||||
jars: JarsStorage<Self>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<SourceJar> for TestDb {
|
|
||||||
fn jar(&self) -> QueryResult<&SourceJar> {
|
|
||||||
Ok(&self.jars()?.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut SourceJar {
|
|
||||||
&mut self.jars_mut().0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<SemanticJar> for TestDb {
|
|
||||||
fn jar(&self) -> QueryResult<&SemanticJar> {
|
|
||||||
Ok(&self.jars()?.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut SemanticJar {
|
|
||||||
&mut self.jars_mut().1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<LintJar> for TestDb {
|
|
||||||
fn jar(&self) -> QueryResult<&LintJar> {
|
|
||||||
Ok(&self.jars()?.2)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut LintJar {
|
|
||||||
&mut self.jars_mut().2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceDb for TestDb {
|
|
||||||
fn file_id(&self, path: &Path) -> FileId {
|
|
||||||
self.files.intern(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_path(&self, file_id: FileId) -> Arc<Path> {
|
|
||||||
self.files.path(file_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DbWithJar<SourceJar> for TestDb {}
|
|
||||||
|
|
||||||
impl Upcast<dyn SourceDb> for TestDb {
|
|
||||||
fn upcast(&self) -> &(dyn SourceDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SemanticDb for TestDb {}
|
|
||||||
|
|
||||||
impl DbWithJar<SemanticJar> for TestDb {}
|
|
||||||
|
|
||||||
impl Upcast<dyn SemanticDb> for TestDb {
|
|
||||||
fn upcast(&self) -> &(dyn SemanticDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LintDb for TestDb {}
|
|
||||||
|
|
||||||
impl Upcast<dyn LintDb> for TestDb {
|
|
||||||
fn upcast(&self) -> &(dyn LintDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DbWithJar<LintJar> for TestDb {}
|
|
||||||
|
|
||||||
impl HasJars for TestDb {
|
|
||||||
type Jars = (SourceJar, SemanticJar, LintJar);
|
|
||||||
|
|
||||||
fn jars(&self) -> QueryResult<&Self::Jars> {
|
|
||||||
self.jars.jars()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jars_mut(&mut self) -> &mut Self::Jars {
|
|
||||||
self.jars.jars_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Database for TestDb {
|
|
||||||
fn runtime(&self) -> &DbRuntime {
|
|
||||||
self.jars.runtime()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn runtime_mut(&mut self) -> &mut DbRuntime {
|
|
||||||
self.jars.runtime_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
use crate::db::query::QueryResult;
|
|
||||||
|
|
||||||
/// Gives access to a specific jar in the database.
|
|
||||||
///
|
|
||||||
/// Nope, the terminology isn't borrowed from Java but from Salsa <https://salsa-rs.github.io/salsa/>,
|
|
||||||
/// which is an analogy to storing the salsa in different jars.
|
|
||||||
///
|
|
||||||
/// The basic idea is that each crate can define its own jar and the jars can be combined to a single
|
|
||||||
/// database in the top level crate. Each crate also defines its own `Database` trait. The combination of
|
|
||||||
/// `Database` trait and the jar allows to write queries in isolation without having to know how they get composed at the upper levels.
|
|
||||||
///
|
|
||||||
/// Salsa further defines a `HasIngredient` trait which slices the jar to a specific storage (e.g. a specific cache).
|
|
||||||
/// We don't need this just yet because we write our queries by hand. We may want a similar trait if we decide
|
|
||||||
/// to use a macro to generate the queries.
|
|
||||||
pub trait HasJar<T> {
|
|
||||||
/// Gives a read-only reference to the jar.
|
|
||||||
fn jar(&self) -> QueryResult<&T>;
|
|
||||||
|
|
||||||
/// Gives a mutable reference to the jar.
|
|
||||||
fn jar_mut(&mut self) -> &mut T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gives access to the jars in a database.
|
|
||||||
pub trait HasJars {
|
|
||||||
/// A type storing the jars.
|
|
||||||
///
|
|
||||||
/// Most commonly, this is a tuple where each jar is a tuple element.
|
|
||||||
type Jars: Default;
|
|
||||||
|
|
||||||
/// Gives access to the underlying jars but tests if the queries have been cancelled.
|
|
||||||
///
|
|
||||||
/// Returns `Err(QueryError::Cancelled)` if the queries have been cancelled.
|
|
||||||
fn jars(&self) -> QueryResult<&Self::Jars>;
|
|
||||||
|
|
||||||
/// Gives mutable access to the underlying jars.
|
|
||||||
fn jars_mut(&mut self) -> &mut Self::Jars;
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
use std::fmt::{Display, Formatter};
|
|
||||||
|
|
||||||
/// Reason why a db query operation failed.
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum QueryError {
|
|
||||||
/// The query was cancelled because the DB was mutated or the query was cancelled by the host (e.g. on a file change or when pressing CTRL+C).
|
|
||||||
Cancelled,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for QueryError {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
QueryError::Cancelled => f.write_str("query was cancelled"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for QueryError {}
|
|
||||||
|
|
||||||
pub type QueryResult<T> = Result<T, QueryError>;
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
use crate::cancellation::CancellationTokenSource;
|
|
||||||
use crate::db::{QueryError, QueryResult};
|
|
||||||
|
|
||||||
/// Holds the jar agnostic state of the database.
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct DbRuntime {
|
|
||||||
/// The cancellation token source used to signal other works that the queries should be aborted and
|
|
||||||
/// exit at the next possible point.
|
|
||||||
cancellation_token: CancellationTokenSource,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DbRuntime {
|
|
||||||
pub(super) fn snapshot(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
cancellation_token: self.cancellation_token.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Cancels the pending queries of other workers. The current worker cannot have any pending
|
|
||||||
/// queries because we're holding a mutable reference to the runtime.
|
|
||||||
pub(super) fn cancel_other_workers(&mut self) {
|
|
||||||
self.cancellation_token.cancel();
|
|
||||||
// Set a new cancellation token so that we're in a non-cancelled state again when running the next
|
|
||||||
// query.
|
|
||||||
self.cancellation_token = CancellationTokenSource::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `Ok` if the queries have not been cancelled and `Err(QueryError::Cancelled)` otherwise.
|
|
||||||
pub(super) fn cancelled(&self) -> QueryResult<()> {
|
|
||||||
if self.cancellation_token.is_cancelled() {
|
|
||||||
Err(QueryError::Cancelled)
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if the queries have been cancelled.
|
|
||||||
pub(super) fn is_cancelled(&self) -> bool {
|
|
||||||
self.cancellation_token.is_cancelled()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
use std::fmt::Formatter;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crossbeam::sync::WaitGroup;
|
|
||||||
|
|
||||||
use crate::db::query::QueryResult;
|
|
||||||
use crate::db::runtime::DbRuntime;
|
|
||||||
use crate::db::{HasJars, ParallelDatabase};
|
|
||||||
|
|
||||||
/// Stores the jars of a database and the state for each worker.
|
|
||||||
///
|
|
||||||
/// Today, all state is shared across all workers, but it may be desired to store data per worker in the future.
|
|
||||||
pub struct JarsStorage<T>
|
|
||||||
where
|
|
||||||
T: HasJars + Sized,
|
|
||||||
{
|
|
||||||
// It's important that `jars_wait_group` is declared after `jars` to ensure that `jars` is dropped first.
|
|
||||||
// See https://doc.rust-lang.org/reference/destructors.html
|
|
||||||
/// Stores the jars of the database.
|
|
||||||
jars: Arc<T::Jars>,
|
|
||||||
|
|
||||||
/// Used to count the references to `jars`. Allows implementing `jars_mut` without requiring to clone `jars`.
|
|
||||||
jars_wait_group: WaitGroup,
|
|
||||||
|
|
||||||
/// The data agnostic state.
|
|
||||||
runtime: DbRuntime,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Db> JarsStorage<Db>
|
|
||||||
where
|
|
||||||
Db: HasJars,
|
|
||||||
{
|
|
||||||
pub(super) fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
jars: Arc::new(Db::Jars::default()),
|
|
||||||
jars_wait_group: WaitGroup::default(),
|
|
||||||
runtime: DbRuntime::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a snapshot of the jars.
|
|
||||||
///
|
|
||||||
/// Creating the snapshot is cheap because it doesn't clone the jars, it only increments a ref counter.
|
|
||||||
#[must_use]
|
|
||||||
pub fn snapshot(&self) -> JarsStorage<Db>
|
|
||||||
where
|
|
||||||
Db: ParallelDatabase,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
jars: self.jars.clone(),
|
|
||||||
jars_wait_group: self.jars_wait_group.clone(),
|
|
||||||
runtime: self.runtime.snapshot(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn jars(&self) -> QueryResult<&Db::Jars> {
|
|
||||||
self.runtime.cancelled()?;
|
|
||||||
Ok(&self.jars)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the jars without cloning their content.
|
|
||||||
///
|
|
||||||
/// The method cancels any pending queries of other works and waits for them to complete so that
|
|
||||||
/// this instance is the only instance holding a reference to the jars.
|
|
||||||
pub(crate) fn jars_mut(&mut self) -> &mut Db::Jars {
|
|
||||||
// We have a mutable ref here, so no more workers can be spawned between calling this function and taking the mut ref below.
|
|
||||||
self.cancel_other_workers();
|
|
||||||
|
|
||||||
// Now all other references to `self.jars` should have been released. We can now safely return a mutable reference
|
|
||||||
// to the Arc's content.
|
|
||||||
let jars =
|
|
||||||
Arc::get_mut(&mut self.jars).expect("All references to jars should have been released");
|
|
||||||
|
|
||||||
jars
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn runtime(&self) -> &DbRuntime {
|
|
||||||
&self.runtime
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn runtime_mut(&mut self) -> &mut DbRuntime {
|
|
||||||
// Note: This method may need to use a similar trick to `jars_mut` if `DbRuntime` is ever to store data that is shared between workers.
|
|
||||||
&mut self.runtime
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "trace", skip(self))]
|
|
||||||
fn cancel_other_workers(&mut self) {
|
|
||||||
self.runtime.cancel_other_workers();
|
|
||||||
|
|
||||||
// Wait for all other works to complete.
|
|
||||||
let existing_wait = std::mem::take(&mut self.jars_wait_group);
|
|
||||||
existing_wait.wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Db> Default for JarsStorage<Db>
|
|
||||||
where
|
|
||||||
Db: HasJars,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> std::fmt::Debug for JarsStorage<T>
|
|
||||||
where
|
|
||||||
T: HasJars,
|
|
||||||
<T as HasJars>::Jars: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("SharedStorage")
|
|
||||||
.field("jars", &self.jars)
|
|
||||||
.field("jars_wait_group", &self.jars_wait_group)
|
|
||||||
.field("runtime", &self.runtime)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,180 +0,0 @@
|
|||||||
use std::fmt::{Debug, Formatter};
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use hashbrown::hash_map::RawEntryMut;
|
|
||||||
use parking_lot::RwLock;
|
|
||||||
use rustc_hash::FxHasher;
|
|
||||||
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
|
||||||
|
|
||||||
type Map<K, V> = hashbrown::HashMap<K, V, ()>;
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct FileId;
|
|
||||||
|
|
||||||
// TODO we'll need a higher level virtual file system abstraction that allows testing if a file exists
|
|
||||||
// or retrieving its content (ideally lazily and in a way that the memory can be retained later)
|
|
||||||
// I suspect that we'll end up with a FileSystem trait and our own Path abstraction.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Files {
|
|
||||||
inner: Arc<RwLock<FilesInner>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Files {
|
|
||||||
#[tracing::instrument(level = "debug", skip(self))]
|
|
||||||
pub fn intern(&self, path: &Path) -> FileId {
|
|
||||||
self.inner.write().intern(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn try_get(&self, path: &Path) -> Option<FileId> {
|
|
||||||
self.inner.read().try_get(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(self))]
|
|
||||||
pub fn path(&self, id: FileId) -> Arc<Path> {
|
|
||||||
self.inner.read().path(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Snapshots files for a new database snapshot.
|
|
||||||
///
|
|
||||||
/// This method should not be used outside a database snapshot.
|
|
||||||
#[must_use]
|
|
||||||
pub fn snapshot(&self) -> Files {
|
|
||||||
Files {
|
|
||||||
inner: self.inner.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Files {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let files = self.inner.read();
|
|
||||||
let mut debug = f.debug_map();
|
|
||||||
for item in files.iter() {
|
|
||||||
debug.entry(&item.0, &item.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Files {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.inner.read().eq(&other.inner.read())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Files {}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct FilesInner {
|
|
||||||
by_path: Map<FileId, ()>,
|
|
||||||
// TODO should we use a map here to reclaim the space for removed files?
|
|
||||||
// TODO I think we should use our own path abstraction here to avoid having to normalize paths
|
|
||||||
// and dealing with non-utf paths everywhere.
|
|
||||||
by_id: IndexVec<FileId, Arc<Path>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FilesInner {
|
|
||||||
/// Inserts the path and returns a new id for it or returns the id if it is an existing path.
|
|
||||||
// TODO should this accept Path or PathBuf?
|
|
||||||
pub(crate) fn intern(&mut self, path: &Path) -> FileId {
|
|
||||||
let hash = FilesInner::hash_path(path);
|
|
||||||
|
|
||||||
let entry = self
|
|
||||||
.by_path
|
|
||||||
.raw_entry_mut()
|
|
||||||
.from_hash(hash, |existing_file| &*self.by_id[*existing_file] == path);
|
|
||||||
|
|
||||||
match entry {
|
|
||||||
RawEntryMut::Occupied(entry) => *entry.key(),
|
|
||||||
RawEntryMut::Vacant(entry) => {
|
|
||||||
let id = self.by_id.push(Arc::from(path));
|
|
||||||
entry.insert_with_hasher(hash, id, (), |file| {
|
|
||||||
FilesInner::hash_path(&self.by_id[*file])
|
|
||||||
});
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash_path(path: &Path) -> u64 {
|
|
||||||
let mut hasher = FxHasher::default();
|
|
||||||
path.hash(&mut hasher);
|
|
||||||
hasher.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn try_get(&self, path: &Path) -> Option<FileId> {
|
|
||||||
let mut hasher = FxHasher::default();
|
|
||||||
path.hash(&mut hasher);
|
|
||||||
let hash = hasher.finish();
|
|
||||||
|
|
||||||
Some(
|
|
||||||
*self
|
|
||||||
.by_path
|
|
||||||
.raw_entry()
|
|
||||||
.from_hash(hash, |existing_file| &*self.by_id[*existing_file] == path)?
|
|
||||||
.0,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the path for the file with the given id.
|
|
||||||
pub(crate) fn path(&self, id: FileId) -> Arc<Path> {
|
|
||||||
self.by_id[id].clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn iter(&self) -> impl Iterator<Item = (FileId, Arc<Path>)> + '_ {
|
|
||||||
self.by_path.keys().map(|id| (*id, self.by_id[*id].clone()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for FilesInner {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.by_id == other.by_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for FilesInner {}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn insert_path_twice_same_id() {
|
|
||||||
let files = Files::default();
|
|
||||||
let path = PathBuf::from("foo/bar");
|
|
||||||
let id1 = files.intern(&path);
|
|
||||||
let id2 = files.intern(&path);
|
|
||||||
assert_eq!(id1, id2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn insert_different_paths_different_ids() {
|
|
||||||
let files = Files::default();
|
|
||||||
let path1 = PathBuf::from("foo/bar");
|
|
||||||
let path2 = PathBuf::from("foo/bar/baz");
|
|
||||||
let id1 = files.intern(&path1);
|
|
||||||
let id2 = files.intern(&path2);
|
|
||||||
assert_ne!(id1, id2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn four_files() {
|
|
||||||
let files = Files::default();
|
|
||||||
let foo_path = PathBuf::from("foo");
|
|
||||||
let foo_id = files.intern(&foo_path);
|
|
||||||
let bar_path = PathBuf::from("bar");
|
|
||||||
files.intern(&bar_path);
|
|
||||||
let baz_path = PathBuf::from("baz");
|
|
||||||
files.intern(&baz_path);
|
|
||||||
let qux_path = PathBuf::from("qux");
|
|
||||||
files.intern(&qux_path);
|
|
||||||
|
|
||||||
let foo_id_2 = files.try_get(&foo_path).expect("foo_path to be found");
|
|
||||||
assert_eq!(foo_id_2, foo_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
//! Key observations
|
|
||||||
//!
|
|
||||||
//! The HIR (High-Level Intermediate Representation) avoids allocations to large extends by:
|
|
||||||
//! * Using an arena per node type
|
|
||||||
//! * using ids and id ranges to reference items.
|
|
||||||
//!
|
|
||||||
//! Using separate arena per node type has the advantage that the IDs are relatively stable, because
|
|
||||||
//! they only change when a node of the same kind has been added or removed. (What's unclear is if that matters or if
|
|
||||||
//! it still triggers a re-compute because the AST-id in the node has changed).
|
|
||||||
//!
|
|
||||||
//! The HIR does not store all details. It mainly stores the *public* interface. There's a reference
|
|
||||||
//! back to the AST node to get more details.
|
|
||||||
//!
|
|
||||||
//!
|
|
||||||
|
|
||||||
use crate::ast_ids::{HasAstId, TypedAstId};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use std::fmt::Formatter;
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
|
|
||||||
pub struct HirAstId<N: HasAstId> {
|
|
||||||
file_id: FileId,
|
|
||||||
node_id: TypedAstId<N>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Copy for HirAstId<N> {}
|
|
||||||
impl<N: HasAstId> Clone for HirAstId<N> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
*self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> PartialEq for HirAstId<N> {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.file_id == other.file_id && self.node_id == other.node_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Eq for HirAstId<N> {}
|
|
||||||
|
|
||||||
impl<N: HasAstId> std::fmt::Debug for HirAstId<N> {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("HirAstId")
|
|
||||||
.field("file_id", &self.file_id)
|
|
||||||
.field("node_id", &self.node_id)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> Hash for HirAstId<N> {
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
self.file_id.hash(state);
|
|
||||||
self.node_id.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: HasAstId> HirAstId<N> {
|
|
||||||
pub fn upcast<M: HasAstId>(self) -> HirAstId<M>
|
|
||||||
where
|
|
||||||
N: Into<M>,
|
|
||||||
{
|
|
||||||
HirAstId {
|
|
||||||
file_id: self.file_id,
|
|
||||||
node_id: self.node_id.upcast(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,556 +0,0 @@
|
|||||||
use std::ops::{Index, Range};
|
|
||||||
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
|
||||||
use ruff_python_ast::visitor::preorder;
|
|
||||||
use ruff_python_ast::visitor::preorder::PreorderVisitor;
|
|
||||||
use ruff_python_ast::{
|
|
||||||
Decorator, ExceptHandler, ExceptHandlerExceptHandler, Expr, MatchCase, ModModule, Stmt,
|
|
||||||
StmtAnnAssign, StmtAssign, StmtClassDef, StmtFunctionDef, StmtGlobal, StmtImport,
|
|
||||||
StmtImportFrom, StmtNonlocal, StmtTypeAlias, TypeParam, TypeParamParamSpec, TypeParamTypeVar,
|
|
||||||
TypeParamTypeVarTuple, WithItem,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::ast_ids::{AstIds, HasAstId};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::hir::HirAstId;
|
|
||||||
use crate::Name;
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct FunctionId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Function {
|
|
||||||
ast_id: HirAstId<StmtFunctionDef>,
|
|
||||||
name: Name,
|
|
||||||
parameters: Range<ParameterId>,
|
|
||||||
type_parameters: Range<TypeParameterId>, // TODO: type_parameters, return expression, decorators
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct ParameterId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Parameter {
|
|
||||||
kind: ParameterKind,
|
|
||||||
name: Name,
|
|
||||||
default: Option<()>, // TODO use expression HIR
|
|
||||||
ast_id: HirAstId<ruff_python_ast::Parameter>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO or should `Parameter` be an enum?
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
pub enum ParameterKind {
|
|
||||||
PositionalOnly,
|
|
||||||
Arguments,
|
|
||||||
Vararg,
|
|
||||||
KeywordOnly,
|
|
||||||
Kwarg,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct ClassId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Class {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<StmtClassDef>,
|
|
||||||
// TODO type parameters, inheritance, decorators, members
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct AssignmentId;
|
|
||||||
|
|
||||||
// This can have more than one name...
|
|
||||||
// but that means we can't implement `name()` on `ModuleItem`.
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Assignment {
|
|
||||||
// TODO: Handle multiple names / targets
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<StmtAssign>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct AnnotatedAssignment {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<StmtAnnAssign>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct AnnotatedAssignmentId;
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct TypeAliasId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct TypeAlias {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<StmtTypeAlias>,
|
|
||||||
parameters: Range<TypeParameterId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct TypeParameterId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub enum TypeParameter {
|
|
||||||
TypeVar(TypeParameterTypeVar),
|
|
||||||
ParamSpec(TypeParameterParamSpec),
|
|
||||||
TypeVarTuple(TypeParameterTypeVarTuple),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TypeParameter {
|
|
||||||
pub fn ast_id(&self) -> HirAstId<TypeParam> {
|
|
||||||
match self {
|
|
||||||
TypeParameter::TypeVar(type_var) => type_var.ast_id.upcast(),
|
|
||||||
TypeParameter::ParamSpec(param_spec) => param_spec.ast_id.upcast(),
|
|
||||||
TypeParameter::TypeVarTuple(type_var_tuple) => type_var_tuple.ast_id.upcast(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct TypeParameterTypeVar {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<TypeParamTypeVar>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct TypeParameterParamSpec {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<TypeParamParamSpec>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct TypeParameterTypeVarTuple {
|
|
||||||
name: Name,
|
|
||||||
ast_id: HirAstId<TypeParamTypeVarTuple>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct GlobalId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Global {
|
|
||||||
// TODO track names
|
|
||||||
ast_id: HirAstId<StmtGlobal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct NonLocalId;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct NonLocal {
|
|
||||||
// TODO track names
|
|
||||||
ast_id: HirAstId<StmtNonlocal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum DefinitionId {
|
|
||||||
Function(FunctionId),
|
|
||||||
Parameter(ParameterId),
|
|
||||||
Class(ClassId),
|
|
||||||
Assignment(AssignmentId),
|
|
||||||
AnnotatedAssignment(AnnotatedAssignmentId),
|
|
||||||
Global(GlobalId),
|
|
||||||
NonLocal(NonLocalId),
|
|
||||||
TypeParameter(TypeParameterId),
|
|
||||||
TypeAlias(TypeAlias),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum DefinitionItem {
|
|
||||||
Function(Function),
|
|
||||||
Parameter(Parameter),
|
|
||||||
Class(Class),
|
|
||||||
Assignment(Assignment),
|
|
||||||
AnnotatedAssignment(AnnotatedAssignment),
|
|
||||||
Global(Global),
|
|
||||||
NonLocal(NonLocal),
|
|
||||||
TypeParameter(TypeParameter),
|
|
||||||
TypeAlias(TypeAlias),
|
|
||||||
}
|
|
||||||
|
|
||||||
// The closest is rust-analyzers item-tree. It only represents "Items" which make the public interface of a module
|
|
||||||
// (it excludes any other statement or expressions). rust-analyzer uses it as the main input to the name resolution
|
|
||||||
// algorithm
|
|
||||||
// > It is the input to the name resolution algorithm, as well as to the queries defined in `adt.rs`,
|
|
||||||
// > `data.rs`, and most things in `attr.rs`.
|
|
||||||
//
|
|
||||||
// > One important purpose of this layer is to provide an "invalidation barrier" for incremental
|
|
||||||
// > computations: when typing inside an item body, the `ItemTree` of the modified file is typically
|
|
||||||
// > unaffected, so we don't have to recompute name resolution results or item data (see `data.rs`).
|
|
||||||
//
|
|
||||||
// I haven't fully figured this out but I think that this composes the "public" interface of a module?
|
|
||||||
// But maybe that's too optimistic.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
|
||||||
pub struct Definitions {
|
|
||||||
functions: IndexVec<FunctionId, Function>,
|
|
||||||
parameters: IndexVec<ParameterId, Parameter>,
|
|
||||||
classes: IndexVec<ClassId, Class>,
|
|
||||||
assignments: IndexVec<AssignmentId, Assignment>,
|
|
||||||
annotated_assignments: IndexVec<AnnotatedAssignmentId, AnnotatedAssignment>,
|
|
||||||
type_aliases: IndexVec<TypeAliasId, TypeAlias>,
|
|
||||||
type_parameters: IndexVec<TypeParameterId, TypeParameter>,
|
|
||||||
globals: IndexVec<GlobalId, Global>,
|
|
||||||
non_locals: IndexVec<NonLocalId, NonLocal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Definitions {
|
|
||||||
pub fn from_module(module: &ModModule, ast_ids: &AstIds, file_id: FileId) -> Self {
|
|
||||||
let mut visitor = DefinitionsVisitor {
|
|
||||||
definitions: Definitions::default(),
|
|
||||||
ast_ids,
|
|
||||||
file_id,
|
|
||||||
};
|
|
||||||
|
|
||||||
visitor.visit_body(&module.body);
|
|
||||||
|
|
||||||
visitor.definitions
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<FunctionId> for Definitions {
|
|
||||||
type Output = Function;
|
|
||||||
|
|
||||||
fn index(&self, index: FunctionId) -> &Self::Output {
|
|
||||||
&self.functions[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<ParameterId> for Definitions {
|
|
||||||
type Output = Parameter;
|
|
||||||
|
|
||||||
fn index(&self, index: ParameterId) -> &Self::Output {
|
|
||||||
&self.parameters[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<ClassId> for Definitions {
|
|
||||||
type Output = Class;
|
|
||||||
|
|
||||||
fn index(&self, index: ClassId) -> &Self::Output {
|
|
||||||
&self.classes[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<AssignmentId> for Definitions {
|
|
||||||
type Output = Assignment;
|
|
||||||
|
|
||||||
fn index(&self, index: AssignmentId) -> &Self::Output {
|
|
||||||
&self.assignments[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<AnnotatedAssignmentId> for Definitions {
|
|
||||||
type Output = AnnotatedAssignment;
|
|
||||||
|
|
||||||
fn index(&self, index: AnnotatedAssignmentId) -> &Self::Output {
|
|
||||||
&self.annotated_assignments[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<TypeAliasId> for Definitions {
|
|
||||||
type Output = TypeAlias;
|
|
||||||
|
|
||||||
fn index(&self, index: TypeAliasId) -> &Self::Output {
|
|
||||||
&self.type_aliases[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<GlobalId> for Definitions {
|
|
||||||
type Output = Global;
|
|
||||||
|
|
||||||
fn index(&self, index: GlobalId) -> &Self::Output {
|
|
||||||
&self.globals[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<NonLocalId> for Definitions {
|
|
||||||
type Output = NonLocal;
|
|
||||||
|
|
||||||
fn index(&self, index: NonLocalId) -> &Self::Output {
|
|
||||||
&self.non_locals[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<TypeParameterId> for Definitions {
|
|
||||||
type Output = TypeParameter;
|
|
||||||
|
|
||||||
fn index(&self, index: TypeParameterId) -> &Self::Output {
|
|
||||||
&self.type_parameters[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DefinitionsVisitor<'a> {
|
|
||||||
definitions: Definitions,
|
|
||||||
ast_ids: &'a AstIds,
|
|
||||||
file_id: FileId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DefinitionsVisitor<'_> {
|
|
||||||
fn ast_id<N: HasAstId>(&self, node: &N) -> HirAstId<N> {
|
|
||||||
HirAstId {
|
|
||||||
file_id: self.file_id,
|
|
||||||
node_id: self.ast_ids.ast_id(node),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_function_def(&mut self, function: &StmtFunctionDef) -> FunctionId {
|
|
||||||
let name = Name::new(&function.name);
|
|
||||||
|
|
||||||
let first_type_parameter_id = self.definitions.type_parameters.next_index();
|
|
||||||
let mut last_type_parameter_id = first_type_parameter_id;
|
|
||||||
|
|
||||||
if let Some(type_params) = &function.type_params {
|
|
||||||
for parameter in &type_params.type_params {
|
|
||||||
let id = self.lower_type_parameter(parameter);
|
|
||||||
last_type_parameter_id = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let parameters = self.lower_parameters(&function.parameters);
|
|
||||||
|
|
||||||
self.definitions.functions.push(Function {
|
|
||||||
name,
|
|
||||||
ast_id: self.ast_id(function),
|
|
||||||
parameters,
|
|
||||||
type_parameters: first_type_parameter_id..last_type_parameter_id,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_parameters(&mut self, parameters: &ruff_python_ast::Parameters) -> Range<ParameterId> {
|
|
||||||
let first_parameter_id = self.definitions.parameters.next_index();
|
|
||||||
let mut last_parameter_id = first_parameter_id;
|
|
||||||
|
|
||||||
for parameter in ¶meters.posonlyargs {
|
|
||||||
last_parameter_id = self.definitions.parameters.push(Parameter {
|
|
||||||
kind: ParameterKind::PositionalOnly,
|
|
||||||
name: Name::new(¶meter.parameter.name),
|
|
||||||
default: None,
|
|
||||||
ast_id: self.ast_id(¶meter.parameter),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(vararg) = ¶meters.vararg {
|
|
||||||
last_parameter_id = self.definitions.parameters.push(Parameter {
|
|
||||||
kind: ParameterKind::Vararg,
|
|
||||||
name: Name::new(&vararg.name),
|
|
||||||
default: None,
|
|
||||||
ast_id: self.ast_id(vararg),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for parameter in ¶meters.kwonlyargs {
|
|
||||||
last_parameter_id = self.definitions.parameters.push(Parameter {
|
|
||||||
kind: ParameterKind::KeywordOnly,
|
|
||||||
name: Name::new(¶meter.parameter.name),
|
|
||||||
default: None,
|
|
||||||
ast_id: self.ast_id(¶meter.parameter),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(kwarg) = ¶meters.kwarg {
|
|
||||||
last_parameter_id = self.definitions.parameters.push(Parameter {
|
|
||||||
kind: ParameterKind::KeywordOnly,
|
|
||||||
name: Name::new(&kwarg.name),
|
|
||||||
default: None,
|
|
||||||
ast_id: self.ast_id(kwarg),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
first_parameter_id..last_parameter_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_class_def(&mut self, class: &StmtClassDef) -> ClassId {
|
|
||||||
let name = Name::new(&class.name);
|
|
||||||
|
|
||||||
self.definitions.classes.push(Class {
|
|
||||||
name,
|
|
||||||
ast_id: self.ast_id(class),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_assignment(&mut self, assignment: &StmtAssign) {
|
|
||||||
// FIXME handle multiple names
|
|
||||||
if let Some(Expr::Name(name)) = assignment.targets.first() {
|
|
||||||
self.definitions.assignments.push(Assignment {
|
|
||||||
name: Name::new(&name.id),
|
|
||||||
ast_id: self.ast_id(assignment),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_annotated_assignment(&mut self, annotated_assignment: &StmtAnnAssign) {
|
|
||||||
if let Expr::Name(name) = &*annotated_assignment.target {
|
|
||||||
self.definitions
|
|
||||||
.annotated_assignments
|
|
||||||
.push(AnnotatedAssignment {
|
|
||||||
name: Name::new(&name.id),
|
|
||||||
ast_id: self.ast_id(annotated_assignment),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_type_alias(&mut self, type_alias: &StmtTypeAlias) {
|
|
||||||
if let Expr::Name(name) = &*type_alias.name {
|
|
||||||
let name = Name::new(&name.id);
|
|
||||||
|
|
||||||
let lower_parameters_id = self.definitions.type_parameters.next_index();
|
|
||||||
let mut last_parameter_id = lower_parameters_id;
|
|
||||||
|
|
||||||
if let Some(type_params) = &type_alias.type_params {
|
|
||||||
for type_parameter in &type_params.type_params {
|
|
||||||
let id = self.lower_type_parameter(type_parameter);
|
|
||||||
last_parameter_id = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.definitions.type_aliases.push(TypeAlias {
|
|
||||||
name,
|
|
||||||
ast_id: self.ast_id(type_alias),
|
|
||||||
parameters: lower_parameters_id..last_parameter_id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_type_parameter(&mut self, type_parameter: &TypeParam) -> TypeParameterId {
|
|
||||||
match type_parameter {
|
|
||||||
TypeParam::TypeVar(type_var) => {
|
|
||||||
self.definitions
|
|
||||||
.type_parameters
|
|
||||||
.push(TypeParameter::TypeVar(TypeParameterTypeVar {
|
|
||||||
name: Name::new(&type_var.name),
|
|
||||||
ast_id: self.ast_id(type_var),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
TypeParam::ParamSpec(param_spec) => {
|
|
||||||
self.definitions
|
|
||||||
.type_parameters
|
|
||||||
.push(TypeParameter::ParamSpec(TypeParameterParamSpec {
|
|
||||||
name: Name::new(¶m_spec.name),
|
|
||||||
ast_id: self.ast_id(param_spec),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
TypeParam::TypeVarTuple(type_var_tuple) => {
|
|
||||||
self.definitions
|
|
||||||
.type_parameters
|
|
||||||
.push(TypeParameter::TypeVarTuple(TypeParameterTypeVarTuple {
|
|
||||||
name: Name::new(&type_var_tuple.name),
|
|
||||||
ast_id: self.ast_id(type_var_tuple),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_import(&mut self, _import: &StmtImport) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_import_from(&mut self, _import_from: &StmtImportFrom) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_global(&mut self, global: &StmtGlobal) -> GlobalId {
|
|
||||||
self.definitions.globals.push(Global {
|
|
||||||
ast_id: self.ast_id(global),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_non_local(&mut self, non_local: &StmtNonlocal) -> NonLocalId {
|
|
||||||
self.definitions.non_locals.push(NonLocal {
|
|
||||||
ast_id: self.ast_id(non_local),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_except_handler(&mut self, _except_handler: &ExceptHandlerExceptHandler) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_with_item(&mut self, _with_item: &WithItem) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lower_match_case(&mut self, _match_case: &MatchCase) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PreorderVisitor<'_> for DefinitionsVisitor<'_> {
|
|
||||||
fn visit_stmt(&mut self, stmt: &Stmt) {
|
|
||||||
match stmt {
|
|
||||||
// Definition statements
|
|
||||||
Stmt::FunctionDef(definition) => {
|
|
||||||
self.lower_function_def(definition);
|
|
||||||
self.visit_body(&definition.body);
|
|
||||||
}
|
|
||||||
Stmt::ClassDef(definition) => {
|
|
||||||
self.lower_class_def(definition);
|
|
||||||
self.visit_body(&definition.body);
|
|
||||||
}
|
|
||||||
Stmt::Assign(assignment) => {
|
|
||||||
self.lower_assignment(assignment);
|
|
||||||
}
|
|
||||||
Stmt::AnnAssign(annotated_assignment) => {
|
|
||||||
self.lower_annotated_assignment(annotated_assignment);
|
|
||||||
}
|
|
||||||
Stmt::TypeAlias(type_alias) => {
|
|
||||||
self.lower_type_alias(type_alias);
|
|
||||||
}
|
|
||||||
|
|
||||||
Stmt::Import(import) => self.lower_import(import),
|
|
||||||
Stmt::ImportFrom(import_from) => self.lower_import_from(import_from),
|
|
||||||
Stmt::Global(global) => {
|
|
||||||
self.lower_global(global);
|
|
||||||
}
|
|
||||||
Stmt::Nonlocal(non_local) => {
|
|
||||||
self.lower_non_local(non_local);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Visit the compound statement bodies because they can contain other definitions.
|
|
||||||
Stmt::For(_)
|
|
||||||
| Stmt::While(_)
|
|
||||||
| Stmt::If(_)
|
|
||||||
| Stmt::With(_)
|
|
||||||
| Stmt::Match(_)
|
|
||||||
| Stmt::Try(_) => {
|
|
||||||
preorder::walk_stmt(self, stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip over simple statements because they can't contain any other definitions.
|
|
||||||
Stmt::Return(_)
|
|
||||||
| Stmt::Delete(_)
|
|
||||||
| Stmt::AugAssign(_)
|
|
||||||
| Stmt::Raise(_)
|
|
||||||
| Stmt::Assert(_)
|
|
||||||
| Stmt::Expr(_)
|
|
||||||
| Stmt::Pass(_)
|
|
||||||
| Stmt::Break(_)
|
|
||||||
| Stmt::Continue(_)
|
|
||||||
| Stmt::IpyEscapeCommand(_) => {
|
|
||||||
// No op
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_expr(&mut self, _: &'_ Expr) {}
|
|
||||||
|
|
||||||
fn visit_decorator(&mut self, _decorator: &'_ Decorator) {}
|
|
||||||
|
|
||||||
fn visit_except_handler(&mut self, except_handler: &'_ ExceptHandler) {
|
|
||||||
match except_handler {
|
|
||||||
ExceptHandler::ExceptHandler(except_handler) => {
|
|
||||||
self.lower_except_handler(except_handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_with_item(&mut self, with_item: &'_ WithItem) {
|
|
||||||
self.lower_with_item(with_item);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_match_case(&mut self, match_case: &'_ MatchCase) {
|
|
||||||
self.lower_match_case(match_case);
|
|
||||||
self.visit_body(&match_case.body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
use std::fmt::Formatter;
|
|
||||||
use std::hash::BuildHasherDefault;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
use rustc_hash::{FxHashSet, FxHasher};
|
|
||||||
|
|
||||||
use crate::files::FileId;
|
|
||||||
|
|
||||||
pub mod ast_ids;
|
|
||||||
pub mod cache;
|
|
||||||
pub mod cancellation;
|
|
||||||
pub mod db;
|
|
||||||
pub mod files;
|
|
||||||
pub mod hir;
|
|
||||||
pub mod lint;
|
|
||||||
pub mod module;
|
|
||||||
mod parse;
|
|
||||||
pub mod program;
|
|
||||||
mod semantic;
|
|
||||||
pub mod source;
|
|
||||||
pub mod watch;
|
|
||||||
|
|
||||||
pub(crate) type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) type FxDashSet<V> = dashmap::DashSet<V, BuildHasherDefault<FxHasher>>;
|
|
||||||
pub(crate) type FxIndexSet<V> = indexmap::set::IndexSet<V, BuildHasherDefault<FxHasher>>;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Workspace {
|
|
||||||
/// TODO this should be a resolved path. We should probably use a newtype wrapper that guarantees that
|
|
||||||
/// PATH is a UTF-8 path and is normalized.
|
|
||||||
root: PathBuf,
|
|
||||||
/// The files that are open in the workspace.
|
|
||||||
///
|
|
||||||
/// * Editor: The files that are actively being edited in the editor (the user has a tab open with the file).
|
|
||||||
/// * CLI: The resolved files passed as arguments to the CLI.
|
|
||||||
open_files: FxHashSet<FileId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Workspace {
|
|
||||||
pub fn new(root: PathBuf) -> Self {
|
|
||||||
Self {
|
|
||||||
root,
|
|
||||||
open_files: FxHashSet::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root(&self) -> &Path {
|
|
||||||
self.root.as_path()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO having the content in workspace feels wrong.
|
|
||||||
pub fn open_file(&mut self, file_id: FileId) {
|
|
||||||
self.open_files.insert(file_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn close_file(&mut self, file_id: FileId) {
|
|
||||||
self.open_files.remove(&file_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO introduce an `OpenFile` type instead of using an anonymous tuple.
|
|
||||||
pub fn open_files(&self) -> impl Iterator<Item = FileId> + '_ {
|
|
||||||
self.open_files.iter().copied()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_file_open(&self, file_id: FileId) -> bool {
|
|
||||||
self.open_files.contains(&file_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
|
||||||
pub struct Name(smol_str::SmolStr);
|
|
||||||
|
|
||||||
impl Name {
|
|
||||||
#[inline]
|
|
||||||
pub fn new(name: &str) -> Self {
|
|
||||||
Self(smol_str::SmolStr::new(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
self.0.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for Name {
|
|
||||||
type Target = str;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
self.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> From<T> for Name
|
|
||||||
where
|
|
||||||
T: Into<smol_str::SmolStr>,
|
|
||||||
{
|
|
||||||
fn from(value: T) -> Self {
|
|
||||||
Self(value.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Name {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.write_str(self.as_str())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,332 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use ruff_python_ast::visitor::Visitor;
|
|
||||||
use ruff_python_ast::{ModModule, StringLiteral};
|
|
||||||
use ruff_python_parser::Parsed;
|
|
||||||
|
|
||||||
use crate::cache::KeyValueCache;
|
|
||||||
use crate::db::{LintDb, LintJar, QueryResult};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::module::{resolve_module, ModuleName};
|
|
||||||
use crate::parse::parse;
|
|
||||||
use crate::semantic::{infer_definition_type, infer_symbol_public_type, Type};
|
|
||||||
use crate::semantic::{
|
|
||||||
resolve_global_symbol, semantic_index, Definition, GlobalSymbolId, SemanticIndex, SymbolId,
|
|
||||||
};
|
|
||||||
use crate::source::{source_text, Source};
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub(crate) fn lint_syntax(db: &dyn LintDb, file_id: FileId) -> QueryResult<Diagnostics> {
|
|
||||||
let lint_jar: &LintJar = db.jar()?;
|
|
||||||
let storage = &lint_jar.lint_syntax;
|
|
||||||
|
|
||||||
#[allow(clippy::print_stdout)]
|
|
||||||
if std::env::var("RED_KNOT_SLOW_LINT").is_ok() {
|
|
||||||
for i in 0..10 {
|
|
||||||
db.cancelled()?;
|
|
||||||
println!("RED_KNOT_SLOW_LINT is set, sleeping for {i}/10 seconds");
|
|
||||||
std::thread::sleep(Duration::from_secs(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storage.get(&file_id, |file_id| {
|
|
||||||
let mut diagnostics = Vec::new();
|
|
||||||
|
|
||||||
let source = source_text(db.upcast(), *file_id)?;
|
|
||||||
lint_lines(source.text(), &mut diagnostics);
|
|
||||||
|
|
||||||
let parsed = parse(db.upcast(), *file_id)?;
|
|
||||||
|
|
||||||
if parsed.errors().is_empty() {
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
|
|
||||||
let mut visitor = SyntaxLintVisitor {
|
|
||||||
diagnostics,
|
|
||||||
source: source.text(),
|
|
||||||
};
|
|
||||||
visitor.visit_body(&ast.body);
|
|
||||||
diagnostics = visitor.diagnostics;
|
|
||||||
} else {
|
|
||||||
diagnostics.extend(parsed.errors().iter().map(std::string::ToString::to_string));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Diagnostics::from(diagnostics))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lint_lines(source: &str, diagnostics: &mut Vec<String>) {
|
|
||||||
for (line_number, line) in source.lines().enumerate() {
|
|
||||||
if line.len() < 88 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let char_count = line.chars().count();
|
|
||||||
if char_count > 88 {
|
|
||||||
diagnostics.push(format!(
|
|
||||||
"Line {} is too long ({} characters)",
|
|
||||||
line_number + 1,
|
|
||||||
char_count
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub(crate) fn lint_semantic(db: &dyn LintDb, file_id: FileId) -> QueryResult<Diagnostics> {
|
|
||||||
let lint_jar: &LintJar = db.jar()?;
|
|
||||||
let storage = &lint_jar.lint_semantic;
|
|
||||||
|
|
||||||
storage.get(&file_id, |file_id| {
|
|
||||||
let source = source_text(db.upcast(), *file_id)?;
|
|
||||||
let parsed = parse(db.upcast(), *file_id)?;
|
|
||||||
let semantic_index = semantic_index(db.upcast(), *file_id)?;
|
|
||||||
|
|
||||||
let context = SemanticLintContext {
|
|
||||||
file_id: *file_id,
|
|
||||||
source,
|
|
||||||
parsed: &parsed,
|
|
||||||
semantic_index,
|
|
||||||
db,
|
|
||||||
diagnostics: RefCell::new(Vec::new()),
|
|
||||||
};
|
|
||||||
|
|
||||||
lint_unresolved_imports(&context)?;
|
|
||||||
lint_bad_overrides(&context)?;
|
|
||||||
|
|
||||||
Ok(Diagnostics::from(context.diagnostics.take()))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lint_unresolved_imports(context: &SemanticLintContext) -> QueryResult<()> {
|
|
||||||
// TODO: Consider iterating over the dependencies (imports) only instead of all definitions.
|
|
||||||
for (symbol, definition) in context.semantic_index().symbol_table().all_definitions() {
|
|
||||||
match definition {
|
|
||||||
Definition::Import(import) => {
|
|
||||||
let ty = context.infer_symbol_public_type(symbol)?;
|
|
||||||
|
|
||||||
if ty.is_unknown() {
|
|
||||||
context.push_diagnostic(format!("Unresolved module {}", import.module));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Definition::ImportFrom(import) => {
|
|
||||||
let ty = context.infer_symbol_public_type(symbol)?;
|
|
||||||
|
|
||||||
if ty.is_unknown() {
|
|
||||||
let module_name = import.module().map(Deref::deref).unwrap_or_default();
|
|
||||||
let message = if import.level() > 0 {
|
|
||||||
format!(
|
|
||||||
"Unresolved relative import '{}' from {}{}",
|
|
||||||
import.name(),
|
|
||||||
".".repeat(import.level() as usize),
|
|
||||||
module_name
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"Unresolved import '{}' from '{}'",
|
|
||||||
import.name(),
|
|
||||||
module_name
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
context.push_diagnostic(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lint_bad_overrides(context: &SemanticLintContext) -> QueryResult<()> {
|
|
||||||
// TODO we should have a special marker on the real typing module (from typeshed) so if you
|
|
||||||
// have your own "typing" module in your project, we don't consider it THE typing module (and
|
|
||||||
// same for other stdlib modules that our lint rules care about)
|
|
||||||
let Some(typing_override) = context.resolve_global_symbol("typing", "override")? else {
|
|
||||||
// TODO once we bundle typeshed, this should be unreachable!()
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO we should maybe index definitions by type instead of iterating all, or else iterate all
|
|
||||||
// just once, match, and branch to all lint rules that care about a type of definition
|
|
||||||
for (symbol, definition) in context.semantic_index().symbol_table().all_definitions() {
|
|
||||||
if !matches!(definition, Definition::FunctionDef(_)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let ty = infer_definition_type(
|
|
||||||
context.db.upcast(),
|
|
||||||
GlobalSymbolId {
|
|
||||||
file_id: context.file_id,
|
|
||||||
symbol_id: symbol,
|
|
||||||
},
|
|
||||||
definition.clone(),
|
|
||||||
)?;
|
|
||||||
let Type::Function(func) = ty else {
|
|
||||||
unreachable!("type of a FunctionDef should always be a Function");
|
|
||||||
};
|
|
||||||
let Some(class) = func.get_containing_class(context.db.upcast())? else {
|
|
||||||
// not a method of a class
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
if func.has_decorator(context.db.upcast(), typing_override)? {
|
|
||||||
let method_name = func.name(context.db.upcast())?;
|
|
||||||
if class
|
|
||||||
.get_super_class_member(context.db.upcast(), &method_name)?
|
|
||||||
.is_none()
|
|
||||||
{
|
|
||||||
// TODO should have a qualname() method to support nested classes
|
|
||||||
context.push_diagnostic(
|
|
||||||
format!(
|
|
||||||
"Method {}.{} is decorated with `typing.override` but does not override any base class method",
|
|
||||||
class.name(context.db.upcast())?,
|
|
||||||
method_name,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SemanticLintContext<'a> {
|
|
||||||
file_id: FileId,
|
|
||||||
source: Source,
|
|
||||||
parsed: &'a Parsed<ModModule>,
|
|
||||||
semantic_index: Arc<SemanticIndex>,
|
|
||||||
db: &'a dyn LintDb,
|
|
||||||
diagnostics: RefCell<Vec<String>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> SemanticLintContext<'a> {
|
|
||||||
pub fn source_text(&self) -> &str {
|
|
||||||
self.source.text()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_id(&self) -> FileId {
|
|
||||||
self.file_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ast(&self) -> &'a ModModule {
|
|
||||||
self.parsed.syntax()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn semantic_index(&self) -> &SemanticIndex {
|
|
||||||
&self.semantic_index
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn infer_symbol_public_type(&self, symbol_id: SymbolId) -> QueryResult<Type> {
|
|
||||||
infer_symbol_public_type(
|
|
||||||
self.db.upcast(),
|
|
||||||
GlobalSymbolId {
|
|
||||||
file_id: self.file_id,
|
|
||||||
symbol_id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push_diagnostic(&self, diagnostic: String) {
|
|
||||||
self.diagnostics.borrow_mut().push(diagnostic);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extend_diagnostics(&mut self, diagnostics: impl IntoIterator<Item = String>) {
|
|
||||||
self.diagnostics.get_mut().extend(diagnostics);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resolve_global_symbol(
|
|
||||||
&self,
|
|
||||||
module: &str,
|
|
||||||
symbol_name: &str,
|
|
||||||
) -> QueryResult<Option<GlobalSymbolId>> {
|
|
||||||
let Some(module) = resolve_module(self.db.upcast(), ModuleName::new(module))? else {
|
|
||||||
return Ok(None);
|
|
||||||
};
|
|
||||||
|
|
||||||
resolve_global_symbol(self.db.upcast(), module, symbol_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct SyntaxLintVisitor<'a> {
|
|
||||||
diagnostics: Vec<String>,
|
|
||||||
source: &'a str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Visitor<'_> for SyntaxLintVisitor<'_> {
|
|
||||||
fn visit_string_literal(&mut self, string_literal: &'_ StringLiteral) {
|
|
||||||
// A very naive implementation of use double quotes
|
|
||||||
let text = &self.source[string_literal.range];
|
|
||||||
|
|
||||||
if text.starts_with('\'') {
|
|
||||||
self.diagnostics
|
|
||||||
.push("Use double quotes for strings".to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Diagnostics {
|
|
||||||
Empty,
|
|
||||||
List(Arc<Vec<String>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Diagnostics {
|
|
||||||
pub fn as_slice(&self) -> &[String] {
|
|
||||||
match self {
|
|
||||||
Diagnostics::Empty => &[],
|
|
||||||
Diagnostics::List(list) => list.as_slice(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for Diagnostics {
|
|
||||||
type Target = [String];
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
self.as_slice()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Vec<String>> for Diagnostics {
|
|
||||||
fn from(value: Vec<String>) -> Self {
|
|
||||||
if value.is_empty() {
|
|
||||||
Diagnostics::Empty
|
|
||||||
} else {
|
|
||||||
Diagnostics::List(Arc::new(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct LintSyntaxStorage(KeyValueCache<FileId, Diagnostics>);
|
|
||||||
|
|
||||||
impl Deref for LintSyntaxStorage {
|
|
||||||
type Target = KeyValueCache<FileId, Diagnostics>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for LintSyntaxStorage {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct LintSemanticStorage(KeyValueCache<FileId, Diagnostics>);
|
|
||||||
|
|
||||||
impl Deref for LintSemanticStorage {
|
|
||||||
type Target = KeyValueCache<FileId, Diagnostics>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for LintSemanticStorage {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,364 +0,0 @@
|
|||||||
#![allow(clippy::dbg_macro)]
|
|
||||||
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Mutex;
|
|
||||||
|
|
||||||
use crossbeam::channel as crossbeam_channel;
|
|
||||||
use tracing::subscriber::Interest;
|
|
||||||
use tracing::{Level, Metadata};
|
|
||||||
use tracing_subscriber::filter::LevelFilter;
|
|
||||||
use tracing_subscriber::layer::{Context, Filter, SubscriberExt};
|
|
||||||
use tracing_subscriber::{Layer, Registry};
|
|
||||||
use tracing_tree::time::Uptime;
|
|
||||||
|
|
||||||
use red_knot::db::{HasJar, ParallelDatabase, QueryError, SourceDb, SourceJar};
|
|
||||||
use red_knot::module::{set_module_search_paths, ModuleResolutionInputs};
|
|
||||||
use red_knot::program::check::ExecutionMode;
|
|
||||||
use red_knot::program::{FileWatcherChange, Program};
|
|
||||||
use red_knot::watch::FileWatcher;
|
|
||||||
use red_knot::Workspace;
|
|
||||||
|
|
||||||
#[allow(clippy::print_stdout, clippy::unnecessary_wraps, clippy::print_stderr)]
|
|
||||||
fn main() -> anyhow::Result<()> {
|
|
||||||
setup_tracing();
|
|
||||||
|
|
||||||
let arguments: Vec<_> = std::env::args().collect();
|
|
||||||
|
|
||||||
if arguments.len() < 2 {
|
|
||||||
eprintln!("Usage: red_knot <path>");
|
|
||||||
return Err(anyhow::anyhow!("Invalid arguments"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let entry_point = Path::new(&arguments[1]);
|
|
||||||
|
|
||||||
if !entry_point.exists() {
|
|
||||||
eprintln!("The entry point does not exist.");
|
|
||||||
return Err(anyhow::anyhow!("Invalid arguments"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !entry_point.is_file() {
|
|
||||||
eprintln!("The entry point is not a file.");
|
|
||||||
return Err(anyhow::anyhow!("Invalid arguments"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let workspace_folder = entry_point.parent().unwrap();
|
|
||||||
let workspace = Workspace::new(workspace_folder.to_path_buf());
|
|
||||||
|
|
||||||
let workspace_search_path = workspace.root().to_path_buf();
|
|
||||||
|
|
||||||
let search_paths = ModuleResolutionInputs {
|
|
||||||
extra_paths: vec![],
|
|
||||||
workspace_root: workspace_search_path,
|
|
||||||
site_packages: None,
|
|
||||||
custom_typeshed: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut program = Program::new(workspace);
|
|
||||||
set_module_search_paths(&mut program, search_paths);
|
|
||||||
|
|
||||||
let entry_id = program.file_id(entry_point);
|
|
||||||
program.workspace_mut().open_file(entry_id);
|
|
||||||
|
|
||||||
let (main_loop, main_loop_cancellation_token) = MainLoop::new();
|
|
||||||
|
|
||||||
// Listen to Ctrl+C and abort the watch mode.
|
|
||||||
let main_loop_cancellation_token = Mutex::new(Some(main_loop_cancellation_token));
|
|
||||||
ctrlc::set_handler(move || {
|
|
||||||
let mut lock = main_loop_cancellation_token.lock().unwrap();
|
|
||||||
|
|
||||||
if let Some(token) = lock.take() {
|
|
||||||
token.stop();
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let file_changes_notifier = main_loop.file_changes_notifier();
|
|
||||||
|
|
||||||
// Watch for file changes and re-trigger the analysis.
|
|
||||||
let mut file_watcher = FileWatcher::new(move |changes| {
|
|
||||||
file_changes_notifier.notify(changes);
|
|
||||||
})?;
|
|
||||||
|
|
||||||
file_watcher.watch_folder(workspace_folder)?;
|
|
||||||
|
|
||||||
main_loop.run(&mut program);
|
|
||||||
|
|
||||||
let source_jar: &SourceJar = program.jar().unwrap();
|
|
||||||
|
|
||||||
dbg!(source_jar.parsed.statistics());
|
|
||||||
dbg!(source_jar.sources.statistics());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
struct MainLoop {
|
|
||||||
orchestrator_sender: crossbeam_channel::Sender<OrchestratorMessage>,
|
|
||||||
main_loop_receiver: crossbeam_channel::Receiver<MainLoopMessage>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MainLoop {
|
|
||||||
fn new() -> (Self, MainLoopCancellationToken) {
|
|
||||||
let (orchestrator_sender, orchestrator_receiver) = crossbeam_channel::bounded(1);
|
|
||||||
let (main_loop_sender, main_loop_receiver) = crossbeam_channel::bounded(1);
|
|
||||||
|
|
||||||
let mut orchestrator = Orchestrator {
|
|
||||||
receiver: orchestrator_receiver,
|
|
||||||
sender: main_loop_sender.clone(),
|
|
||||||
revision: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
orchestrator.run();
|
|
||||||
});
|
|
||||||
|
|
||||||
(
|
|
||||||
Self {
|
|
||||||
orchestrator_sender,
|
|
||||||
main_loop_receiver,
|
|
||||||
},
|
|
||||||
MainLoopCancellationToken {
|
|
||||||
sender: main_loop_sender,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_changes_notifier(&self) -> FileChangesNotifier {
|
|
||||||
FileChangesNotifier {
|
|
||||||
sender: self.orchestrator_sender.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(self, program: &mut Program) {
|
|
||||||
self.orchestrator_sender
|
|
||||||
.send(OrchestratorMessage::Run)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
for message in &self.main_loop_receiver {
|
|
||||||
tracing::trace!("Main Loop: Tick");
|
|
||||||
|
|
||||||
match message {
|
|
||||||
MainLoopMessage::CheckProgram { revision } => {
|
|
||||||
let program = program.snapshot();
|
|
||||||
let sender = self.orchestrator_sender.clone();
|
|
||||||
|
|
||||||
// Spawn a new task that checks the program. This needs to be done in a separate thread
|
|
||||||
// to prevent blocking the main loop here.
|
|
||||||
rayon::spawn(move || match program.check(ExecutionMode::ThreadPool) {
|
|
||||||
Ok(result) => {
|
|
||||||
sender
|
|
||||||
.send(OrchestratorMessage::CheckProgramCompleted {
|
|
||||||
diagnostics: result,
|
|
||||||
revision,
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
Err(QueryError::Cancelled) => {}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
MainLoopMessage::ApplyChanges(changes) => {
|
|
||||||
// Automatically cancels any pending queries and waits for them to complete.
|
|
||||||
program.apply_changes(changes);
|
|
||||||
}
|
|
||||||
MainLoopMessage::CheckCompleted(diagnostics) => {
|
|
||||||
dbg!(diagnostics);
|
|
||||||
}
|
|
||||||
MainLoopMessage::Exit => {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for MainLoop {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.orchestrator_sender
|
|
||||||
.send(OrchestratorMessage::Shutdown)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct FileChangesNotifier {
|
|
||||||
sender: crossbeam_channel::Sender<OrchestratorMessage>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileChangesNotifier {
|
|
||||||
fn notify(&self, changes: Vec<FileWatcherChange>) {
|
|
||||||
self.sender
|
|
||||||
.send(OrchestratorMessage::FileChanges(changes))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct MainLoopCancellationToken {
|
|
||||||
sender: crossbeam_channel::Sender<MainLoopMessage>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MainLoopCancellationToken {
|
|
||||||
fn stop(self) {
|
|
||||||
self.sender.send(MainLoopMessage::Exit).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Orchestrator {
|
|
||||||
/// Sends messages to the main loop.
|
|
||||||
sender: crossbeam_channel::Sender<MainLoopMessage>,
|
|
||||||
/// Receives messages from the main loop.
|
|
||||||
receiver: crossbeam_channel::Receiver<OrchestratorMessage>,
|
|
||||||
revision: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Orchestrator {
|
|
||||||
fn run(&mut self) {
|
|
||||||
while let Ok(message) = self.receiver.recv() {
|
|
||||||
match message {
|
|
||||||
OrchestratorMessage::Run => {
|
|
||||||
self.sender
|
|
||||||
.send(MainLoopMessage::CheckProgram {
|
|
||||||
revision: self.revision,
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
OrchestratorMessage::CheckProgramCompleted {
|
|
||||||
diagnostics,
|
|
||||||
revision,
|
|
||||||
} => {
|
|
||||||
// Only take the diagnostics if they are for the latest revision.
|
|
||||||
if self.revision == revision {
|
|
||||||
self.sender
|
|
||||||
.send(MainLoopMessage::CheckCompleted(diagnostics))
|
|
||||||
.unwrap();
|
|
||||||
} else {
|
|
||||||
tracing::debug!("Discarding diagnostics for outdated revision {revision} (current: {}).", self.revision);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OrchestratorMessage::FileChanges(changes) => {
|
|
||||||
// Request cancellation, but wait until all analysis tasks have completed to
|
|
||||||
// avoid stale messages in the next main loop.
|
|
||||||
|
|
||||||
self.revision += 1;
|
|
||||||
self.debounce_changes(changes);
|
|
||||||
}
|
|
||||||
OrchestratorMessage::Shutdown => {
|
|
||||||
return self.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn debounce_changes(&self, mut changes: Vec<FileWatcherChange>) {
|
|
||||||
loop {
|
|
||||||
// Consume possibly incoming file change messages before running a new analysis, but don't wait for more than 100ms.
|
|
||||||
crossbeam_channel::select! {
|
|
||||||
recv(self.receiver) -> message => {
|
|
||||||
match message {
|
|
||||||
Ok(OrchestratorMessage::Shutdown) => {
|
|
||||||
return self.shutdown();
|
|
||||||
}
|
|
||||||
Ok(OrchestratorMessage::FileChanges(file_changes)) => {
|
|
||||||
changes.extend(file_changes);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(OrchestratorMessage::CheckProgramCompleted { .. })=> {
|
|
||||||
// disregard any outdated completion message.
|
|
||||||
}
|
|
||||||
Ok(OrchestratorMessage::Run) => unreachable!("The orchestrator is already running."),
|
|
||||||
|
|
||||||
Err(_) => {
|
|
||||||
// There are no more senders, no point in waiting for more messages
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
default(std::time::Duration::from_millis(10)) => {
|
|
||||||
// No more file changes after 10 ms, send the changes and schedule a new analysis
|
|
||||||
self.sender.send(MainLoopMessage::ApplyChanges(changes)).unwrap();
|
|
||||||
self.sender.send(MainLoopMessage::CheckProgram { revision: self.revision}).unwrap();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::unused_self)]
|
|
||||||
fn shutdown(&self) {
|
|
||||||
tracing::trace!("Shutting down orchestrator.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Message sent from the orchestrator to the main loop.
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum MainLoopMessage {
|
|
||||||
CheckProgram { revision: usize },
|
|
||||||
CheckCompleted(Vec<String>),
|
|
||||||
ApplyChanges(Vec<FileWatcherChange>),
|
|
||||||
Exit,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum OrchestratorMessage {
|
|
||||||
Run,
|
|
||||||
Shutdown,
|
|
||||||
|
|
||||||
CheckProgramCompleted {
|
|
||||||
diagnostics: Vec<String>,
|
|
||||||
revision: usize,
|
|
||||||
},
|
|
||||||
|
|
||||||
FileChanges(Vec<FileWatcherChange>),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup_tracing() {
|
|
||||||
let subscriber = Registry::default().with(
|
|
||||||
tracing_tree::HierarchicalLayer::default()
|
|
||||||
.with_indent_lines(true)
|
|
||||||
.with_indent_amount(2)
|
|
||||||
.with_bracketed_fields(true)
|
|
||||||
.with_thread_ids(true)
|
|
||||||
.with_targets(true)
|
|
||||||
.with_writer(|| Box::new(std::io::stderr()))
|
|
||||||
.with_timer(Uptime::default())
|
|
||||||
.with_filter(LoggingFilter {
|
|
||||||
trace_level: Level::TRACE,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct LoggingFilter {
|
|
||||||
trace_level: Level,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LoggingFilter {
|
|
||||||
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
|
|
||||||
let filter = if meta.target().starts_with("red_knot") || meta.target().starts_with("ruff") {
|
|
||||||
self.trace_level
|
|
||||||
} else {
|
|
||||||
Level::INFO
|
|
||||||
};
|
|
||||||
|
|
||||||
meta.level() <= &filter
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> Filter<S> for LoggingFilter {
|
|
||||||
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool {
|
|
||||||
self.is_enabled(meta)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest {
|
|
||||||
if self.is_enabled(meta) {
|
|
||||||
Interest::always()
|
|
||||||
} else {
|
|
||||||
Interest::never()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn max_level_hint(&self) -> Option<LevelFilter> {
|
|
||||||
Some(LevelFilter::from_level(self.trace_level))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,41 +0,0 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use ruff_python_ast::ModModule;
|
|
||||||
use ruff_python_parser::Parsed;
|
|
||||||
|
|
||||||
use crate::cache::KeyValueCache;
|
|
||||||
use crate::db::{QueryResult, SourceDb};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::source::source_text;
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub(crate) fn parse(db: &dyn SourceDb, file_id: FileId) -> QueryResult<Arc<Parsed<ModModule>>> {
|
|
||||||
let jar = db.jar()?;
|
|
||||||
|
|
||||||
jar.parsed.get(&file_id, |file_id| {
|
|
||||||
let source = source_text(db, *file_id)?;
|
|
||||||
|
|
||||||
Ok(Arc::new(ruff_python_parser::parse_unchecked_source(
|
|
||||||
source.text(),
|
|
||||||
source.kind().into(),
|
|
||||||
)))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct ParsedStorage(KeyValueCache<FileId, Arc<Parsed<ModModule>>>);
|
|
||||||
|
|
||||||
impl Deref for ParsedStorage {
|
|
||||||
type Target = KeyValueCache<FileId, Arc<Parsed<ModModule>>>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for ParsedStorage {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,413 +0,0 @@
|
|||||||
use rayon::{current_num_threads, yield_local};
|
|
||||||
use rustc_hash::FxHashSet;
|
|
||||||
|
|
||||||
use crate::db::{Database, QueryError, QueryResult};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::lint::{lint_semantic, lint_syntax, Diagnostics};
|
|
||||||
use crate::module::{file_to_module, resolve_module};
|
|
||||||
use crate::program::Program;
|
|
||||||
use crate::semantic::{semantic_index, Dependency};
|
|
||||||
|
|
||||||
impl Program {
|
|
||||||
/// Checks all open files in the workspace and its dependencies.
|
|
||||||
#[tracing::instrument(level = "debug", skip_all)]
|
|
||||||
pub fn check(&self, mode: ExecutionMode) -> QueryResult<Vec<String>> {
|
|
||||||
self.cancelled()?;
|
|
||||||
|
|
||||||
let mut context = CheckContext::new(self);
|
|
||||||
|
|
||||||
match mode {
|
|
||||||
ExecutionMode::SingleThreaded => SingleThreadedExecutor.run(&mut context)?,
|
|
||||||
ExecutionMode::ThreadPool => ThreadPoolExecutor.run(&mut context)?,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(context.finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(self, context))]
|
|
||||||
fn check_file(&self, file: FileId, context: &CheckFileContext) -> QueryResult<Diagnostics> {
|
|
||||||
self.cancelled()?;
|
|
||||||
|
|
||||||
let index = semantic_index(self, file)?;
|
|
||||||
let dependencies = index.symbol_table().dependencies();
|
|
||||||
|
|
||||||
if !dependencies.is_empty() {
|
|
||||||
let module = file_to_module(self, file)?;
|
|
||||||
|
|
||||||
// TODO scheduling all dependencies here is wasteful if we don't infer any types on them
|
|
||||||
// but I think that's unlikely, so it is okay?
|
|
||||||
// Anyway, we need to figure out a way to retrieve the dependencies of a module
|
|
||||||
// from the persistent cache. So maybe it should be a separate query after all.
|
|
||||||
for dependency in dependencies {
|
|
||||||
let dependency_name = match dependency {
|
|
||||||
Dependency::Module(name) => Some(name.clone()),
|
|
||||||
Dependency::Relative { .. } => match &module {
|
|
||||||
Some(module) => module.resolve_dependency(self, dependency)?,
|
|
||||||
None => None,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(dependency_name) = dependency_name {
|
|
||||||
// TODO We may want to have a different check functions for non-first-party
|
|
||||||
// files because we only need to index them and not check them.
|
|
||||||
// Supporting non-first-party code also requires supporting typing stubs.
|
|
||||||
if let Some(dependency) = resolve_module(self, dependency_name)? {
|
|
||||||
if dependency.path(self)?.root().kind().is_first_party() {
|
|
||||||
context.schedule_dependency(dependency.path(self)?.file());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut diagnostics = Vec::new();
|
|
||||||
|
|
||||||
if self.workspace().is_file_open(file) {
|
|
||||||
diagnostics.extend_from_slice(&lint_syntax(self, file)?);
|
|
||||||
diagnostics.extend_from_slice(&lint_semantic(self, file)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Diagnostics::from(diagnostics))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub enum ExecutionMode {
|
|
||||||
SingleThreaded,
|
|
||||||
ThreadPool,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Context that stores state information about the entire check operation.
|
|
||||||
struct CheckContext<'a> {
|
|
||||||
/// IDs of the files that have been queued for checking.
|
|
||||||
///
|
|
||||||
/// Used to avoid queuing the same file twice.
|
|
||||||
scheduled_files: FxHashSet<FileId>,
|
|
||||||
|
|
||||||
/// Reference to the program that is checked.
|
|
||||||
program: &'a Program,
|
|
||||||
|
|
||||||
/// The aggregated diagnostics
|
|
||||||
diagnostics: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> CheckContext<'a> {
|
|
||||||
fn new(program: &'a Program) -> Self {
|
|
||||||
Self {
|
|
||||||
scheduled_files: FxHashSet::default(),
|
|
||||||
program,
|
|
||||||
diagnostics: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the tasks to check all open files in the workspace.
|
|
||||||
fn check_open_files(&mut self) -> Vec<CheckOpenFileTask> {
|
|
||||||
self.scheduled_files
|
|
||||||
.extend(self.program.workspace().open_files());
|
|
||||||
|
|
||||||
self.program
|
|
||||||
.workspace()
|
|
||||||
.open_files()
|
|
||||||
.map(|file_id| CheckOpenFileTask { file_id })
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the task to check a dependency.
|
|
||||||
fn check_dependency(&mut self, file_id: FileId) -> Option<CheckDependencyTask> {
|
|
||||||
if self.scheduled_files.insert(file_id) {
|
|
||||||
Some(CheckDependencyTask { file_id })
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pushes the result for a single file check operation
|
|
||||||
fn push_diagnostics(&mut self, diagnostics: &Diagnostics) {
|
|
||||||
self.diagnostics.extend_from_slice(diagnostics);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the program that is being checked.
|
|
||||||
fn program(&self) -> &'a Program {
|
|
||||||
self.program
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a task context that is used to check a single file.
|
|
||||||
fn task_context<'b, S>(&self, dependency_scheduler: &'b S) -> CheckTaskContext<'a, 'b, S>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
CheckTaskContext {
|
|
||||||
program: self.program,
|
|
||||||
dependency_scheduler,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finish(self) -> Vec<String> {
|
|
||||||
self.diagnostics
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait that abstracts away how a dependency of a file gets scheduled for checking.
|
|
||||||
trait ScheduleDependency {
|
|
||||||
/// Schedules the file with the given ID for checking.
|
|
||||||
fn schedule(&self, file_id: FileId);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ScheduleDependency for T
|
|
||||||
where
|
|
||||||
T: Fn(FileId),
|
|
||||||
{
|
|
||||||
fn schedule(&self, file_id: FileId) {
|
|
||||||
let f = self;
|
|
||||||
f(file_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Context that is used to run a single file check task.
|
|
||||||
///
|
|
||||||
/// The task is generic over `S` because it is passed across thread boundaries and
|
|
||||||
/// we don't want to add the requirement that [`ScheduleDependency`] must be [`Send`].
|
|
||||||
struct CheckTaskContext<'a, 'scheduler, S>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
dependency_scheduler: &'scheduler S,
|
|
||||||
program: &'a Program,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, 'scheduler, S> CheckTaskContext<'a, 'scheduler, S>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
fn as_file_context(&self) -> CheckFileContext<'scheduler> {
|
|
||||||
CheckFileContext {
|
|
||||||
dependency_scheduler: self.dependency_scheduler,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Context passed when checking a single file.
|
|
||||||
///
|
|
||||||
/// This is a trimmed down version of [`CheckTaskContext`] with the type parameter `S` erased
|
|
||||||
/// to avoid monomorphization of [`Program:check_file`].
|
|
||||||
struct CheckFileContext<'a> {
|
|
||||||
dependency_scheduler: &'a dyn ScheduleDependency,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> CheckFileContext<'a> {
|
|
||||||
fn schedule_dependency(&self, file_id: FileId) {
|
|
||||||
self.dependency_scheduler.schedule(file_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum CheckFileTask {
|
|
||||||
OpenFile(CheckOpenFileTask),
|
|
||||||
Dependency(CheckDependencyTask),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CheckFileTask {
|
|
||||||
/// Runs the task and returns the results for checking this file.
|
|
||||||
fn run<S>(&self, context: &CheckTaskContext<S>) -> QueryResult<Diagnostics>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
match self {
|
|
||||||
Self::OpenFile(task) => task.run(context),
|
|
||||||
Self::Dependency(task) => task.run(context),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_id(&self) -> FileId {
|
|
||||||
match self {
|
|
||||||
CheckFileTask::OpenFile(task) => task.file_id,
|
|
||||||
CheckFileTask::Dependency(task) => task.file_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Task to check an open file.
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct CheckOpenFileTask {
|
|
||||||
file_id: FileId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CheckOpenFileTask {
|
|
||||||
fn run<S>(&self, context: &CheckTaskContext<S>) -> QueryResult<Diagnostics>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
context
|
|
||||||
.program
|
|
||||||
.check_file(self.file_id, &context.as_file_context())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Task to check a dependency file.
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct CheckDependencyTask {
|
|
||||||
file_id: FileId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CheckDependencyTask {
|
|
||||||
fn run<S>(&self, context: &CheckTaskContext<S>) -> QueryResult<Diagnostics>
|
|
||||||
where
|
|
||||||
S: ScheduleDependency,
|
|
||||||
{
|
|
||||||
context
|
|
||||||
.program
|
|
||||||
.check_file(self.file_id, &context.as_file_context())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Executor that schedules the checking of individual program files.
|
|
||||||
trait CheckExecutor {
|
|
||||||
fn run(self, context: &mut CheckContext) -> QueryResult<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Executor that runs all check operations on the current thread.
|
|
||||||
///
|
|
||||||
/// The executor does not schedule dependencies for checking.
|
|
||||||
/// The main motivation for scheduling dependencies
|
|
||||||
/// in a multithreaded environment is to parse and index the dependencies concurrently.
|
|
||||||
/// However, that doesn't make sense in a single threaded environment, because the dependencies then compute
|
|
||||||
/// with checking the open files. Checking dependencies in a single threaded environment is more likely
|
|
||||||
/// to hurt performance because we end up analyzing files in their entirety, even if we only need to type check parts of them.
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
struct SingleThreadedExecutor;
|
|
||||||
|
|
||||||
impl CheckExecutor for SingleThreadedExecutor {
|
|
||||||
fn run(self, context: &mut CheckContext) -> QueryResult<()> {
|
|
||||||
let mut queue = context.check_open_files();
|
|
||||||
|
|
||||||
let noop_schedule_dependency = |_| {};
|
|
||||||
|
|
||||||
while let Some(file) = queue.pop() {
|
|
||||||
context.program().cancelled()?;
|
|
||||||
|
|
||||||
let task_context = context.task_context(&noop_schedule_dependency);
|
|
||||||
context.push_diagnostics(&file.run(&task_context)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Executor that runs the check operations on a thread pool.
|
|
||||||
///
|
|
||||||
/// The executor runs each check operation as its own task using a thread pool.
|
|
||||||
///
|
|
||||||
/// Other than [`SingleThreadedExecutor`], this executor schedules dependencies for checking. It
|
|
||||||
/// even schedules dependencies for checking when the thread pool size is 1 for a better debugging experience.
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
struct ThreadPoolExecutor;
|
|
||||||
|
|
||||||
impl CheckExecutor for ThreadPoolExecutor {
|
|
||||||
fn run(self, context: &mut CheckContext) -> QueryResult<()> {
|
|
||||||
let num_threads = current_num_threads();
|
|
||||||
let single_threaded = num_threads == 1;
|
|
||||||
let span = tracing::trace_span!("ThreadPoolExecutor::run", num_threads);
|
|
||||||
let _ = span.enter();
|
|
||||||
|
|
||||||
let mut queue: Vec<_> = context
|
|
||||||
.check_open_files()
|
|
||||||
.into_iter()
|
|
||||||
.map(CheckFileTask::OpenFile)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let (sender, receiver) = if single_threaded {
|
|
||||||
// Use an unbounded queue for single threaded execution to prevent deadlocks
|
|
||||||
// when a single file schedules multiple dependencies.
|
|
||||||
crossbeam::channel::unbounded()
|
|
||||||
} else {
|
|
||||||
// Use a bounded queue to apply backpressure when the orchestration thread isn't able to keep
|
|
||||||
// up processing messages from the worker threads.
|
|
||||||
crossbeam::channel::bounded(num_threads)
|
|
||||||
};
|
|
||||||
|
|
||||||
let schedule_sender = sender.clone();
|
|
||||||
let schedule_dependency = move |file_id| {
|
|
||||||
schedule_sender
|
|
||||||
.send(ThreadPoolMessage::ScheduleDependency(file_id))
|
|
||||||
.unwrap();
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = rayon::in_place_scope(|scope| {
|
|
||||||
let mut pending = 0usize;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
context.program().cancelled()?;
|
|
||||||
|
|
||||||
// 1. Try to get a queued message to ensure that we have always remaining space in the channel to prevent blocking the worker threads.
|
|
||||||
// 2. Try to process a queued file
|
|
||||||
// 3. If there's no queued file wait for the next incoming message.
|
|
||||||
// 4. Exit if there are no more messages and no senders.
|
|
||||||
let message = if let Ok(message) = receiver.try_recv() {
|
|
||||||
message
|
|
||||||
} else if let Some(task) = queue.pop() {
|
|
||||||
pending += 1;
|
|
||||||
|
|
||||||
let task_context = context.task_context(&schedule_dependency);
|
|
||||||
let sender = sender.clone();
|
|
||||||
let task_span = tracing::trace_span!(
|
|
||||||
parent: &span,
|
|
||||||
"CheckFileTask::run",
|
|
||||||
file_id = task.file_id().as_u32(),
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.spawn(move |_| {
|
|
||||||
task_span.in_scope(|| match task.run(&task_context) {
|
|
||||||
Ok(result) => {
|
|
||||||
sender.send(ThreadPoolMessage::Completed(result)).unwrap();
|
|
||||||
}
|
|
||||||
Err(err) => sender.send(ThreadPoolMessage::Errored(err)).unwrap(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// If this is a single threaded rayon thread pool, yield the current thread
|
|
||||||
// or we never start processing the work items.
|
|
||||||
if single_threaded {
|
|
||||||
yield_local();
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
} else if let Ok(message) = receiver.recv() {
|
|
||||||
message
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
|
|
||||||
match message {
|
|
||||||
ThreadPoolMessage::ScheduleDependency(dependency) => {
|
|
||||||
if let Some(task) = context.check_dependency(dependency) {
|
|
||||||
queue.push(CheckFileTask::Dependency(task));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ThreadPoolMessage::Completed(diagnostics) => {
|
|
||||||
context.push_diagnostics(&diagnostics);
|
|
||||||
pending -= 1;
|
|
||||||
|
|
||||||
if pending == 0 && queue.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ThreadPoolMessage::Errored(err) => {
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum ThreadPoolMessage {
|
|
||||||
ScheduleDependency(FileId),
|
|
||||||
Completed(Diagnostics),
|
|
||||||
Errored(QueryError),
|
|
||||||
}
|
|
||||||
@@ -1,275 +0,0 @@
|
|||||||
use std::collections::hash_map::Entry;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
|
|
||||||
use crate::db::{
|
|
||||||
Database, Db, DbRuntime, DbWithJar, HasJar, HasJars, JarsStorage, LintDb, LintJar,
|
|
||||||
ParallelDatabase, QueryResult, SemanticDb, SemanticJar, Snapshot, SourceDb, SourceJar, Upcast,
|
|
||||||
};
|
|
||||||
use crate::files::{FileId, Files};
|
|
||||||
use crate::Workspace;
|
|
||||||
|
|
||||||
pub mod check;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Program {
|
|
||||||
jars: JarsStorage<Program>,
|
|
||||||
files: Files,
|
|
||||||
workspace: Workspace,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Program {
|
|
||||||
pub fn new(workspace: Workspace) -> Self {
|
|
||||||
Self {
|
|
||||||
jars: JarsStorage::default(),
|
|
||||||
files: Files::default(),
|
|
||||||
workspace,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn apply_changes<I>(&mut self, changes: I)
|
|
||||||
where
|
|
||||||
I: IntoIterator<Item = FileWatcherChange>,
|
|
||||||
{
|
|
||||||
let mut aggregated_changes = AggregatedChanges::default();
|
|
||||||
|
|
||||||
aggregated_changes.extend(changes.into_iter().map(|change| FileChange {
|
|
||||||
id: self.files.intern(&change.path),
|
|
||||||
kind: change.kind,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let (source, semantic, lint) = self.jars_mut();
|
|
||||||
for change in aggregated_changes.iter() {
|
|
||||||
semantic.module_resolver.remove_module_by_file(change.id);
|
|
||||||
semantic.semantic_indices.remove(&change.id);
|
|
||||||
source.sources.remove(&change.id);
|
|
||||||
source.parsed.remove(&change.id);
|
|
||||||
// TODO: remove all dependent modules as well
|
|
||||||
semantic.type_store.remove_module(change.id);
|
|
||||||
lint.lint_syntax.remove(&change.id);
|
|
||||||
lint.lint_semantic.remove(&change.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn files(&self) -> &Files {
|
|
||||||
&self.files
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn workspace(&self) -> &Workspace {
|
|
||||||
&self.workspace
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn workspace_mut(&mut self) -> &mut Workspace {
|
|
||||||
&mut self.workspace
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceDb for Program {
|
|
||||||
fn file_id(&self, path: &Path) -> FileId {
|
|
||||||
self.files.intern(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_path(&self, file_id: FileId) -> Arc<Path> {
|
|
||||||
self.files.path(file_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DbWithJar<SourceJar> for Program {}
|
|
||||||
|
|
||||||
impl SemanticDb for Program {}
|
|
||||||
|
|
||||||
impl DbWithJar<SemanticJar> for Program {}
|
|
||||||
|
|
||||||
impl LintDb for Program {}
|
|
||||||
|
|
||||||
impl DbWithJar<LintJar> for Program {}
|
|
||||||
|
|
||||||
impl Upcast<dyn SemanticDb> for Program {
|
|
||||||
fn upcast(&self) -> &(dyn SemanticDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Upcast<dyn SourceDb> for Program {
|
|
||||||
fn upcast(&self) -> &(dyn SourceDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Upcast<dyn LintDb> for Program {
|
|
||||||
fn upcast(&self) -> &(dyn LintDb + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Db for Program {}
|
|
||||||
|
|
||||||
impl Database for Program {
|
|
||||||
fn runtime(&self) -> &DbRuntime {
|
|
||||||
self.jars.runtime()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn runtime_mut(&mut self) -> &mut DbRuntime {
|
|
||||||
self.jars.runtime_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParallelDatabase for Program {
|
|
||||||
fn snapshot(&self) -> Snapshot<Self> {
|
|
||||||
Snapshot::new(Self {
|
|
||||||
jars: self.jars.snapshot(),
|
|
||||||
files: self.files.snapshot(),
|
|
||||||
workspace: self.workspace.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJars for Program {
|
|
||||||
type Jars = (SourceJar, SemanticJar, LintJar);
|
|
||||||
|
|
||||||
fn jars(&self) -> QueryResult<&Self::Jars> {
|
|
||||||
self.jars.jars()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jars_mut(&mut self) -> &mut Self::Jars {
|
|
||||||
self.jars.jars_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<SourceJar> for Program {
|
|
||||||
fn jar(&self) -> QueryResult<&SourceJar> {
|
|
||||||
Ok(&self.jars()?.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut SourceJar {
|
|
||||||
&mut self.jars_mut().0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<SemanticJar> for Program {
|
|
||||||
fn jar(&self) -> QueryResult<&SemanticJar> {
|
|
||||||
Ok(&self.jars()?.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut SemanticJar {
|
|
||||||
&mut self.jars_mut().1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasJar<LintJar> for Program {
|
|
||||||
fn jar(&self) -> QueryResult<&LintJar> {
|
|
||||||
Ok(&self.jars()?.2)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn jar_mut(&mut self) -> &mut LintJar {
|
|
||||||
&mut self.jars_mut().2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct FileWatcherChange {
|
|
||||||
path: PathBuf,
|
|
||||||
kind: FileChangeKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileWatcherChange {
|
|
||||||
pub fn new(path: PathBuf, kind: FileChangeKind) -> Self {
|
|
||||||
Self { path, kind }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
|
||||||
struct FileChange {
|
|
||||||
id: FileId,
|
|
||||||
kind: FileChangeKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileChange {
|
|
||||||
fn file_id(self) -> FileId {
|
|
||||||
self.id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn kind(self) -> FileChangeKind {
|
|
||||||
self.kind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
||||||
pub enum FileChangeKind {
|
|
||||||
Created,
|
|
||||||
Modified,
|
|
||||||
Deleted,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
struct AggregatedChanges {
|
|
||||||
changes: FxHashMap<FileId, FileChangeKind>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AggregatedChanges {
|
|
||||||
fn add(&mut self, change: FileChange) {
|
|
||||||
match self.changes.entry(change.file_id()) {
|
|
||||||
Entry::Occupied(mut entry) => {
|
|
||||||
let merged = entry.get_mut();
|
|
||||||
|
|
||||||
match (merged, change.kind()) {
|
|
||||||
(FileChangeKind::Created, FileChangeKind::Deleted) => {
|
|
||||||
// Deletion after creations means that ruff never saw the file.
|
|
||||||
entry.remove();
|
|
||||||
}
|
|
||||||
(FileChangeKind::Created, FileChangeKind::Modified) => {
|
|
||||||
// No-op, for ruff, modifying a file that it doesn't yet know that it exists is still considered a creation.
|
|
||||||
}
|
|
||||||
|
|
||||||
(FileChangeKind::Modified, FileChangeKind::Created) => {
|
|
||||||
// Uhh, that should probably not happen. Continue considering it a modification.
|
|
||||||
}
|
|
||||||
|
|
||||||
(FileChangeKind::Modified, FileChangeKind::Deleted) => {
|
|
||||||
*entry.get_mut() = FileChangeKind::Deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
(FileChangeKind::Deleted, FileChangeKind::Created) => {
|
|
||||||
*entry.get_mut() = FileChangeKind::Modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
(FileChangeKind::Deleted, FileChangeKind::Modified) => {
|
|
||||||
// That's weird, but let's consider it a modification.
|
|
||||||
*entry.get_mut() = FileChangeKind::Modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
(FileChangeKind::Created, FileChangeKind::Created)
|
|
||||||
| (FileChangeKind::Modified, FileChangeKind::Modified)
|
|
||||||
| (FileChangeKind::Deleted, FileChangeKind::Deleted) => {
|
|
||||||
// No-op transitions. Some of them should be impossible but we handle them anyway.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Entry::Vacant(entry) => {
|
|
||||||
entry.insert(change.kind());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extend<I>(&mut self, changes: I)
|
|
||||||
where
|
|
||||||
I: IntoIterator<Item = FileChange>,
|
|
||||||
{
|
|
||||||
let iter = changes.into_iter();
|
|
||||||
let (lower, _) = iter.size_hint();
|
|
||||||
self.changes.reserve(lower);
|
|
||||||
|
|
||||||
for change in iter {
|
|
||||||
self.add(change);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iter(&self) -> impl Iterator<Item = FileChange> + '_ {
|
|
||||||
self.changes.iter().map(|(id, kind)| FileChange {
|
|
||||||
id: *id,
|
|
||||||
kind: *kind,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,882 +0,0 @@
|
|||||||
use std::num::NonZeroU32;
|
|
||||||
|
|
||||||
use ruff_python_ast as ast;
|
|
||||||
use ruff_python_ast::visitor::source_order::SourceOrderVisitor;
|
|
||||||
use ruff_python_ast::AstNode;
|
|
||||||
|
|
||||||
use crate::ast_ids::{NodeKey, TypedNodeKey};
|
|
||||||
use crate::cache::KeyValueCache;
|
|
||||||
use crate::db::{QueryResult, SemanticDb, SemanticJar};
|
|
||||||
use crate::files::FileId;
|
|
||||||
use crate::module::Module;
|
|
||||||
use crate::module::ModuleName;
|
|
||||||
use crate::parse::parse;
|
|
||||||
use crate::Name;
|
|
||||||
pub(crate) use definitions::Definition;
|
|
||||||
use definitions::{ImportDefinition, ImportFromDefinition};
|
|
||||||
pub(crate) use flow_graph::ConstrainedDefinition;
|
|
||||||
use flow_graph::{FlowGraph, FlowGraphBuilder, FlowNodeId, ReachableDefinitionsIterator};
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::Arc;
|
|
||||||
pub(crate) use symbol_table::{Dependency, SymbolId};
|
|
||||||
use symbol_table::{ScopeId, ScopeKind, SymbolFlags, SymbolTable, SymbolTableBuilder};
|
|
||||||
pub(crate) use types::{infer_definition_type, infer_symbol_public_type, Type, TypeStore};
|
|
||||||
|
|
||||||
mod definitions;
|
|
||||||
mod flow_graph;
|
|
||||||
mod symbol_table;
|
|
||||||
mod types;
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub fn semantic_index(db: &dyn SemanticDb, file_id: FileId) -> QueryResult<Arc<SemanticIndex>> {
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
|
|
||||||
jar.semantic_indices.get(&file_id, |_| {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
Ok(Arc::from(SemanticIndex::from_ast(parsed.syntax())))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub fn resolve_global_symbol(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
module: Module,
|
|
||||||
name: &str,
|
|
||||||
) -> QueryResult<Option<GlobalSymbolId>> {
|
|
||||||
let file_id = module.path(db)?.file();
|
|
||||||
let symbol_table = &semantic_index(db, file_id)?.symbol_table;
|
|
||||||
let Some(symbol_id) = symbol_table.root_symbol_id_by_name(name) else {
|
|
||||||
return Ok(None);
|
|
||||||
};
|
|
||||||
Ok(Some(GlobalSymbolId { file_id, symbol_id }))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct ExpressionId;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub struct GlobalSymbolId {
|
|
||||||
pub(crate) file_id: FileId,
|
|
||||||
pub(crate) symbol_id: SymbolId,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct SemanticIndex {
|
|
||||||
symbol_table: SymbolTable,
|
|
||||||
flow_graph: FlowGraph,
|
|
||||||
expressions: FxHashMap<NodeKey, ExpressionId>,
|
|
||||||
expressions_by_id: IndexVec<ExpressionId, NodeKey>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SemanticIndex {
|
|
||||||
pub fn from_ast(module: &ast::ModModule) -> Self {
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let mut indexer = SemanticIndexer {
|
|
||||||
symbol_table_builder: SymbolTableBuilder::new(),
|
|
||||||
flow_graph_builder: FlowGraphBuilder::new(),
|
|
||||||
scopes: vec![ScopeState {
|
|
||||||
scope_id: root_scope_id,
|
|
||||||
current_flow_node_id: FlowGraph::start(),
|
|
||||||
}],
|
|
||||||
expressions: FxHashMap::default(),
|
|
||||||
expressions_by_id: IndexVec::default(),
|
|
||||||
current_definition: None,
|
|
||||||
};
|
|
||||||
indexer.visit_body(&module.body);
|
|
||||||
indexer.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn resolve_expression_id<'a>(
|
|
||||||
&self,
|
|
||||||
ast: &'a ast::ModModule,
|
|
||||||
expression_id: ExpressionId,
|
|
||||||
) -> ast::AnyNodeRef<'a> {
|
|
||||||
let node_key = self.expressions_by_id[expression_id];
|
|
||||||
node_key
|
|
||||||
.resolve(ast.as_any_node_ref())
|
|
||||||
.expect("node to resolve")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return an iterator over all definitions of `symbol_id` reachable from `use_expr`. The value
|
|
||||||
/// of `symbol_id` in `use_expr` must originate from one of the iterated definitions (or from
|
|
||||||
/// an external reassignment of the name outside of this scope).
|
|
||||||
pub fn reachable_definitions(
|
|
||||||
&self,
|
|
||||||
symbol_id: SymbolId,
|
|
||||||
use_expr: &ast::Expr,
|
|
||||||
) -> ReachableDefinitionsIterator {
|
|
||||||
let expression_id = self.expression_id(use_expr);
|
|
||||||
ReachableDefinitionsIterator::new(
|
|
||||||
&self.flow_graph,
|
|
||||||
symbol_id,
|
|
||||||
self.flow_graph.for_expr(expression_id),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expression_id(&self, expression: &ast::Expr) -> ExpressionId {
|
|
||||||
self.expressions[&NodeKey::from_node(expression.into())]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn symbol_table(&self) -> &SymbolTable {
|
|
||||||
&self.symbol_table
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct ScopeState {
|
|
||||||
scope_id: ScopeId,
|
|
||||||
current_flow_node_id: FlowNodeId,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct SemanticIndexer {
|
|
||||||
symbol_table_builder: SymbolTableBuilder,
|
|
||||||
flow_graph_builder: FlowGraphBuilder,
|
|
||||||
scopes: Vec<ScopeState>,
|
|
||||||
/// the definition whose target(s) we are currently walking
|
|
||||||
current_definition: Option<Definition>,
|
|
||||||
expressions: FxHashMap<NodeKey, ExpressionId>,
|
|
||||||
expressions_by_id: IndexVec<ExpressionId, NodeKey>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SemanticIndexer {
|
|
||||||
pub(crate) fn finish(mut self) -> SemanticIndex {
|
|
||||||
let SemanticIndexer {
|
|
||||||
flow_graph_builder,
|
|
||||||
symbol_table_builder,
|
|
||||||
..
|
|
||||||
} = self;
|
|
||||||
self.expressions.shrink_to_fit();
|
|
||||||
self.expressions_by_id.shrink_to_fit();
|
|
||||||
SemanticIndex {
|
|
||||||
flow_graph: flow_graph_builder.finish(),
|
|
||||||
symbol_table: symbol_table_builder.finish(),
|
|
||||||
expressions: self.expressions,
|
|
||||||
expressions_by_id: self.expressions_by_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_current_flow_node(&mut self, new_flow_node_id: FlowNodeId) {
|
|
||||||
let scope_state = self.scopes.last_mut().expect("scope stack is never empty");
|
|
||||||
scope_state.current_flow_node_id = new_flow_node_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_flow_node(&self) -> FlowNodeId {
|
|
||||||
self.scopes
|
|
||||||
.last()
|
|
||||||
.expect("scope stack is never empty")
|
|
||||||
.current_flow_node_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_or_update_symbol(&mut self, identifier: &str, flags: SymbolFlags) -> SymbolId {
|
|
||||||
self.symbol_table_builder
|
|
||||||
.add_or_update_symbol(self.cur_scope(), identifier, flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_or_update_symbol_with_def(
|
|
||||||
&mut self,
|
|
||||||
identifier: &str,
|
|
||||||
definition: Definition,
|
|
||||||
) -> SymbolId {
|
|
||||||
let symbol_id = self.add_or_update_symbol(identifier, SymbolFlags::IS_DEFINED);
|
|
||||||
self.symbol_table_builder
|
|
||||||
.add_definition(symbol_id, definition.clone());
|
|
||||||
let new_flow_node_id =
|
|
||||||
self.flow_graph_builder
|
|
||||||
.add_definition(symbol_id, definition, self.current_flow_node());
|
|
||||||
self.set_current_flow_node(new_flow_node_id);
|
|
||||||
symbol_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn push_scope(
|
|
||||||
&mut self,
|
|
||||||
name: &str,
|
|
||||||
kind: ScopeKind,
|
|
||||||
definition: Option<Definition>,
|
|
||||||
defining_symbol: Option<SymbolId>,
|
|
||||||
) -> ScopeId {
|
|
||||||
let scope_id = self.symbol_table_builder.add_child_scope(
|
|
||||||
self.cur_scope(),
|
|
||||||
name,
|
|
||||||
kind,
|
|
||||||
definition,
|
|
||||||
defining_symbol,
|
|
||||||
);
|
|
||||||
self.scopes.push(ScopeState {
|
|
||||||
scope_id,
|
|
||||||
current_flow_node_id: FlowGraph::start(),
|
|
||||||
});
|
|
||||||
scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pop_scope(&mut self) -> ScopeId {
|
|
||||||
self.scopes
|
|
||||||
.pop()
|
|
||||||
.expect("Scope stack should never be empty")
|
|
||||||
.scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cur_scope(&self) -> ScopeId {
|
|
||||||
self.scopes
|
|
||||||
.last()
|
|
||||||
.expect("Scope stack should never be empty")
|
|
||||||
.scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn record_scope_for_node(&mut self, node_key: NodeKey, scope_id: ScopeId) {
|
|
||||||
self.symbol_table_builder
|
|
||||||
.record_scope_for_node(node_key, scope_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn insert_constraint(&mut self, expr: &ast::Expr) {
|
|
||||||
let node_key = NodeKey::from_node(expr.into());
|
|
||||||
let expression_id = self.expressions[&node_key];
|
|
||||||
let constraint = self
|
|
||||||
.flow_graph_builder
|
|
||||||
.add_constraint(self.current_flow_node(), expression_id);
|
|
||||||
self.set_current_flow_node(constraint);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_type_params(
|
|
||||||
&mut self,
|
|
||||||
name: &str,
|
|
||||||
params: &Option<Box<ast::TypeParams>>,
|
|
||||||
definition: Option<Definition>,
|
|
||||||
defining_symbol: Option<SymbolId>,
|
|
||||||
nested: impl FnOnce(&mut Self) -> ScopeId,
|
|
||||||
) -> ScopeId {
|
|
||||||
if let Some(type_params) = params {
|
|
||||||
self.push_scope(name, ScopeKind::Annotation, definition, defining_symbol);
|
|
||||||
for type_param in &type_params.type_params {
|
|
||||||
let name = match type_param {
|
|
||||||
ast::TypeParam::TypeVar(ast::TypeParamTypeVar { name, .. }) => name,
|
|
||||||
ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { name, .. }) => name,
|
|
||||||
ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { name, .. }) => name,
|
|
||||||
};
|
|
||||||
self.add_or_update_symbol(name, SymbolFlags::IS_DEFINED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let scope_id = nested(self);
|
|
||||||
if params.is_some() {
|
|
||||||
self.pop_scope();
|
|
||||||
}
|
|
||||||
scope_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceOrderVisitor<'_> for SemanticIndexer {
|
|
||||||
fn visit_expr(&mut self, expr: &ast::Expr) {
|
|
||||||
let node_key = NodeKey::from_node(expr.into());
|
|
||||||
let expression_id = self.expressions_by_id.push(node_key);
|
|
||||||
|
|
||||||
let flow_expression_id = self
|
|
||||||
.flow_graph_builder
|
|
||||||
.record_expr(self.current_flow_node());
|
|
||||||
debug_assert_eq!(expression_id, flow_expression_id);
|
|
||||||
|
|
||||||
let symbol_expression_id = self
|
|
||||||
.symbol_table_builder
|
|
||||||
.record_expression(self.cur_scope());
|
|
||||||
|
|
||||||
debug_assert_eq!(expression_id, symbol_expression_id);
|
|
||||||
|
|
||||||
self.expressions.insert(node_key, expression_id);
|
|
||||||
|
|
||||||
match expr {
|
|
||||||
ast::Expr::Name(ast::ExprName { id, ctx, .. }) => {
|
|
||||||
let flags = match ctx {
|
|
||||||
ast::ExprContext::Load => SymbolFlags::IS_USED,
|
|
||||||
ast::ExprContext::Store => SymbolFlags::IS_DEFINED,
|
|
||||||
ast::ExprContext::Del => SymbolFlags::IS_DEFINED,
|
|
||||||
ast::ExprContext::Invalid => SymbolFlags::empty(),
|
|
||||||
};
|
|
||||||
self.add_or_update_symbol(id, flags);
|
|
||||||
if flags.contains(SymbolFlags::IS_DEFINED) {
|
|
||||||
if let Some(curdef) = self.current_definition.clone() {
|
|
||||||
self.add_or_update_symbol_with_def(id, curdef);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast::visitor::source_order::walk_expr(self, expr);
|
|
||||||
}
|
|
||||||
ast::Expr::Named(node) => {
|
|
||||||
debug_assert!(self.current_definition.is_none());
|
|
||||||
self.current_definition =
|
|
||||||
Some(Definition::NamedExpr(TypedNodeKey::from_node(node)));
|
|
||||||
// TODO walrus in comprehensions is implicitly nonlocal
|
|
||||||
self.visit_expr(&node.target);
|
|
||||||
self.current_definition = None;
|
|
||||||
self.visit_expr(&node.value);
|
|
||||||
}
|
|
||||||
ast::Expr::If(ast::ExprIf {
|
|
||||||
body, test, orelse, ..
|
|
||||||
}) => {
|
|
||||||
// TODO detect statically known truthy or falsy test (via type inference, not naive
|
|
||||||
// AST inspection, so we can't simplify here, need to record test expression in CFG
|
|
||||||
// for later checking)
|
|
||||||
|
|
||||||
self.visit_expr(test);
|
|
||||||
|
|
||||||
let if_branch = self.flow_graph_builder.add_branch(self.current_flow_node());
|
|
||||||
|
|
||||||
self.set_current_flow_node(if_branch);
|
|
||||||
self.insert_constraint(test);
|
|
||||||
self.visit_expr(body);
|
|
||||||
|
|
||||||
let post_body = self.current_flow_node();
|
|
||||||
|
|
||||||
self.set_current_flow_node(if_branch);
|
|
||||||
self.visit_expr(orelse);
|
|
||||||
|
|
||||||
let post_else = self
|
|
||||||
.flow_graph_builder
|
|
||||||
.add_phi(self.current_flow_node(), post_body);
|
|
||||||
|
|
||||||
self.set_current_flow_node(post_else);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
ast::visitor::source_order::walk_expr(self, expr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
|
|
||||||
// TODO need to capture more definition statements here
|
|
||||||
match stmt {
|
|
||||||
ast::Stmt::ClassDef(node) => {
|
|
||||||
let node_key = TypedNodeKey::from_node(node);
|
|
||||||
let def = Definition::ClassDef(node_key.clone());
|
|
||||||
let symbol_id = self.add_or_update_symbol_with_def(&node.name, def.clone());
|
|
||||||
for decorator in &node.decorator_list {
|
|
||||||
self.visit_decorator(decorator);
|
|
||||||
}
|
|
||||||
let scope_id = self.with_type_params(
|
|
||||||
&node.name,
|
|
||||||
&node.type_params,
|
|
||||||
Some(def.clone()),
|
|
||||||
Some(symbol_id),
|
|
||||||
|indexer| {
|
|
||||||
if let Some(arguments) = &node.arguments {
|
|
||||||
indexer.visit_arguments(arguments);
|
|
||||||
}
|
|
||||||
let scope_id = indexer.push_scope(
|
|
||||||
&node.name,
|
|
||||||
ScopeKind::Class,
|
|
||||||
Some(def.clone()),
|
|
||||||
Some(symbol_id),
|
|
||||||
);
|
|
||||||
indexer.visit_body(&node.body);
|
|
||||||
indexer.pop_scope();
|
|
||||||
scope_id
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.record_scope_for_node(*node_key.erased(), scope_id);
|
|
||||||
}
|
|
||||||
ast::Stmt::FunctionDef(node) => {
|
|
||||||
let node_key = TypedNodeKey::from_node(node);
|
|
||||||
let def = Definition::FunctionDef(node_key.clone());
|
|
||||||
let symbol_id = self.add_or_update_symbol_with_def(&node.name, def.clone());
|
|
||||||
for decorator in &node.decorator_list {
|
|
||||||
self.visit_decorator(decorator);
|
|
||||||
}
|
|
||||||
let scope_id = self.with_type_params(
|
|
||||||
&node.name,
|
|
||||||
&node.type_params,
|
|
||||||
Some(def.clone()),
|
|
||||||
Some(symbol_id),
|
|
||||||
|indexer| {
|
|
||||||
indexer.visit_parameters(&node.parameters);
|
|
||||||
for expr in &node.returns {
|
|
||||||
indexer.visit_annotation(expr);
|
|
||||||
}
|
|
||||||
let scope_id = indexer.push_scope(
|
|
||||||
&node.name,
|
|
||||||
ScopeKind::Function,
|
|
||||||
Some(def.clone()),
|
|
||||||
Some(symbol_id),
|
|
||||||
);
|
|
||||||
indexer.visit_body(&node.body);
|
|
||||||
indexer.pop_scope();
|
|
||||||
scope_id
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.record_scope_for_node(*node_key.erased(), scope_id);
|
|
||||||
}
|
|
||||||
ast::Stmt::Import(ast::StmtImport { names, .. }) => {
|
|
||||||
for alias in names {
|
|
||||||
let symbol_name = if let Some(asname) = &alias.asname {
|
|
||||||
asname.id.as_str()
|
|
||||||
} else {
|
|
||||||
alias.name.id.split('.').next().unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
let module = ModuleName::new(&alias.name.id);
|
|
||||||
|
|
||||||
let def = Definition::Import(ImportDefinition {
|
|
||||||
module: module.clone(),
|
|
||||||
});
|
|
||||||
self.add_or_update_symbol_with_def(symbol_name, def);
|
|
||||||
self.symbol_table_builder
|
|
||||||
.add_dependency(Dependency::Module(module));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast::Stmt::ImportFrom(ast::StmtImportFrom {
|
|
||||||
module,
|
|
||||||
names,
|
|
||||||
level,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
let module = module.as_ref().map(|m| ModuleName::new(&m.id));
|
|
||||||
|
|
||||||
for alias in names {
|
|
||||||
let symbol_name = if let Some(asname) = &alias.asname {
|
|
||||||
asname.id.as_str()
|
|
||||||
} else {
|
|
||||||
alias.name.id.as_str()
|
|
||||||
};
|
|
||||||
let def = Definition::ImportFrom(ImportFromDefinition {
|
|
||||||
module: module.clone(),
|
|
||||||
name: Name::new(&alias.name.id),
|
|
||||||
level: *level,
|
|
||||||
});
|
|
||||||
self.add_or_update_symbol_with_def(symbol_name, def);
|
|
||||||
}
|
|
||||||
|
|
||||||
let dependency = if let Some(module) = module {
|
|
||||||
match NonZeroU32::new(*level) {
|
|
||||||
Some(level) => Dependency::Relative {
|
|
||||||
level,
|
|
||||||
module: Some(module),
|
|
||||||
},
|
|
||||||
None => Dependency::Module(module),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Dependency::Relative {
|
|
||||||
level: NonZeroU32::new(*level)
|
|
||||||
.expect("Import without a module to have a level > 0"),
|
|
||||||
module,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.symbol_table_builder.add_dependency(dependency);
|
|
||||||
}
|
|
||||||
ast::Stmt::Assign(node) => {
|
|
||||||
debug_assert!(self.current_definition.is_none());
|
|
||||||
self.visit_expr(&node.value);
|
|
||||||
self.current_definition =
|
|
||||||
Some(Definition::Assignment(TypedNodeKey::from_node(node)));
|
|
||||||
for expr in &node.targets {
|
|
||||||
self.visit_expr(expr);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.current_definition = None;
|
|
||||||
}
|
|
||||||
ast::Stmt::If(node) => {
|
|
||||||
// TODO detect statically known truthy or falsy test (via type inference, not naive
|
|
||||||
// AST inspection, so we can't simplify here, need to record test expression in CFG
|
|
||||||
// for later checking)
|
|
||||||
|
|
||||||
// we visit the if "test" condition first regardless
|
|
||||||
self.visit_expr(&node.test);
|
|
||||||
|
|
||||||
// create branch node: does the if test pass or not?
|
|
||||||
let if_branch = self.flow_graph_builder.add_branch(self.current_flow_node());
|
|
||||||
|
|
||||||
// visit the body of the `if` clause
|
|
||||||
self.set_current_flow_node(if_branch);
|
|
||||||
self.insert_constraint(&node.test);
|
|
||||||
self.visit_body(&node.body);
|
|
||||||
|
|
||||||
// Flow node for the last if/elif condition branch; represents the "no branch
|
|
||||||
// taken yet" possibility (where "taking a branch" means that the condition in an
|
|
||||||
// if or elif evaluated to true and control flow went into that clause).
|
|
||||||
let mut prior_branch = if_branch;
|
|
||||||
|
|
||||||
// Flow node for the state after the prior if/elif/else clause; represents "we have
|
|
||||||
// taken one of the branches up to this point." Initially set to the post-if-clause
|
|
||||||
// state, later will be set to the phi node joining that possible path with the
|
|
||||||
// possibility that we took a later if/elif/else clause instead.
|
|
||||||
let mut post_prior_clause = self.current_flow_node();
|
|
||||||
|
|
||||||
// Flag to mark if the final clause is an "else" -- if so, that means the "match no
|
|
||||||
// clauses" path is not possible, we have to go through one of the clauses.
|
|
||||||
let mut last_branch_is_else = false;
|
|
||||||
|
|
||||||
for clause in &node.elif_else_clauses {
|
|
||||||
if let Some(test) = &clause.test {
|
|
||||||
self.visit_expr(test);
|
|
||||||
// This is an elif clause. Create a new branch node. Its predecessor is the
|
|
||||||
// previous branch node, because we can only take one branch in an entire
|
|
||||||
// if/elif/else chain, so if we take this branch, it can only be because we
|
|
||||||
// didn't take the previous one.
|
|
||||||
prior_branch = self.flow_graph_builder.add_branch(prior_branch);
|
|
||||||
self.set_current_flow_node(prior_branch);
|
|
||||||
self.insert_constraint(test);
|
|
||||||
} else {
|
|
||||||
// This is an else clause. No need to create a branch node; there's no
|
|
||||||
// branch here, if we haven't taken any previous branch, we definitely go
|
|
||||||
// into the "else" clause.
|
|
||||||
self.set_current_flow_node(prior_branch);
|
|
||||||
last_branch_is_else = true;
|
|
||||||
}
|
|
||||||
self.visit_elif_else_clause(clause);
|
|
||||||
// Update `post_prior_clause` to a new phi node joining the possibility that we
|
|
||||||
// took any of the previous branches with the possibility that we took the one
|
|
||||||
// just visited.
|
|
||||||
post_prior_clause = self
|
|
||||||
.flow_graph_builder
|
|
||||||
.add_phi(self.current_flow_node(), post_prior_clause);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !last_branch_is_else {
|
|
||||||
// Final branch was not an "else", which means it's possible we took zero
|
|
||||||
// branches in the entire if/elif chain, so we need one more phi node to join
|
|
||||||
// the "no branches taken" possibility.
|
|
||||||
post_prior_clause = self
|
|
||||||
.flow_graph_builder
|
|
||||||
.add_phi(post_prior_clause, prior_branch);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Onward, with current flow node set to our final Phi node.
|
|
||||||
self.set_current_flow_node(post_prior_clause);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
ast::visitor::source_order::walk_stmt(self, stmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct SemanticIndexStorage(KeyValueCache<FileId, Arc<SemanticIndex>>);
|
|
||||||
|
|
||||||
impl Deref for SemanticIndexStorage {
|
|
||||||
type Target = KeyValueCache<FileId, Arc<SemanticIndex>>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for SemanticIndexStorage {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::semantic::symbol_table::{Symbol, SymbolIterator};
|
|
||||||
use ruff_python_ast as ast;
|
|
||||||
use ruff_python_ast::ModModule;
|
|
||||||
use ruff_python_parser::{Mode, Parsed};
|
|
||||||
|
|
||||||
use super::{Definition, ScopeKind, SemanticIndex, SymbolId};
|
|
||||||
|
|
||||||
fn parse(code: &str) -> Parsed<ModModule> {
|
|
||||||
ruff_python_parser::parse_unchecked(code, Mode::Module)
|
|
||||||
.try_into_module()
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn names<I>(it: SymbolIterator<I>) -> Vec<&str>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = SymbolId>,
|
|
||||||
{
|
|
||||||
let mut symbols: Vec<_> = it.map(Symbol::name).collect();
|
|
||||||
symbols.sort_unstable();
|
|
||||||
symbols
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn empty() {
|
|
||||||
let parsed = parse("");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()).len(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn simple() {
|
|
||||||
let parsed = parse("x");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["x"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("x").unwrap())
|
|
||||||
.len(),
|
|
||||||
0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn annotation_only() {
|
|
||||||
let parsed = parse("x: int");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["int", "x"]);
|
|
||||||
// TODO record definition
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn import() {
|
|
||||||
let parsed = parse("import foo");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["foo"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("foo").unwrap())
|
|
||||||
.len(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn import_sub() {
|
|
||||||
let parsed = parse("import foo.bar");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["foo"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn import_as() {
|
|
||||||
let parsed = parse("import foo.bar as baz");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["baz"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn import_from() {
|
|
||||||
let parsed = parse("from bar import foo");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["foo"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("foo").unwrap())
|
|
||||||
.len(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
table.root_symbol_id_by_name("foo").is_some_and(|sid| {
|
|
||||||
let s = sid.symbol(&table);
|
|
||||||
s.is_defined() || !s.is_used()
|
|
||||||
}),
|
|
||||||
"symbols that are defined get the defined flag"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn assign() {
|
|
||||||
let parsed = parse("x = foo");
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["foo", "x"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("x").unwrap())
|
|
||||||
.len(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
table.root_symbol_id_by_name("foo").is_some_and(|sid| {
|
|
||||||
let s = sid.symbol(&table);
|
|
||||||
!s.is_defined() && s.is_used()
|
|
||||||
}),
|
|
||||||
"a symbol used but not defined in a scope should have only the used flag"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn class_scope() {
|
|
||||||
let parsed = parse(
|
|
||||||
"
|
|
||||||
class C:
|
|
||||||
x = 1
|
|
||||||
y = 2
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["C", "y"]);
|
|
||||||
let scopes = table.root_child_scope_ids();
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let c_scope = scopes[0].scope(&table);
|
|
||||||
assert_eq!(c_scope.kind(), ScopeKind::Class);
|
|
||||||
assert_eq!(c_scope.name(), "C");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(scopes[0])), vec!["x"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("C").unwrap())
|
|
||||||
.len(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn func_scope() {
|
|
||||||
let parsed = parse(
|
|
||||||
"
|
|
||||||
def func():
|
|
||||||
x = 1
|
|
||||||
y = 2
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["func", "y"]);
|
|
||||||
let scopes = table.root_child_scope_ids();
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let func_scope = scopes[0].scope(&table);
|
|
||||||
assert_eq!(func_scope.kind(), ScopeKind::Function);
|
|
||||||
assert_eq!(func_scope.name(), "func");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(scopes[0])), vec!["x"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("func").unwrap())
|
|
||||||
.len(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dupes() {
|
|
||||||
let parsed = parse(
|
|
||||||
"
|
|
||||||
def func():
|
|
||||||
x = 1
|
|
||||||
def func():
|
|
||||||
y = 2
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["func"]);
|
|
||||||
let scopes = table.root_child_scope_ids();
|
|
||||||
assert_eq!(scopes.len(), 2);
|
|
||||||
let func_scope_1 = scopes[0].scope(&table);
|
|
||||||
let func_scope_2 = scopes[1].scope(&table);
|
|
||||||
assert_eq!(func_scope_1.kind(), ScopeKind::Function);
|
|
||||||
assert_eq!(func_scope_1.name(), "func");
|
|
||||||
assert_eq!(func_scope_2.kind(), ScopeKind::Function);
|
|
||||||
assert_eq!(func_scope_2.name(), "func");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(scopes[0])), vec!["x"]);
|
|
||||||
assert_eq!(names(table.symbols_for_scope(scopes[1])), vec!["y"]);
|
|
||||||
assert_eq!(
|
|
||||||
table
|
|
||||||
.definitions(table.root_symbol_id_by_name("func").unwrap())
|
|
||||||
.len(),
|
|
||||||
2
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn generic_func() {
|
|
||||||
let parsed = parse(
|
|
||||||
"
|
|
||||||
def func[T]():
|
|
||||||
x = 1
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["func"]);
|
|
||||||
let scopes = table.root_child_scope_ids();
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let ann_scope_id = scopes[0];
|
|
||||||
let ann_scope = ann_scope_id.scope(&table);
|
|
||||||
assert_eq!(ann_scope.kind(), ScopeKind::Annotation);
|
|
||||||
assert_eq!(ann_scope.name(), "func");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(ann_scope_id)), vec!["T"]);
|
|
||||||
let scopes = table.child_scope_ids_of(ann_scope_id);
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let func_scope_id = scopes[0];
|
|
||||||
let func_scope = func_scope_id.scope(&table);
|
|
||||||
assert_eq!(func_scope.kind(), ScopeKind::Function);
|
|
||||||
assert_eq!(func_scope.name(), "func");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(func_scope_id)), vec!["x"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn generic_class() {
|
|
||||||
let parsed = parse(
|
|
||||||
"
|
|
||||||
class C[T]:
|
|
||||||
x = 1
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let table = SemanticIndex::from_ast(parsed.syntax()).symbol_table;
|
|
||||||
assert_eq!(names(table.root_symbols()), vec!["C"]);
|
|
||||||
let scopes = table.root_child_scope_ids();
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let ann_scope_id = scopes[0];
|
|
||||||
let ann_scope = ann_scope_id.scope(&table);
|
|
||||||
assert_eq!(ann_scope.kind(), ScopeKind::Annotation);
|
|
||||||
assert_eq!(ann_scope.name(), "C");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(ann_scope_id)), vec!["T"]);
|
|
||||||
assert!(
|
|
||||||
table
|
|
||||||
.symbol_by_name(ann_scope_id, "T")
|
|
||||||
.is_some_and(|s| s.is_defined() && !s.is_used()),
|
|
||||||
"type parameters are defined by the scope that introduces them"
|
|
||||||
);
|
|
||||||
let scopes = table.child_scope_ids_of(ann_scope_id);
|
|
||||||
assert_eq!(scopes.len(), 1);
|
|
||||||
let func_scope_id = scopes[0];
|
|
||||||
let func_scope = func_scope_id.scope(&table);
|
|
||||||
assert_eq!(func_scope.kind(), ScopeKind::Class);
|
|
||||||
assert_eq!(func_scope.name(), "C");
|
|
||||||
assert_eq!(names(table.symbols_for_scope(func_scope_id)), vec!["x"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn reachability_trivial() {
|
|
||||||
let parsed = parse("x = 1; x");
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let index = SemanticIndex::from_ast(ast);
|
|
||||||
let table = &index.symbol_table;
|
|
||||||
let x_sym = table
|
|
||||||
.root_symbol_id_by_name("x")
|
|
||||||
.expect("x symbol should exist");
|
|
||||||
let ast::Stmt::Expr(ast::StmtExpr { value: x_use, .. }) = &ast.body[1] else {
|
|
||||||
panic!("should be an expr")
|
|
||||||
};
|
|
||||||
let x_defs: Vec<_> = index
|
|
||||||
.reachable_definitions(x_sym, x_use)
|
|
||||||
.map(|constrained_definition| constrained_definition.definition)
|
|
||||||
.collect();
|
|
||||||
assert_eq!(x_defs.len(), 1);
|
|
||||||
let Definition::Assignment(node_key) = &x_defs[0] else {
|
|
||||||
panic!("def should be an assignment")
|
|
||||||
};
|
|
||||||
let Some(def_node) = node_key.resolve(ast.into()) else {
|
|
||||||
panic!("node key should resolve")
|
|
||||||
};
|
|
||||||
let ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
|
|
||||||
value: ast::Number::Int(num),
|
|
||||||
..
|
|
||||||
}) = &*def_node.value
|
|
||||||
else {
|
|
||||||
panic!("should be a number literal")
|
|
||||||
};
|
|
||||||
assert_eq!(*num, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn expression_scope() {
|
|
||||||
let parsed = parse("x = 1;\ndef test():\n y = 4");
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let index = SemanticIndex::from_ast(ast);
|
|
||||||
let table = &index.symbol_table;
|
|
||||||
|
|
||||||
let x_sym = table
|
|
||||||
.root_symbol_by_name("x")
|
|
||||||
.expect("x symbol should exist");
|
|
||||||
|
|
||||||
let x_stmt = ast.body[0].as_assign_stmt().unwrap();
|
|
||||||
|
|
||||||
let x_id = index.expression_id(&x_stmt.targets[0]);
|
|
||||||
|
|
||||||
assert_eq!(table.scope_of_expression(x_id).kind(), ScopeKind::Module);
|
|
||||||
assert_eq!(table.scope_id_of_expression(x_id), x_sym.scope_id());
|
|
||||||
|
|
||||||
let def = ast.body[1].as_function_def_stmt().unwrap();
|
|
||||||
let y_stmt = def.body[0].as_assign_stmt().unwrap();
|
|
||||||
let y_id = index.expression_id(&y_stmt.targets[0]);
|
|
||||||
|
|
||||||
assert_eq!(table.scope_of_expression(y_id).kind(), ScopeKind::Function);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
use crate::ast_ids::TypedNodeKey;
|
|
||||||
use crate::semantic::ModuleName;
|
|
||||||
use crate::Name;
|
|
||||||
use ruff_python_ast as ast;
|
|
||||||
|
|
||||||
// TODO storing TypedNodeKey for definitions means we have to search to find them again in the AST;
|
|
||||||
// this is at best O(log n). If looking up definitions is a bottleneck we should look for
|
|
||||||
// alternatives here.
|
|
||||||
// TODO intern Definitions in SymbolTable and reference using IDs?
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum Definition {
|
|
||||||
// For the import cases, we don't need reference to any arbitrary AST subtrees (annotations,
|
|
||||||
// RHS), and referencing just the import statement node is imprecise (a single import statement
|
|
||||||
// can assign many symbols, we'd have to re-search for the one we care about), so we just copy
|
|
||||||
// the small amount of information we need from the AST.
|
|
||||||
Import(ImportDefinition),
|
|
||||||
ImportFrom(ImportFromDefinition),
|
|
||||||
ClassDef(TypedNodeKey<ast::StmtClassDef>),
|
|
||||||
FunctionDef(TypedNodeKey<ast::StmtFunctionDef>),
|
|
||||||
Assignment(TypedNodeKey<ast::StmtAssign>),
|
|
||||||
AnnotatedAssignment(TypedNodeKey<ast::StmtAnnAssign>),
|
|
||||||
NamedExpr(TypedNodeKey<ast::ExprNamed>),
|
|
||||||
/// represents the implicit initial definition of every name as "unbound"
|
|
||||||
Unbound,
|
|
||||||
// TODO with statements, except handlers, function args...
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct ImportDefinition {
|
|
||||||
pub module: ModuleName,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct ImportFromDefinition {
|
|
||||||
pub module: Option<ModuleName>,
|
|
||||||
pub name: Name,
|
|
||||||
pub level: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ImportFromDefinition {
|
|
||||||
pub fn module(&self) -> Option<&ModuleName> {
|
|
||||||
self.module.as_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
|
||||||
&self.name
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn level(&self) -> u32 {
|
|
||||||
self.level
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,270 +0,0 @@
|
|||||||
use super::symbol_table::SymbolId;
|
|
||||||
use crate::semantic::{Definition, ExpressionId};
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
|
||||||
use std::iter::FusedIterator;
|
|
||||||
use std::ops::Range;
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct FlowNodeId;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) enum FlowNode {
|
|
||||||
Start,
|
|
||||||
Definition(DefinitionFlowNode),
|
|
||||||
Branch(BranchFlowNode),
|
|
||||||
Phi(PhiFlowNode),
|
|
||||||
Constraint(ConstraintFlowNode),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A point in control flow where a symbol is defined
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct DefinitionFlowNode {
|
|
||||||
symbol_id: SymbolId,
|
|
||||||
definition: Definition,
|
|
||||||
predecessor: FlowNodeId,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A branch in control flow
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct BranchFlowNode {
|
|
||||||
predecessor: FlowNodeId,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A join point where control flow paths come together
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct PhiFlowNode {
|
|
||||||
first_predecessor: FlowNodeId,
|
|
||||||
second_predecessor: FlowNodeId,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A branch test which may apply constraints to a symbol's type
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct ConstraintFlowNode {
|
|
||||||
predecessor: FlowNodeId,
|
|
||||||
test_expression: ExpressionId,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FlowGraph {
|
|
||||||
flow_nodes_by_id: IndexVec<FlowNodeId, FlowNode>,
|
|
||||||
expression_map: IndexVec<ExpressionId, FlowNodeId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FlowGraph {
|
|
||||||
pub fn start() -> FlowNodeId {
|
|
||||||
FlowNodeId::from_usize(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn for_expr(&self, expr: ExpressionId) -> FlowNodeId {
|
|
||||||
self.expression_map[expr]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct FlowGraphBuilder {
|
|
||||||
flow_graph: FlowGraph,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FlowGraphBuilder {
|
|
||||||
pub(crate) fn new() -> Self {
|
|
||||||
let mut graph = FlowGraph {
|
|
||||||
flow_nodes_by_id: IndexVec::default(),
|
|
||||||
expression_map: IndexVec::default(),
|
|
||||||
};
|
|
||||||
graph.flow_nodes_by_id.push(FlowNode::Start);
|
|
||||||
Self { flow_graph: graph }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add(&mut self, node: FlowNode) -> FlowNodeId {
|
|
||||||
self.flow_graph.flow_nodes_by_id.push(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_definition(
|
|
||||||
&mut self,
|
|
||||||
symbol_id: SymbolId,
|
|
||||||
definition: Definition,
|
|
||||||
predecessor: FlowNodeId,
|
|
||||||
) -> FlowNodeId {
|
|
||||||
self.add(FlowNode::Definition(DefinitionFlowNode {
|
|
||||||
symbol_id,
|
|
||||||
definition,
|
|
||||||
predecessor,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_branch(&mut self, predecessor: FlowNodeId) -> FlowNodeId {
|
|
||||||
self.add(FlowNode::Branch(BranchFlowNode { predecessor }))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_phi(
|
|
||||||
&mut self,
|
|
||||||
first_predecessor: FlowNodeId,
|
|
||||||
second_predecessor: FlowNodeId,
|
|
||||||
) -> FlowNodeId {
|
|
||||||
self.add(FlowNode::Phi(PhiFlowNode {
|
|
||||||
first_predecessor,
|
|
||||||
second_predecessor,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_constraint(
|
|
||||||
&mut self,
|
|
||||||
predecessor: FlowNodeId,
|
|
||||||
test_expression: ExpressionId,
|
|
||||||
) -> FlowNodeId {
|
|
||||||
self.add(FlowNode::Constraint(ConstraintFlowNode {
|
|
||||||
predecessor,
|
|
||||||
test_expression,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn record_expr(&mut self, node_id: FlowNodeId) -> ExpressionId {
|
|
||||||
self.flow_graph.expression_map.push(node_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn finish(mut self) -> FlowGraph {
|
|
||||||
self.flow_graph.flow_nodes_by_id.shrink_to_fit();
|
|
||||||
self.flow_graph.expression_map.shrink_to_fit();
|
|
||||||
self.flow_graph
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A definition, and the set of constraints between a use and the definition
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct ConstrainedDefinition {
|
|
||||||
pub definition: Definition,
|
|
||||||
pub constraints: Vec<ExpressionId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A flow node and the constraints we passed through to reach it
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct FlowState {
|
|
||||||
node_id: FlowNodeId,
|
|
||||||
constraints_range: Range<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ReachableDefinitionsIterator<'a> {
|
|
||||||
flow_graph: &'a FlowGraph,
|
|
||||||
symbol_id: SymbolId,
|
|
||||||
pending: Vec<FlowState>,
|
|
||||||
constraints: Vec<ExpressionId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> ReachableDefinitionsIterator<'a> {
|
|
||||||
pub fn new(flow_graph: &'a FlowGraph, symbol_id: SymbolId, start_node_id: FlowNodeId) -> Self {
|
|
||||||
Self {
|
|
||||||
flow_graph,
|
|
||||||
symbol_id,
|
|
||||||
pending: vec![FlowState {
|
|
||||||
node_id: start_node_id,
|
|
||||||
constraints_range: 0..0,
|
|
||||||
}],
|
|
||||||
constraints: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for ReachableDefinitionsIterator<'a> {
|
|
||||||
type Item = ConstrainedDefinition;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let FlowState {
|
|
||||||
mut node_id,
|
|
||||||
mut constraints_range,
|
|
||||||
} = self.pending.pop()?;
|
|
||||||
self.constraints.truncate(constraints_range.end + 1);
|
|
||||||
loop {
|
|
||||||
match &self.flow_graph.flow_nodes_by_id[node_id] {
|
|
||||||
FlowNode::Start => {
|
|
||||||
// constraints on unbound are irrelevant
|
|
||||||
return Some(ConstrainedDefinition {
|
|
||||||
definition: Definition::Unbound,
|
|
||||||
constraints: vec![],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
FlowNode::Definition(def_node) => {
|
|
||||||
if def_node.symbol_id == self.symbol_id {
|
|
||||||
return Some(ConstrainedDefinition {
|
|
||||||
definition: def_node.definition.clone(),
|
|
||||||
constraints: self.constraints[constraints_range].to_vec(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
node_id = def_node.predecessor;
|
|
||||||
}
|
|
||||||
FlowNode::Branch(branch_node) => {
|
|
||||||
node_id = branch_node.predecessor;
|
|
||||||
}
|
|
||||||
FlowNode::Phi(phi_node) => {
|
|
||||||
self.pending.push(FlowState {
|
|
||||||
node_id: phi_node.first_predecessor,
|
|
||||||
constraints_range: constraints_range.clone(),
|
|
||||||
});
|
|
||||||
node_id = phi_node.second_predecessor;
|
|
||||||
}
|
|
||||||
FlowNode::Constraint(constraint_node) => {
|
|
||||||
node_id = constraint_node.predecessor;
|
|
||||||
self.constraints.push(constraint_node.test_expression);
|
|
||||||
constraints_range.end += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> FusedIterator for ReachableDefinitionsIterator<'a> {}
|
|
||||||
|
|
||||||
impl std::fmt::Display for FlowGraph {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
writeln!(f, "flowchart TD")?;
|
|
||||||
for (id, node) in self.flow_nodes_by_id.iter_enumerated() {
|
|
||||||
write!(f, " id{}", id.as_u32())?;
|
|
||||||
match node {
|
|
||||||
FlowNode::Start => writeln!(f, r"[\Start/]")?,
|
|
||||||
FlowNode::Definition(def_node) => {
|
|
||||||
writeln!(f, r"(Define symbol {})", def_node.symbol_id.as_u32())?;
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
r" id{}-->id{}",
|
|
||||||
def_node.predecessor.as_u32(),
|
|
||||||
id.as_u32()
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
FlowNode::Branch(branch_node) => {
|
|
||||||
writeln!(f, r"{{Branch}}")?;
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
r" id{}-->id{}",
|
|
||||||
branch_node.predecessor.as_u32(),
|
|
||||||
id.as_u32()
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
FlowNode::Phi(phi_node) => {
|
|
||||||
writeln!(f, r"((Phi))")?;
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
r" id{}-->id{}",
|
|
||||||
phi_node.second_predecessor.as_u32(),
|
|
||||||
id.as_u32()
|
|
||||||
)?;
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
r" id{}-->id{}",
|
|
||||||
phi_node.first_predecessor.as_u32(),
|
|
||||||
id.as_u32()
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
FlowNode::Constraint(constraint_node) => {
|
|
||||||
writeln!(f, r"((Constraint))")?;
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
r" id{}-->id{}",
|
|
||||||
constraint_node.predecessor.as_u32(),
|
|
||||||
id.as_u32()
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,560 +0,0 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::iter::{Copied, DoubleEndedIterator, FusedIterator};
|
|
||||||
use std::num::NonZeroU32;
|
|
||||||
|
|
||||||
use bitflags::bitflags;
|
|
||||||
use hashbrown::hash_map::{Keys, RawEntryMut};
|
|
||||||
use rustc_hash::{FxHashMap, FxHasher};
|
|
||||||
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
|
||||||
|
|
||||||
use crate::ast_ids::NodeKey;
|
|
||||||
use crate::module::ModuleName;
|
|
||||||
use crate::semantic::{Definition, ExpressionId};
|
|
||||||
use crate::Name;
|
|
||||||
|
|
||||||
type Map<K, V> = hashbrown::HashMap<K, V, ()>;
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct ScopeId;
|
|
||||||
|
|
||||||
impl ScopeId {
|
|
||||||
pub fn scope(self, table: &SymbolTable) -> &Scope {
|
|
||||||
&table.scopes_by_id[self]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[newtype_index]
|
|
||||||
pub struct SymbolId;
|
|
||||||
|
|
||||||
impl SymbolId {
|
|
||||||
pub fn symbol(self, table: &SymbolTable) -> &Symbol {
|
|
||||||
&table.symbols_by_id[self]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
||||||
pub enum ScopeKind {
|
|
||||||
Module,
|
|
||||||
Annotation,
|
|
||||||
Class,
|
|
||||||
Function,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Scope {
|
|
||||||
name: Name,
|
|
||||||
kind: ScopeKind,
|
|
||||||
parent: Option<ScopeId>,
|
|
||||||
children: Vec<ScopeId>,
|
|
||||||
/// the definition (e.g. class or function) that created this scope
|
|
||||||
definition: Option<Definition>,
|
|
||||||
/// the symbol (e.g. class or function) that owns this scope
|
|
||||||
defining_symbol: Option<SymbolId>,
|
|
||||||
/// symbol IDs, hashed by symbol name
|
|
||||||
symbols_by_name: Map<SymbolId, ()>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scope {
|
|
||||||
pub fn name(&self) -> &str {
|
|
||||||
self.name.as_str()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn kind(&self) -> ScopeKind {
|
|
||||||
self.kind
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn definition(&self) -> Option<Definition> {
|
|
||||||
self.definition.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn defining_symbol(&self) -> Option<SymbolId> {
|
|
||||||
self.defining_symbol
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) enum Kind {
|
|
||||||
FreeVar,
|
|
||||||
CellVar,
|
|
||||||
CellVarAssigned,
|
|
||||||
ExplicitGlobal,
|
|
||||||
ImplicitGlobal,
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags! {
|
|
||||||
#[derive(Copy,Clone,Debug)]
|
|
||||||
pub struct SymbolFlags: u8 {
|
|
||||||
const IS_USED = 1 << 0;
|
|
||||||
const IS_DEFINED = 1 << 1;
|
|
||||||
/// TODO: This flag is not yet set by anything
|
|
||||||
const MARKED_GLOBAL = 1 << 2;
|
|
||||||
/// TODO: This flag is not yet set by anything
|
|
||||||
const MARKED_NONLOCAL = 1 << 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Symbol {
|
|
||||||
name: Name,
|
|
||||||
flags: SymbolFlags,
|
|
||||||
scope_id: ScopeId,
|
|
||||||
// kind: Kind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Symbol {
|
|
||||||
pub fn name(&self) -> &str {
|
|
||||||
self.name.as_str()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_id(&self) -> ScopeId {
|
|
||||||
self.scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Is the symbol used in its containing scope?
|
|
||||||
pub fn is_used(&self) -> bool {
|
|
||||||
self.flags.contains(SymbolFlags::IS_USED)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Is the symbol defined in its containing scope?
|
|
||||||
pub fn is_defined(&self) -> bool {
|
|
||||||
self.flags.contains(SymbolFlags::IS_DEFINED)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: implement Symbol.kind 2-pass analysis to categorize as: free-var, cell-var,
|
|
||||||
// explicit-global, implicit-global and implement Symbol.kind by modifying the preorder
|
|
||||||
// traversal code
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Dependency {
|
|
||||||
Module(ModuleName),
|
|
||||||
Relative {
|
|
||||||
level: NonZeroU32,
|
|
||||||
module: Option<ModuleName>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Table of all symbols in all scopes for a module.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct SymbolTable {
|
|
||||||
scopes_by_id: IndexVec<ScopeId, Scope>,
|
|
||||||
symbols_by_id: IndexVec<SymbolId, Symbol>,
|
|
||||||
/// the definitions for each symbol
|
|
||||||
defs: FxHashMap<SymbolId, Vec<Definition>>,
|
|
||||||
/// map of AST node (e.g. class/function def) to sub-scope it creates
|
|
||||||
scopes_by_node: FxHashMap<NodeKey, ScopeId>,
|
|
||||||
/// Maps expressions to their enclosing scope.
|
|
||||||
expression_scopes: IndexVec<ExpressionId, ScopeId>,
|
|
||||||
/// dependencies of this module
|
|
||||||
dependencies: Vec<Dependency>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SymbolTable {
|
|
||||||
pub fn dependencies(&self) -> &[Dependency] {
|
|
||||||
&self.dependencies
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn root_scope_id() -> ScopeId {
|
|
||||||
ScopeId::from_usize(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_scope(&self) -> &Scope {
|
|
||||||
&self.scopes_by_id[SymbolTable::root_scope_id()]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn symbol_ids_for_scope(&self, scope_id: ScopeId) -> Copied<Keys<SymbolId, ()>> {
|
|
||||||
self.scopes_by_id[scope_id].symbols_by_name.keys().copied()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn symbols_for_scope(
|
|
||||||
&self,
|
|
||||||
scope_id: ScopeId,
|
|
||||||
) -> SymbolIterator<Copied<Keys<SymbolId, ()>>> {
|
|
||||||
SymbolIterator {
|
|
||||||
table: self,
|
|
||||||
ids: self.symbol_ids_for_scope(scope_id),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_symbol_ids(&self) -> Copied<Keys<SymbolId, ()>> {
|
|
||||||
self.symbol_ids_for_scope(SymbolTable::root_scope_id())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_symbols(&self) -> SymbolIterator<Copied<Keys<SymbolId, ()>>> {
|
|
||||||
self.symbols_for_scope(SymbolTable::root_scope_id())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn child_scope_ids_of(&self, scope_id: ScopeId) -> &[ScopeId] {
|
|
||||||
&self.scopes_by_id[scope_id].children
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn child_scopes_of(&self, scope_id: ScopeId) -> ScopeIterator<&[ScopeId]> {
|
|
||||||
ScopeIterator {
|
|
||||||
table: self,
|
|
||||||
ids: self.child_scope_ids_of(scope_id),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_child_scope_ids(&self) -> &[ScopeId] {
|
|
||||||
self.child_scope_ids_of(SymbolTable::root_scope_id())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_child_scopes(&self) -> ScopeIterator<&[ScopeId]> {
|
|
||||||
self.child_scopes_of(SymbolTable::root_scope_id())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn symbol_id_by_name(&self, scope_id: ScopeId, name: &str) -> Option<SymbolId> {
|
|
||||||
let scope = &self.scopes_by_id[scope_id];
|
|
||||||
let hash = SymbolTable::hash_name(name);
|
|
||||||
let name = Name::new(name);
|
|
||||||
Some(
|
|
||||||
*scope
|
|
||||||
.symbols_by_name
|
|
||||||
.raw_entry()
|
|
||||||
.from_hash(hash, |symid| self.symbols_by_id[*symid].name == name)?
|
|
||||||
.0,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn symbol_by_name(&self, scope_id: ScopeId, name: &str) -> Option<&Symbol> {
|
|
||||||
Some(&self.symbols_by_id[self.symbol_id_by_name(scope_id, name)?])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_symbol_id_by_name(&self, name: &str) -> Option<SymbolId> {
|
|
||||||
self.symbol_id_by_name(SymbolTable::root_scope_id(), name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_symbol_by_name(&self, name: &str) -> Option<&Symbol> {
|
|
||||||
self.symbol_by_name(SymbolTable::root_scope_id(), name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_id_of_symbol(&self, symbol_id: SymbolId) -> ScopeId {
|
|
||||||
self.symbols_by_id[symbol_id].scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_of_symbol(&self, symbol_id: SymbolId) -> &Scope {
|
|
||||||
&self.scopes_by_id[self.scope_id_of_symbol(symbol_id)]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_id_of_expression(&self, expression: ExpressionId) -> ScopeId {
|
|
||||||
self.expression_scopes[expression]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_of_expression(&self, expr_id: ExpressionId) -> &Scope {
|
|
||||||
&self.scopes_by_id[self.scope_id_of_expression(expr_id)]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parent_scopes(
|
|
||||||
&self,
|
|
||||||
scope_id: ScopeId,
|
|
||||||
) -> ScopeIterator<impl Iterator<Item = ScopeId> + '_> {
|
|
||||||
ScopeIterator {
|
|
||||||
table: self,
|
|
||||||
ids: std::iter::successors(Some(scope_id), |scope| self.scopes_by_id[*scope].parent),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parent_scope(&self, scope_id: ScopeId) -> Option<ScopeId> {
|
|
||||||
self.scopes_by_id[scope_id].parent
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scope_id_for_node(&self, node_key: &NodeKey) -> ScopeId {
|
|
||||||
self.scopes_by_node[node_key]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn definitions(&self, symbol_id: SymbolId) -> &[Definition] {
|
|
||||||
self.defs
|
|
||||||
.get(&symbol_id)
|
|
||||||
.map(std::vec::Vec::as_slice)
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn all_definitions(&self) -> impl Iterator<Item = (SymbolId, &Definition)> + '_ {
|
|
||||||
self.defs
|
|
||||||
.iter()
|
|
||||||
.flat_map(|(sym_id, defs)| defs.iter().map(move |def| (*sym_id, def)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash_name(name: &str) -> u64 {
|
|
||||||
let mut hasher = FxHasher::default();
|
|
||||||
name.hash(&mut hasher);
|
|
||||||
hasher.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SymbolIterator<'a, I> {
|
|
||||||
table: &'a SymbolTable,
|
|
||||||
ids: I,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, I> Iterator for SymbolIterator<'a, I>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = SymbolId>,
|
|
||||||
{
|
|
||||||
type Item = &'a Symbol;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let id = self.ids.next()?;
|
|
||||||
Some(&self.table.symbols_by_id[id])
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
||||||
self.ids.size_hint()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, I> FusedIterator for SymbolIterator<'a, I> where
|
|
||||||
I: Iterator<Item = SymbolId> + FusedIterator
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, I> DoubleEndedIterator for SymbolIterator<'a, I>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = SymbolId> + DoubleEndedIterator,
|
|
||||||
{
|
|
||||||
fn next_back(&mut self) -> Option<Self::Item> {
|
|
||||||
let id = self.ids.next_back()?;
|
|
||||||
Some(&self.table.symbols_by_id[id])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO maybe get rid of this and just do all data access via methods on ScopeId?
|
|
||||||
pub struct ScopeIterator<'a, I> {
|
|
||||||
table: &'a SymbolTable,
|
|
||||||
ids: I,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// iterate (`ScopeId`, `Scope`) pairs for given `ScopeId` iterator
|
|
||||||
impl<'a, I> Iterator for ScopeIterator<'a, I>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = ScopeId>,
|
|
||||||
{
|
|
||||||
type Item = (ScopeId, &'a Scope);
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let id = self.ids.next()?;
|
|
||||||
Some((id, &self.table.scopes_by_id[id]))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
||||||
self.ids.size_hint()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, I> FusedIterator for ScopeIterator<'a, I> where I: Iterator<Item = ScopeId> + FusedIterator {}
|
|
||||||
|
|
||||||
impl<'a, I> DoubleEndedIterator for ScopeIterator<'a, I>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = ScopeId> + DoubleEndedIterator,
|
|
||||||
{
|
|
||||||
fn next_back(&mut self) -> Option<Self::Item> {
|
|
||||||
let id = self.ids.next_back()?;
|
|
||||||
Some((id, &self.table.scopes_by_id[id]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(super) struct SymbolTableBuilder {
|
|
||||||
symbol_table: SymbolTable,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SymbolTableBuilder {
|
|
||||||
pub(super) fn new() -> Self {
|
|
||||||
let mut table = SymbolTable {
|
|
||||||
scopes_by_id: IndexVec::new(),
|
|
||||||
symbols_by_id: IndexVec::new(),
|
|
||||||
defs: FxHashMap::default(),
|
|
||||||
scopes_by_node: FxHashMap::default(),
|
|
||||||
expression_scopes: IndexVec::new(),
|
|
||||||
dependencies: Vec::new(),
|
|
||||||
};
|
|
||||||
table.scopes_by_id.push(Scope {
|
|
||||||
name: Name::new("<module>"),
|
|
||||||
kind: ScopeKind::Module,
|
|
||||||
parent: None,
|
|
||||||
children: Vec::new(),
|
|
||||||
definition: None,
|
|
||||||
defining_symbol: None,
|
|
||||||
symbols_by_name: Map::default(),
|
|
||||||
});
|
|
||||||
Self {
|
|
||||||
symbol_table: table,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn finish(self) -> SymbolTable {
|
|
||||||
let mut symbol_table = self.symbol_table;
|
|
||||||
symbol_table.scopes_by_id.shrink_to_fit();
|
|
||||||
symbol_table.symbols_by_id.shrink_to_fit();
|
|
||||||
symbol_table.defs.shrink_to_fit();
|
|
||||||
symbol_table.scopes_by_node.shrink_to_fit();
|
|
||||||
symbol_table.expression_scopes.shrink_to_fit();
|
|
||||||
symbol_table.dependencies.shrink_to_fit();
|
|
||||||
symbol_table
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn add_or_update_symbol(
|
|
||||||
&mut self,
|
|
||||||
scope_id: ScopeId,
|
|
||||||
name: &str,
|
|
||||||
flags: SymbolFlags,
|
|
||||||
) -> SymbolId {
|
|
||||||
let hash = SymbolTable::hash_name(name);
|
|
||||||
let scope = &mut self.symbol_table.scopes_by_id[scope_id];
|
|
||||||
let name = Name::new(name);
|
|
||||||
|
|
||||||
let entry = scope
|
|
||||||
.symbols_by_name
|
|
||||||
.raw_entry_mut()
|
|
||||||
.from_hash(hash, |existing| {
|
|
||||||
self.symbol_table.symbols_by_id[*existing].name == name
|
|
||||||
});
|
|
||||||
|
|
||||||
match entry {
|
|
||||||
RawEntryMut::Occupied(entry) => {
|
|
||||||
if let Some(symbol) = self.symbol_table.symbols_by_id.get_mut(*entry.key()) {
|
|
||||||
symbol.flags.insert(flags);
|
|
||||||
};
|
|
||||||
*entry.key()
|
|
||||||
}
|
|
||||||
RawEntryMut::Vacant(entry) => {
|
|
||||||
let id = self.symbol_table.symbols_by_id.push(Symbol {
|
|
||||||
name,
|
|
||||||
flags,
|
|
||||||
scope_id,
|
|
||||||
});
|
|
||||||
entry.insert_with_hasher(hash, id, (), |symid| {
|
|
||||||
SymbolTable::hash_name(&self.symbol_table.symbols_by_id[*symid].name)
|
|
||||||
});
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn add_definition(&mut self, symbol_id: SymbolId, definition: Definition) {
|
|
||||||
self.symbol_table
|
|
||||||
.defs
|
|
||||||
.entry(symbol_id)
|
|
||||||
.or_default()
|
|
||||||
.push(definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn add_child_scope(
|
|
||||||
&mut self,
|
|
||||||
parent_scope_id: ScopeId,
|
|
||||||
name: &str,
|
|
||||||
kind: ScopeKind,
|
|
||||||
definition: Option<Definition>,
|
|
||||||
defining_symbol: Option<SymbolId>,
|
|
||||||
) -> ScopeId {
|
|
||||||
let new_scope_id = self.symbol_table.scopes_by_id.push(Scope {
|
|
||||||
name: Name::new(name),
|
|
||||||
kind,
|
|
||||||
parent: Some(parent_scope_id),
|
|
||||||
children: Vec::new(),
|
|
||||||
definition,
|
|
||||||
defining_symbol,
|
|
||||||
symbols_by_name: Map::default(),
|
|
||||||
});
|
|
||||||
let parent_scope = &mut self.symbol_table.scopes_by_id[parent_scope_id];
|
|
||||||
parent_scope.children.push(new_scope_id);
|
|
||||||
new_scope_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn record_scope_for_node(&mut self, node_key: NodeKey, scope_id: ScopeId) {
|
|
||||||
self.symbol_table.scopes_by_node.insert(node_key, scope_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn add_dependency(&mut self, dependency: Dependency) {
|
|
||||||
self.symbol_table.dependencies.push(dependency);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Records the scope for the current expression
|
|
||||||
pub(super) fn record_expression(&mut self, scope: ScopeId) -> ExpressionId {
|
|
||||||
self.symbol_table.expression_scopes.push(scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::{ScopeKind, SymbolFlags, SymbolTable, SymbolTableBuilder};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn insert_same_name_symbol_twice() {
|
|
||||||
let mut builder = SymbolTableBuilder::new();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let symbol_id_1 =
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::IS_DEFINED);
|
|
||||||
let symbol_id_2 = builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::IS_USED);
|
|
||||||
let table = builder.finish();
|
|
||||||
|
|
||||||
assert_eq!(symbol_id_1, symbol_id_2);
|
|
||||||
assert!(symbol_id_1.symbol(&table).is_used(), "flags must merge");
|
|
||||||
assert!(symbol_id_1.symbol(&table).is_defined(), "flags must merge");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn insert_different_named_symbols() {
|
|
||||||
let mut builder = SymbolTableBuilder::new();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let symbol_id_1 = builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::empty());
|
|
||||||
let symbol_id_2 = builder.add_or_update_symbol(root_scope_id, "bar", SymbolFlags::empty());
|
|
||||||
|
|
||||||
assert_ne!(symbol_id_1, symbol_id_2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add_child_scope_with_symbol() {
|
|
||||||
let mut builder = SymbolTableBuilder::new();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let foo_symbol_top =
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::empty());
|
|
||||||
let c_scope = builder.add_child_scope(root_scope_id, "C", ScopeKind::Class, None, None);
|
|
||||||
let foo_symbol_inner = builder.add_or_update_symbol(c_scope, "foo", SymbolFlags::empty());
|
|
||||||
|
|
||||||
assert_ne!(foo_symbol_top, foo_symbol_inner);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scope_from_id() {
|
|
||||||
let table = SymbolTableBuilder::new().finish();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let scope = root_scope_id.scope(&table);
|
|
||||||
|
|
||||||
assert_eq!(scope.name.as_str(), "<module>");
|
|
||||||
assert_eq!(scope.kind, ScopeKind::Module);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn symbol_from_id() {
|
|
||||||
let mut builder = SymbolTableBuilder::new();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let foo_symbol_id =
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::empty());
|
|
||||||
let table = builder.finish();
|
|
||||||
let symbol = foo_symbol_id.symbol(&table);
|
|
||||||
|
|
||||||
assert_eq!(symbol.name(), "foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bigger_symbol_table() {
|
|
||||||
let mut builder = SymbolTableBuilder::new();
|
|
||||||
let root_scope_id = SymbolTable::root_scope_id();
|
|
||||||
let foo_symbol_id =
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "foo", SymbolFlags::empty());
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "bar", SymbolFlags::empty());
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "baz", SymbolFlags::empty());
|
|
||||||
builder.add_or_update_symbol(root_scope_id, "qux", SymbolFlags::empty());
|
|
||||||
let table = builder.finish();
|
|
||||||
|
|
||||||
let foo_symbol_id_2 = table
|
|
||||||
.root_symbol_id_by_name("foo")
|
|
||||||
.expect("foo symbol to be found");
|
|
||||||
|
|
||||||
assert_eq!(foo_symbol_id_2, foo_symbol_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,762 +0,0 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
use ruff_python_ast as ast;
|
|
||||||
use ruff_python_ast::AstNode;
|
|
||||||
use std::fmt::Debug;
|
|
||||||
|
|
||||||
use crate::db::{QueryResult, SemanticDb, SemanticJar};
|
|
||||||
|
|
||||||
use crate::module::{resolve_module, ModuleName};
|
|
||||||
use crate::parse::parse;
|
|
||||||
use crate::semantic::types::{ModuleTypeId, Type};
|
|
||||||
use crate::semantic::{
|
|
||||||
resolve_global_symbol, semantic_index, ConstrainedDefinition, Definition, GlobalSymbolId,
|
|
||||||
ImportDefinition, ImportFromDefinition,
|
|
||||||
};
|
|
||||||
use crate::{FileId, Name};
|
|
||||||
|
|
||||||
// FIXME: Figure out proper dead-lock free synchronisation now that this takes `&db` instead of `&mut db`.
|
|
||||||
/// Resolve the public-facing type for a symbol (the type seen by other scopes: other modules, or
|
|
||||||
/// nested functions). Because calls to nested functions and imports can occur anywhere in control
|
|
||||||
/// flow, this type must be conservative and consider all definitions of the symbol that could
|
|
||||||
/// possibly be seen by another scope. Currently we take the most conservative approach, which is
|
|
||||||
/// the union of all definitions. We may be able to narrow this in future to eliminate definitions
|
|
||||||
/// which can't possibly (or at least likely) be seen by any other scope, so that e.g. we could
|
|
||||||
/// infer `Literal["1"]` instead of `Literal[1] | Literal["1"]` for `x` in `x = x; x = str(x);`.
|
|
||||||
#[tracing::instrument(level = "trace", skip(db))]
|
|
||||||
pub fn infer_symbol_public_type(db: &dyn SemanticDb, symbol: GlobalSymbolId) -> QueryResult<Type> {
|
|
||||||
let index = semantic_index(db, symbol.file_id)?;
|
|
||||||
let defs = index.symbol_table().definitions(symbol.symbol_id).to_vec();
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
|
|
||||||
if let Some(ty) = jar.type_store.get_cached_symbol_public_type(symbol) {
|
|
||||||
return Ok(ty);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ty = infer_type_from_definitions(db, symbol, defs.iter().cloned())?;
|
|
||||||
|
|
||||||
jar.type_store.cache_symbol_public_type(symbol, ty);
|
|
||||||
|
|
||||||
// TODO record dependencies
|
|
||||||
Ok(ty)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Infer type of a symbol as union of the given `Definitions`.
|
|
||||||
fn infer_type_from_definitions<T>(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
symbol: GlobalSymbolId,
|
|
||||||
definitions: T,
|
|
||||||
) -> QueryResult<Type>
|
|
||||||
where
|
|
||||||
T: Debug + IntoIterator<Item = Definition>,
|
|
||||||
{
|
|
||||||
infer_type_from_constrained_definitions(
|
|
||||||
db,
|
|
||||||
symbol,
|
|
||||||
definitions
|
|
||||||
.into_iter()
|
|
||||||
.map(|definition| ConstrainedDefinition {
|
|
||||||
definition,
|
|
||||||
constraints: vec![],
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Infer type of a symbol as union of the given `ConstrainedDefinitions`.
|
|
||||||
fn infer_type_from_constrained_definitions<T>(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
symbol: GlobalSymbolId,
|
|
||||||
constrained_definitions: T,
|
|
||||||
) -> QueryResult<Type>
|
|
||||||
where
|
|
||||||
T: IntoIterator<Item = ConstrainedDefinition>,
|
|
||||||
{
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
let mut tys = constrained_definitions
|
|
||||||
.into_iter()
|
|
||||||
.map(|def| infer_constrained_definition_type(db, symbol, def.clone()))
|
|
||||||
.peekable();
|
|
||||||
if let Some(first) = tys.next() {
|
|
||||||
if tys.peek().is_some() {
|
|
||||||
Ok(jar.type_store.add_union(
|
|
||||||
symbol.file_id,
|
|
||||||
&Iterator::chain(std::iter::once(first), tys).collect::<QueryResult<Vec<_>>>()?,
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
first
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(Type::Unknown)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Infer type for a ConstrainedDefinition (intersection of the definition type and the
|
|
||||||
/// constraints)
|
|
||||||
#[tracing::instrument(level = "trace", skip(db))]
|
|
||||||
pub fn infer_constrained_definition_type(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
symbol: GlobalSymbolId,
|
|
||||||
constrained_definition: ConstrainedDefinition,
|
|
||||||
) -> QueryResult<Type> {
|
|
||||||
let ConstrainedDefinition {
|
|
||||||
definition,
|
|
||||||
constraints,
|
|
||||||
} = constrained_definition;
|
|
||||||
let index = semantic_index(db, symbol.file_id)?;
|
|
||||||
let parsed = parse(db.upcast(), symbol.file_id)?;
|
|
||||||
let mut intersected_types = vec![infer_definition_type(db, symbol, definition)?];
|
|
||||||
for constraint in constraints {
|
|
||||||
if let Some(constraint_type) = infer_constraint_type(
|
|
||||||
db,
|
|
||||||
symbol,
|
|
||||||
index.resolve_expression_id(parsed.syntax(), constraint),
|
|
||||||
)? {
|
|
||||||
intersected_types.push(constraint_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
Ok(jar
|
|
||||||
.type_store
|
|
||||||
.add_intersection(symbol.file_id, &intersected_types, &[]))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Infer a type for a Definition
|
|
||||||
#[tracing::instrument(level = "trace", skip(db))]
|
|
||||||
pub fn infer_definition_type(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
symbol: GlobalSymbolId,
|
|
||||||
definition: Definition,
|
|
||||||
) -> QueryResult<Type> {
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
let type_store = &jar.type_store;
|
|
||||||
let file_id = symbol.file_id;
|
|
||||||
|
|
||||||
match definition {
|
|
||||||
Definition::Unbound => Ok(Type::Unbound),
|
|
||||||
Definition::Import(ImportDefinition {
|
|
||||||
module: module_name,
|
|
||||||
}) => {
|
|
||||||
if let Some(module) = resolve_module(db, module_name.clone())? {
|
|
||||||
Ok(Type::Module(ModuleTypeId { module, file_id }))
|
|
||||||
} else {
|
|
||||||
Ok(Type::Unknown)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Definition::ImportFrom(ImportFromDefinition {
|
|
||||||
module,
|
|
||||||
name,
|
|
||||||
level,
|
|
||||||
}) => {
|
|
||||||
// TODO relative imports
|
|
||||||
assert!(matches!(level, 0));
|
|
||||||
let module_name = ModuleName::new(module.as_ref().expect("TODO relative imports"));
|
|
||||||
let Some(module) = resolve_module(db, module_name.clone())? else {
|
|
||||||
return Ok(Type::Unknown);
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(remote_symbol) = resolve_global_symbol(db, module, &name)? {
|
|
||||||
infer_symbol_public_type(db, remote_symbol)
|
|
||||||
} else {
|
|
||||||
Ok(Type::Unknown)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Definition::ClassDef(node_key) => {
|
|
||||||
if let Some(ty) = type_store.get_cached_node_type(file_id, node_key.erased()) {
|
|
||||||
Ok(ty)
|
|
||||||
} else {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let index = semantic_index(db, file_id)?;
|
|
||||||
let node = node_key.resolve_unwrap(ast.as_any_node_ref());
|
|
||||||
|
|
||||||
let mut bases = Vec::with_capacity(node.bases().len());
|
|
||||||
|
|
||||||
for base in node.bases() {
|
|
||||||
bases.push(infer_expr_type(db, file_id, base)?);
|
|
||||||
}
|
|
||||||
let scope_id = index.symbol_table().scope_id_for_node(node_key.erased());
|
|
||||||
let ty = type_store.add_class(file_id, &node.name.id, scope_id, bases);
|
|
||||||
type_store.cache_node_type(file_id, *node_key.erased(), ty);
|
|
||||||
Ok(ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Definition::FunctionDef(node_key) => {
|
|
||||||
if let Some(ty) = type_store.get_cached_node_type(file_id, node_key.erased()) {
|
|
||||||
Ok(ty)
|
|
||||||
} else {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let index = semantic_index(db, file_id)?;
|
|
||||||
let node = node_key
|
|
||||||
.resolve(ast.as_any_node_ref())
|
|
||||||
.expect("node key should resolve");
|
|
||||||
|
|
||||||
let decorator_tys = node
|
|
||||||
.decorator_list
|
|
||||||
.iter()
|
|
||||||
.map(|decorator| infer_expr_type(db, file_id, &decorator.expression))
|
|
||||||
.collect::<QueryResult<_>>()?;
|
|
||||||
let scope_id = index.symbol_table().scope_id_for_node(node_key.erased());
|
|
||||||
let ty = type_store.add_function(
|
|
||||||
file_id,
|
|
||||||
&node.name.id,
|
|
||||||
symbol.symbol_id,
|
|
||||||
scope_id,
|
|
||||||
decorator_tys,
|
|
||||||
);
|
|
||||||
type_store.cache_node_type(file_id, *node_key.erased(), ty);
|
|
||||||
Ok(ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Definition::Assignment(node_key) => {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let node = node_key.resolve_unwrap(ast.as_any_node_ref());
|
|
||||||
// TODO handle unpacking assignment
|
|
||||||
infer_expr_type(db, file_id, &node.value)
|
|
||||||
}
|
|
||||||
Definition::AnnotatedAssignment(node_key) => {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let node = node_key.resolve_unwrap(ast.as_any_node_ref());
|
|
||||||
// TODO actually look at the annotation
|
|
||||||
let Some(value) = &node.value else {
|
|
||||||
return Ok(Type::Unknown);
|
|
||||||
};
|
|
||||||
// TODO handle unpacking assignment
|
|
||||||
infer_expr_type(db, file_id, value)
|
|
||||||
}
|
|
||||||
Definition::NamedExpr(node_key) => {
|
|
||||||
let parsed = parse(db.upcast(), file_id)?;
|
|
||||||
let ast = parsed.syntax();
|
|
||||||
let node = node_key.resolve_unwrap(ast.as_any_node_ref());
|
|
||||||
infer_expr_type(db, file_id, &node.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the type that the given constraint (an expression from a control-flow test) requires the
|
|
||||||
/// given symbol to have. For example, returns the Type "~None" as the constraint type if given the
|
|
||||||
/// symbol ID for x and the expression ID for `x is not None`. Returns (Rust) None if the given
|
|
||||||
/// expression applies no constraints on the given symbol.
|
|
||||||
#[tracing::instrument(level = "trace", skip(db))]
|
|
||||||
fn infer_constraint_type(
|
|
||||||
db: &dyn SemanticDb,
|
|
||||||
symbol_id: GlobalSymbolId,
|
|
||||||
// TODO this should preferably take an &ast::Expr instead of AnyNodeRef
|
|
||||||
expression: ast::AnyNodeRef,
|
|
||||||
) -> QueryResult<Option<Type>> {
|
|
||||||
let file_id = symbol_id.file_id;
|
|
||||||
let index = semantic_index(db, file_id)?;
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
let symbol_name = symbol_id.symbol_id.symbol(&index.symbol_table).name();
|
|
||||||
// TODO narrowing attributes
|
|
||||||
// TODO narrowing dict keys
|
|
||||||
// TODO isinstance, ==/!=, type(...), literals, bools...
|
|
||||||
match expression {
|
|
||||||
ast::AnyNodeRef::ExprCompare(ast::ExprCompare {
|
|
||||||
left,
|
|
||||||
ops,
|
|
||||||
comparators,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
// TODO chained comparisons
|
|
||||||
match left.as_ref() {
|
|
||||||
ast::Expr::Name(ast::ExprName { id, .. }) if id == symbol_name => match ops[0] {
|
|
||||||
ast::CmpOp::Is | ast::CmpOp::IsNot => {
|
|
||||||
Ok(match infer_expr_type(db, file_id, &comparators[0])? {
|
|
||||||
Type::None => Some(Type::None),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
.map(|ty| {
|
|
||||||
if matches!(ops[0], ast::CmpOp::IsNot) {
|
|
||||||
jar.type_store.add_intersection(file_id, &[], &[ty])
|
|
||||||
} else {
|
|
||||||
ty
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
_ => Ok(None),
|
|
||||||
},
|
|
||||||
_ => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Infer type of the given expression.
|
|
||||||
fn infer_expr_type(db: &dyn SemanticDb, file_id: FileId, expr: &ast::Expr) -> QueryResult<Type> {
|
|
||||||
// TODO cache the resolution of the type on the node
|
|
||||||
let index = semantic_index(db, file_id)?;
|
|
||||||
match expr {
|
|
||||||
ast::Expr::NoneLiteral(_) => Ok(Type::None),
|
|
||||||
ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => {
|
|
||||||
match value {
|
|
||||||
ast::Number::Int(n) => {
|
|
||||||
// TODO support big int literals
|
|
||||||
Ok(n.as_i64().map(Type::IntLiteral).unwrap_or(Type::Unknown))
|
|
||||||
}
|
|
||||||
// TODO builtins.float or builtins.complex
|
|
||||||
_ => Ok(Type::Unknown),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast::Expr::Name(name) => {
|
|
||||||
// TODO look up in the correct scope, don't assume global
|
|
||||||
if let Some(symbol_id) = index.symbol_table().root_symbol_id_by_name(&name.id) {
|
|
||||||
infer_type_from_constrained_definitions(
|
|
||||||
db,
|
|
||||||
GlobalSymbolId { file_id, symbol_id },
|
|
||||||
index.reachable_definitions(symbol_id, expr),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Ok(Type::Unknown)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
|
|
||||||
let value_type = infer_expr_type(db, file_id, value)?;
|
|
||||||
let attr_name = &Name::new(&attr.id);
|
|
||||||
value_type
|
|
||||||
.get_member(db, attr_name)
|
|
||||||
.map(|ty| ty.unwrap_or(Type::Unknown))
|
|
||||||
}
|
|
||||||
ast::Expr::BinOp(ast::ExprBinOp {
|
|
||||||
left, op, right, ..
|
|
||||||
}) => {
|
|
||||||
let left_ty = infer_expr_type(db, file_id, left)?;
|
|
||||||
let right_ty = infer_expr_type(db, file_id, right)?;
|
|
||||||
// TODO add reverse bin op support if right <: left
|
|
||||||
left_ty.resolve_bin_op(db, *op, right_ty)
|
|
||||||
}
|
|
||||||
ast::Expr::Named(ast::ExprNamed { value, .. }) => infer_expr_type(db, file_id, value),
|
|
||||||
ast::Expr::If(ast::ExprIf { body, orelse, .. }) => {
|
|
||||||
// TODO detect statically known truthy or falsy test
|
|
||||||
let body_ty = infer_expr_type(db, file_id, body)?;
|
|
||||||
let else_ty = infer_expr_type(db, file_id, orelse)?;
|
|
||||||
let jar: &SemanticJar = db.jar()?;
|
|
||||||
Ok(jar.type_store.add_union(file_id, &[body_ty, else_ty]))
|
|
||||||
}
|
|
||||||
_ => todo!("expression type resolution for {:?}", expr),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use crate::db::tests::TestDb;
|
|
||||||
use crate::db::{HasJar, SemanticJar};
|
|
||||||
use crate::module::{
|
|
||||||
resolve_module, set_module_search_paths, ModuleName, ModuleResolutionInputs,
|
|
||||||
};
|
|
||||||
use crate::semantic::{infer_symbol_public_type, resolve_global_symbol, Type};
|
|
||||||
use crate::Name;
|
|
||||||
|
|
||||||
// TODO with virtual filesystem we shouldn't have to write files to disk for these
|
|
||||||
// tests
|
|
||||||
|
|
||||||
struct TestCase {
|
|
||||||
temp_dir: tempfile::TempDir,
|
|
||||||
db: TestDb,
|
|
||||||
|
|
||||||
src: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_test() -> std::io::Result<TestCase> {
|
|
||||||
let temp_dir = tempfile::tempdir()?;
|
|
||||||
|
|
||||||
let src = temp_dir.path().join("src");
|
|
||||||
std::fs::create_dir(&src)?;
|
|
||||||
let src = src.canonicalize()?;
|
|
||||||
|
|
||||||
let search_paths = ModuleResolutionInputs {
|
|
||||||
extra_paths: vec![],
|
|
||||||
workspace_root: src.clone(),
|
|
||||||
site_packages: None,
|
|
||||||
custom_typeshed: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut db = TestDb::default();
|
|
||||||
set_module_search_paths(&mut db, search_paths);
|
|
||||||
|
|
||||||
Ok(TestCase { temp_dir, db, src })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_to_path(case: &TestCase, relative_path: &str, contents: &str) -> anyhow::Result<()> {
|
|
||||||
let path = case.src.join(relative_path);
|
|
||||||
std::fs::write(path, contents)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_public_type(
|
|
||||||
case: &TestCase,
|
|
||||||
module_name: &str,
|
|
||||||
variable_name: &str,
|
|
||||||
) -> anyhow::Result<Type> {
|
|
||||||
let db = &case.db;
|
|
||||||
let module = resolve_module(db, ModuleName::new(module_name))?.expect("Module to exist");
|
|
||||||
let symbol = resolve_global_symbol(db, module, variable_name)?.expect("symbol to exist");
|
|
||||||
|
|
||||||
Ok(infer_symbol_public_type(db, symbol)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn assert_public_type(
|
|
||||||
case: &TestCase,
|
|
||||||
module_name: &str,
|
|
||||||
variable_name: &str,
|
|
||||||
type_name: &str,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let ty = get_public_type(case, module_name, variable_name)?;
|
|
||||||
|
|
||||||
let jar = HasJar::<SemanticJar>::jar(&case.db)?;
|
|
||||||
assert_eq!(format!("{}", ty.display(&jar.type_store)), type_name);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn follow_import_to_class() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(&case, "a.py", "from b import C as D; E = D")?;
|
|
||||||
write_to_path(&case, "b.py", "class C: pass")?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "E", "Literal[C]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_base_class_by_name() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"mod.py",
|
|
||||||
"
|
|
||||||
class Base: pass
|
|
||||||
class Sub(Base): pass
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let ty = get_public_type(&case, "mod", "Sub")?;
|
|
||||||
|
|
||||||
let Type::Class(class_id) = ty else {
|
|
||||||
panic!("Sub is not a Class")
|
|
||||||
};
|
|
||||||
let jar = HasJar::<SemanticJar>::jar(&case.db)?;
|
|
||||||
let base_names: Vec<_> = jar
|
|
||||||
.type_store
|
|
||||||
.get_class(class_id)
|
|
||||||
.bases()
|
|
||||||
.iter()
|
|
||||||
.map(|base_ty| format!("{}", base_ty.display(&jar.type_store)))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
assert_eq!(base_names, vec!["Literal[Base]"]);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_method() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"mod.py",
|
|
||||||
"
|
|
||||||
class C:
|
|
||||||
def f(self): pass
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let ty = get_public_type(&case, "mod", "C")?;
|
|
||||||
|
|
||||||
let Type::Class(class_id) = ty else {
|
|
||||||
panic!("C is not a Class");
|
|
||||||
};
|
|
||||||
|
|
||||||
let member_ty = class_id
|
|
||||||
.get_own_class_member(&case.db, &Name::new("f"))
|
|
||||||
.expect("C.f to resolve");
|
|
||||||
|
|
||||||
let Some(Type::Function(func_id)) = member_ty else {
|
|
||||||
panic!("C.f is not a Function");
|
|
||||||
};
|
|
||||||
|
|
||||||
let jar = HasJar::<SemanticJar>::jar(&case.db)?;
|
|
||||||
let function = jar.type_store.get_function(func_id);
|
|
||||||
assert_eq!(function.name(), "f");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_module_member() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(&case, "a.py", "import b; D = b.C")?;
|
|
||||||
write_to_path(&case, "b.py", "class C: pass")?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "D", "Literal[C]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_literal() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(&case, "a.py", "x = 1")?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_union() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
if flag:
|
|
||||||
x = 1
|
|
||||||
else:
|
|
||||||
x = 2
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1, 2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn resolve_visible_def() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
write_to_path(&case, "a.py", "y = 1; y = 2; x = y")?;
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn join_paths() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
y = 1
|
|
||||||
y = 2
|
|
||||||
if flag:
|
|
||||||
y = 3
|
|
||||||
x = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[2, 3]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn maybe_unbound() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
if flag:
|
|
||||||
y = 1
|
|
||||||
x = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1] | Unbound")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn if_elif_else() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
y = 1
|
|
||||||
y = 2
|
|
||||||
if flag:
|
|
||||||
y = 3
|
|
||||||
elif flag2:
|
|
||||||
y = 4
|
|
||||||
else:
|
|
||||||
r = y
|
|
||||||
y = 5
|
|
||||||
s = y
|
|
||||||
x = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[3, 4, 5]")?;
|
|
||||||
assert_public_type(&case, "a", "r", "Literal[2]")?;
|
|
||||||
assert_public_type(&case, "a", "s", "Literal[5]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn if_elif() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
y = 1
|
|
||||||
y = 2
|
|
||||||
if flag:
|
|
||||||
y = 3
|
|
||||||
elif flag2:
|
|
||||||
y = 4
|
|
||||||
x = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[2, 3, 4]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn literal_int_arithmetic() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
a = 2 + 1
|
|
||||||
b = a - 4
|
|
||||||
c = a * b
|
|
||||||
d = c / 3
|
|
||||||
e = 5 % 3
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "a", "Literal[3]")?;
|
|
||||||
assert_public_type(&case, "a", "b", "Literal[-1]")?;
|
|
||||||
assert_public_type(&case, "a", "c", "Literal[-3]")?;
|
|
||||||
assert_public_type(&case, "a", "d", "Literal[-1]")?;
|
|
||||||
assert_public_type(&case, "a", "e", "Literal[2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn walrus() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
x = (y := 1) + 1
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[2]")?;
|
|
||||||
assert_public_type(&case, "a", "y", "Literal[1]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ifexpr() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
x = 1 if flag else 2
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1, 2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ifexpr_walrus() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
y = z = 0
|
|
||||||
x = (y := 1) if flag else (z := 2)
|
|
||||||
a = y
|
|
||||||
b = z
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1, 2]")?;
|
|
||||||
assert_public_type(&case, "a", "a", "Literal[0, 1]")?;
|
|
||||||
assert_public_type(&case, "a", "b", "Literal[0, 2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ifexpr_walrus_2() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
y = 0
|
|
||||||
(y := 1) if flag else (y := 2)
|
|
||||||
a = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "a", "Literal[1, 2]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ifexpr_nested() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
x = 1 if flag else 2 if flag2 else 3
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1, 2, 3]")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn none() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
x = 1 if flag else None
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assert_public_type(&case, "a", "x", "Literal[1] | None")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn narrow_none() -> anyhow::Result<()> {
|
|
||||||
let case = create_test()?;
|
|
||||||
|
|
||||||
write_to_path(
|
|
||||||
&case,
|
|
||||||
"a.py",
|
|
||||||
"
|
|
||||||
x = 1 if flag else None
|
|
||||||
y = 0
|
|
||||||
if x is not None:
|
|
||||||
y = x
|
|
||||||
z = y
|
|
||||||
",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// TODO normalization of unions and intersections: this type is technically correct but
|
|
||||||
// begging for normalization
|
|
||||||
assert_public_type(&case, "a", "z", "Literal[0] | Literal[1] | None & ~None")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use ruff_notebook::Notebook;
|
|
||||||
use ruff_python_ast::PySourceType;
|
|
||||||
|
|
||||||
use crate::cache::KeyValueCache;
|
|
||||||
use crate::db::{QueryResult, SourceDb};
|
|
||||||
use crate::files::FileId;
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub(crate) fn source_text(db: &dyn SourceDb, file_id: FileId) -> QueryResult<Source> {
|
|
||||||
let jar = db.jar()?;
|
|
||||||
let sources = &jar.sources;
|
|
||||||
|
|
||||||
sources.get(&file_id, |file_id| {
|
|
||||||
let path = db.file_path(*file_id);
|
|
||||||
|
|
||||||
let source_text = std::fs::read_to_string(&path).unwrap_or_else(|err| {
|
|
||||||
tracing::error!("Failed to read file '{path:?}: {err}'. Falling back to empty text");
|
|
||||||
String::new()
|
|
||||||
});
|
|
||||||
|
|
||||||
let python_ty = PySourceType::from(&path);
|
|
||||||
|
|
||||||
let kind = match python_ty {
|
|
||||||
PySourceType::Python => {
|
|
||||||
SourceKind::Python(Arc::from(source_text))
|
|
||||||
}
|
|
||||||
PySourceType::Stub => SourceKind::Stub(Arc::from(source_text)),
|
|
||||||
PySourceType::Ipynb => {
|
|
||||||
let notebook = Notebook::from_source_code(&source_text).unwrap_or_else(|err| {
|
|
||||||
// TODO should this be changed to never fail?
|
|
||||||
// or should we instead add a diagnostic somewhere? But what would we return in this case?
|
|
||||||
tracing::error!(
|
|
||||||
"Failed to parse notebook '{path:?}: {err}'. Falling back to an empty notebook"
|
|
||||||
);
|
|
||||||
Notebook::from_source_code("").unwrap()
|
|
||||||
});
|
|
||||||
|
|
||||||
SourceKind::IpyNotebook(Arc::new(notebook))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Source { kind })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum SourceKind {
|
|
||||||
Python(Arc<str>),
|
|
||||||
Stub(Arc<str>),
|
|
||||||
IpyNotebook(Arc<Notebook>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a SourceKind> for PySourceType {
|
|
||||||
fn from(value: &'a SourceKind) -> Self {
|
|
||||||
match value {
|
|
||||||
SourceKind::Python(_) => PySourceType::Python,
|
|
||||||
SourceKind::Stub(_) => PySourceType::Stub,
|
|
||||||
SourceKind::IpyNotebook(_) => PySourceType::Ipynb,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct Source {
|
|
||||||
kind: SourceKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Source {
|
|
||||||
pub fn python<T: Into<Arc<str>>>(source: T) -> Self {
|
|
||||||
Self {
|
|
||||||
kind: SourceKind::Python(source.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn kind(&self) -> &SourceKind {
|
|
||||||
&self.kind
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn text(&self) -> &str {
|
|
||||||
match &self.kind {
|
|
||||||
SourceKind::Python(text) => text,
|
|
||||||
SourceKind::Stub(text) => text,
|
|
||||||
SourceKind::IpyNotebook(notebook) => notebook.source_code(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct SourceStorage(pub(crate) KeyValueCache<FileId, Source>);
|
|
||||||
|
|
||||||
impl Deref for SourceStorage {
|
|
||||||
type Target = KeyValueCache<FileId, Source>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for SourceStorage {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use notify::event::{CreateKind, RemoveKind};
|
|
||||||
use notify::{recommended_watcher, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
|
||||||
|
|
||||||
use crate::program::{FileChangeKind, FileWatcherChange};
|
|
||||||
|
|
||||||
pub struct FileWatcher {
|
|
||||||
watcher: RecommendedWatcher,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait EventHandler: Send + 'static {
|
|
||||||
fn handle(&self, changes: Vec<FileWatcherChange>);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> EventHandler for F
|
|
||||||
where
|
|
||||||
F: Fn(Vec<FileWatcherChange>) + Send + 'static,
|
|
||||||
{
|
|
||||||
fn handle(&self, changes: Vec<FileWatcherChange>) {
|
|
||||||
let f = self;
|
|
||||||
f(changes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileWatcher {
|
|
||||||
pub fn new<E>(handler: E) -> anyhow::Result<Self>
|
|
||||||
where
|
|
||||||
E: EventHandler,
|
|
||||||
{
|
|
||||||
Self::from_handler(Box::new(handler))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_handler(handler: Box<dyn EventHandler>) -> anyhow::Result<Self> {
|
|
||||||
let watcher = recommended_watcher(move |changes: notify::Result<Event>| {
|
|
||||||
match changes {
|
|
||||||
Ok(event) => {
|
|
||||||
// TODO verify that this handles all events correctly
|
|
||||||
let change_kind = match event.kind {
|
|
||||||
EventKind::Create(CreateKind::File) => FileChangeKind::Created,
|
|
||||||
EventKind::Modify(_) => FileChangeKind::Modified,
|
|
||||||
EventKind::Remove(RemoveKind::File) => FileChangeKind::Deleted,
|
|
||||||
_ => {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut changes = Vec::new();
|
|
||||||
|
|
||||||
for path in event.paths {
|
|
||||||
if path.is_file() {
|
|
||||||
changes.push(FileWatcherChange::new(path, change_kind));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !changes.is_empty() {
|
|
||||||
handler.handle(changes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO proper error handling
|
|
||||||
Err(err) => {
|
|
||||||
panic!("Error: {err}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.context("Failed to create file watcher.")?;
|
|
||||||
|
|
||||||
Ok(Self { watcher })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn watch_folder(&mut self, path: &Path) -> anyhow::Result<()> {
|
|
||||||
self.watcher.watch(path, RecursiveMode::Recursive)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "red_knot_module_resolver"
|
|
||||||
version = "0.0.0"
|
|
||||||
publish = false
|
|
||||||
authors = { workspace = true }
|
|
||||||
edition = { workspace = true }
|
|
||||||
rust-version = { workspace = true }
|
|
||||||
homepage = { workspace = true }
|
|
||||||
documentation = { workspace = true }
|
|
||||||
repository = { workspace = true }
|
|
||||||
license = { workspace = true }
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
ruff_db = { workspace = true }
|
|
||||||
ruff_python_stdlib = { workspace = true }
|
|
||||||
|
|
||||||
rustc-hash = { workspace = true }
|
|
||||||
salsa = { workspace = true }
|
|
||||||
smol_str = { workspace = true }
|
|
||||||
tracing = { workspace = true }
|
|
||||||
zip = { workspace = true }
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
path-slash = { workspace = true }
|
|
||||||
walkdir = { workspace = true }
|
|
||||||
zip = { workspace = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
anyhow = { workspace = true }
|
|
||||||
insta = { workspace = true }
|
|
||||||
tempfile = { workspace = true }
|
|
||||||
walkdir = { workspace = true }
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# 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).
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
//! Build script to package our vendored typeshed files
|
|
||||||
//! into a zip archive that can be included in the Ruff binary.
|
|
||||||
//!
|
|
||||||
//! This script should be automatically run at build time
|
|
||||||
//! whenever the script itself changes, or whenever any files
|
|
||||||
//! in `crates/red_knot_module_resolver/vendor/typeshed` change.
|
|
||||||
|
|
||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use path_slash::PathExt;
|
|
||||||
use zip::result::ZipResult;
|
|
||||||
use zip::write::{FileOptions, ZipWriter};
|
|
||||||
use zip::CompressionMethod;
|
|
||||||
|
|
||||||
const TYPESHED_SOURCE_DIR: &str = "vendor/typeshed";
|
|
||||||
const TYPESHED_ZIP_LOCATION: &str = "/zipped_typeshed.zip";
|
|
||||||
|
|
||||||
/// Recursively zip the contents of an entire directory.
|
|
||||||
///
|
|
||||||
/// This routine is adapted from a recipe at
|
|
||||||
/// <https://github.com/zip-rs/zip-old/blob/5d0f198124946b7be4e5969719a7f29f363118cd/examples/write_dir.rs>
|
|
||||||
fn zip_dir(directory_path: &str, writer: File) -> ZipResult<File> {
|
|
||||||
let mut zip = ZipWriter::new(writer);
|
|
||||||
|
|
||||||
let options = FileOptions::default()
|
|
||||||
.compression_method(CompressionMethod::Zstd)
|
|
||||||
.unix_permissions(0o644);
|
|
||||||
|
|
||||||
for entry in walkdir::WalkDir::new(directory_path) {
|
|
||||||
let dir_entry = entry.unwrap();
|
|
||||||
let absolute_path = dir_entry.path();
|
|
||||||
let normalized_relative_path = absolute_path
|
|
||||||
.strip_prefix(Path::new(directory_path))
|
|
||||||
.unwrap()
|
|
||||||
.to_slash()
|
|
||||||
.expect("Unexpected non-utf8 typeshed path!");
|
|
||||||
|
|
||||||
// Write file or directory explicitly
|
|
||||||
// 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)?;
|
|
||||||
let mut f = File::open(absolute_path)?;
|
|
||||||
std::io::copy(&mut f, &mut zip).unwrap();
|
|
||||||
} else if !normalized_relative_path.is_empty() {
|
|
||||||
// Only if not root! Avoids path spec / warning
|
|
||||||
// and mapname conversion failed error on unzip
|
|
||||||
println!("adding dir {absolute_path:?} as {normalized_relative_path:?} ...");
|
|
||||||
zip.add_directory(normalized_relative_path, options)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
zip.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("cargo:rerun-if-changed={TYPESHED_SOURCE_DIR}");
|
|
||||||
assert!(
|
|
||||||
Path::new(TYPESHED_SOURCE_DIR).is_dir(),
|
|
||||||
"Where is typeshed?"
|
|
||||||
);
|
|
||||||
let out_dir = std::env::var("OUT_DIR").unwrap();
|
|
||||||
|
|
||||||
// N.B. Deliberately using `format!()` instead of `Path::join()` here,
|
|
||||||
// so that we use `/` as a path separator on all platforms.
|
|
||||||
// That enables us to load the typeshed zip at compile time in `module.rs`
|
|
||||||
// (otherwise we'd have to dynamically determine the exact path to the typeshed zip
|
|
||||||
// based on the default path separator for the specific platform we're on,
|
|
||||||
// which can't be done at compile time.)
|
|
||||||
let zipped_typeshed_location = format!("{out_dir}{TYPESHED_ZIP_LOCATION}");
|
|
||||||
|
|
||||||
let zipped_typeshed = File::create(zipped_typeshed_location).unwrap();
|
|
||||||
zip_dir(TYPESHED_SOURCE_DIR, zipped_typeshed).unwrap();
|
|
||||||
}
|
|
||||||
@@ -1,156 +0,0 @@
|
|||||||
use ruff_db::Upcast;
|
|
||||||
|
|
||||||
use crate::resolver::{
|
|
||||||
file_to_module,
|
|
||||||
internal::{ModuleNameIngredient, ModuleResolverSearchPaths},
|
|
||||||
resolve_module_query,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[salsa::jar(db=Db)]
|
|
||||||
pub struct Jar(
|
|
||||||
ModuleNameIngredient<'_>,
|
|
||||||
ModuleResolverSearchPaths,
|
|
||||||
resolve_module_query,
|
|
||||||
file_to_module,
|
|
||||||
);
|
|
||||||
|
|
||||||
pub trait Db: salsa::DbWithJar<Jar> + ruff_db::Db + Upcast<dyn ruff_db::Db> {}
|
|
||||||
|
|
||||||
pub(crate) mod tests {
|
|
||||||
use std::sync;
|
|
||||||
|
|
||||||
use salsa::DebugWithDb;
|
|
||||||
|
|
||||||
use ruff_db::file_system::{FileSystem, MemoryFileSystem, OsFileSystem};
|
|
||||||
use ruff_db::vfs::Vfs;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[salsa::db(Jar, ruff_db::Jar)]
|
|
||||||
pub(crate) struct TestDb {
|
|
||||||
storage: salsa::Storage<Self>,
|
|
||||||
file_system: TestFileSystem,
|
|
||||||
events: sync::Arc<sync::Mutex<Vec<salsa::Event>>>,
|
|
||||||
vfs: Vfs,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestDb {
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
storage: salsa::Storage::default(),
|
|
||||||
file_system: TestFileSystem::Memory(MemoryFileSystem::default()),
|
|
||||||
events: sync::Arc::default(),
|
|
||||||
vfs: Vfs::with_stubbed_vendored(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the memory file system.
|
|
||||||
///
|
|
||||||
/// ## Panics
|
|
||||||
/// If this test db isn't using a memory file system.
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn memory_file_system(&self) -> &MemoryFileSystem {
|
|
||||||
if let TestFileSystem::Memory(fs) = &self.file_system {
|
|
||||||
fs
|
|
||||||
} else {
|
|
||||||
panic!("The test db is not using a memory file system");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Uses the real file system instead of the memory file system.
|
|
||||||
///
|
|
||||||
/// This useful for testing advanced file system features like permissions, symlinks, etc.
|
|
||||||
///
|
|
||||||
/// Note that any files written to the memory file system won't be copied over.
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn with_os_file_system(&mut self) {
|
|
||||||
self.file_system = TestFileSystem::Os(OsFileSystem);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn vfs_mut(&mut self) -> &mut Vfs {
|
|
||||||
&mut self.vfs
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Takes the salsa events.
|
|
||||||
///
|
|
||||||
/// ## Panics
|
|
||||||
/// If there are any pending salsa snapshots.
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn take_salsa_events(&mut self) -> Vec<salsa::Event> {
|
|
||||||
let inner = sync::Arc::get_mut(&mut self.events).expect("no pending salsa snapshots");
|
|
||||||
|
|
||||||
let events = inner.get_mut().unwrap();
|
|
||||||
std::mem::take(&mut *events)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clears the salsa events.
|
|
||||||
///
|
|
||||||
/// ## Panics
|
|
||||||
/// If there are any pending salsa snapshots.
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn clear_salsa_events(&mut self) {
|
|
||||||
self.take_salsa_events();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Upcast<dyn ruff_db::Db> for TestDb {
|
|
||||||
fn upcast(&self) -> &(dyn ruff_db::Db + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ruff_db::Db for TestDb {
|
|
||||||
fn file_system(&self) -> &dyn ruff_db::file_system::FileSystem {
|
|
||||||
self.file_system.inner()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn vfs(&self) -> &ruff_db::vfs::Vfs {
|
|
||||||
&self.vfs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Db for TestDb {}
|
|
||||||
|
|
||||||
impl salsa::Database for TestDb {
|
|
||||||
fn salsa_event(&self, event: salsa::Event) {
|
|
||||||
tracing::trace!("event: {:?}", event.debug(self));
|
|
||||||
let mut events = self.events.lock().unwrap();
|
|
||||||
events.push(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl salsa::ParallelDatabase for TestDb {
|
|
||||||
fn snapshot(&self) -> salsa::Snapshot<Self> {
|
|
||||||
salsa::Snapshot::new(Self {
|
|
||||||
storage: self.storage.snapshot(),
|
|
||||||
file_system: self.file_system.snapshot(),
|
|
||||||
events: self.events.clone(),
|
|
||||||
vfs: self.vfs.snapshot(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum TestFileSystem {
|
|
||||||
Memory(MemoryFileSystem),
|
|
||||||
#[allow(unused)]
|
|
||||||
Os(OsFileSystem),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestFileSystem {
|
|
||||||
fn inner(&self) -> &dyn FileSystem {
|
|
||||||
match self {
|
|
||||||
Self::Memory(inner) => inner,
|
|
||||||
Self::Os(inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn snapshot(&self) -> Self {
|
|
||||||
match self {
|
|
||||||
Self::Memory(inner) => Self::Memory(inner.snapshot()),
|
|
||||||
Self::Os(inner) => Self::Os(inner.snapshot()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
mod db;
|
|
||||||
mod module;
|
|
||||||
mod resolver;
|
|
||||||
mod typeshed;
|
|
||||||
|
|
||||||
pub use db::{Db, Jar};
|
|
||||||
pub use module::{ModuleKind, ModuleName};
|
|
||||||
pub use resolver::{resolve_module, set_module_resolution_settings, ModuleResolutionSettings};
|
|
||||||
pub use typeshed::versions::TypeshedVersions;
|
|
||||||
@@ -1,346 +0,0 @@
|
|||||||
use std::fmt::Formatter;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use ruff_db::file_system::FileSystemPath;
|
|
||||||
use ruff_db::vfs::{VfsFile, VfsPath};
|
|
||||||
use ruff_python_stdlib::identifiers::is_identifier;
|
|
||||||
|
|
||||||
use crate::Db;
|
|
||||||
|
|
||||||
/// A module name, e.g. `foo.bar`.
|
|
||||||
///
|
|
||||||
/// Always normalized to the absolute form (never a relative module name, i.e., never `.foo`).
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
|
||||||
pub struct ModuleName(smol_str::SmolStr);
|
|
||||||
|
|
||||||
impl ModuleName {
|
|
||||||
/// Creates a new module name for `name`. Returns `Some` if `name` is a valid, absolute
|
|
||||||
/// module name and `None` otherwise.
|
|
||||||
///
|
|
||||||
/// The module name is invalid if:
|
|
||||||
///
|
|
||||||
/// * The name is empty
|
|
||||||
/// * The name is relative
|
|
||||||
/// * The name ends with a `.`
|
|
||||||
/// * The name contains a sequence of multiple dots
|
|
||||||
/// * A component of a name (the part between two dots) isn't a valid python identifier.
|
|
||||||
#[inline]
|
|
||||||
pub fn new(name: &str) -> Option<Self> {
|
|
||||||
Self::new_from_smol(smol_str::SmolStr::new(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new module name for `name` where `name` is a static string.
|
|
||||||
/// Returns `Some` if `name` is a valid, absolute module name and `None` otherwise.
|
|
||||||
///
|
|
||||||
/// The module name is invalid if:
|
|
||||||
///
|
|
||||||
/// * The name is empty
|
|
||||||
/// * The name is relative
|
|
||||||
/// * The name ends with a `.`
|
|
||||||
/// * The name contains a sequence of multiple dots
|
|
||||||
/// * A component of a name (the part between two dots) isn't a valid python identifier.
|
|
||||||
///
|
|
||||||
/// ## Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use red_knot_module_resolver::ModuleName;
|
|
||||||
///
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo.bar").as_deref(), Some("foo.bar"));
|
|
||||||
/// assert_eq!(ModuleName::new_static(""), None);
|
|
||||||
/// assert_eq!(ModuleName::new_static("..foo"), None);
|
|
||||||
/// assert_eq!(ModuleName::new_static(".foo"), None);
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo."), None);
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo..bar"), None);
|
|
||||||
/// assert_eq!(ModuleName::new_static("2000"), None);
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn new_static(name: &'static str) -> Option<Self> {
|
|
||||||
Self::new_from_smol(smol_str::SmolStr::new_static(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_from_smol(name: smol_str::SmolStr) -> Option<Self> {
|
|
||||||
if name.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if name.split('.').all(is_identifier) {
|
|
||||||
Some(Self(name))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An iterator over the components of the module name:
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use red_knot_module_resolver::ModuleName;
|
|
||||||
///
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().components().collect::<Vec<_>>(), vec!["foo", "bar", "baz"]);
|
|
||||||
/// ```
|
|
||||||
pub fn components(&self) -> impl DoubleEndedIterator<Item = &str> {
|
|
||||||
self.0.split('.')
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The name of this module's immediate parent, if it has a parent.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use red_knot_module_resolver::ModuleName;
|
|
||||||
///
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo.bar").unwrap().parent(), Some(ModuleName::new_static("foo").unwrap()));
|
|
||||||
/// assert_eq!(ModuleName::new_static("foo.bar.baz").unwrap().parent(), Some(ModuleName::new_static("foo.bar").unwrap()));
|
|
||||||
/// assert_eq!(ModuleName::new_static("root").unwrap().parent(), None);
|
|
||||||
/// ```
|
|
||||||
pub fn parent(&self) -> Option<ModuleName> {
|
|
||||||
let (parent, _) = self.0.rsplit_once('.')?;
|
|
||||||
|
|
||||||
Some(Self(smol_str::SmolStr::new(parent)))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if the name starts with `other`.
|
|
||||||
///
|
|
||||||
/// This is equivalent to checking if `self` is a sub-module of `other`.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use red_knot_module_resolver::ModuleName;
|
|
||||||
///
|
|
||||||
/// assert!(ModuleName::new_static("foo.bar").unwrap().starts_with(&ModuleName::new_static("foo").unwrap()));
|
|
||||||
///
|
|
||||||
/// assert!(!ModuleName::new_static("foo.bar").unwrap().starts_with(&ModuleName::new_static("bar").unwrap()));
|
|
||||||
/// assert!(!ModuleName::new_static("foo_bar").unwrap().starts_with(&ModuleName::new_static("foo").unwrap()));
|
|
||||||
/// ```
|
|
||||||
pub fn starts_with(&self, other: &ModuleName) -> bool {
|
|
||||||
let mut self_components = self.components();
|
|
||||||
let other_components = other.components();
|
|
||||||
|
|
||||||
for other_component in other_components {
|
|
||||||
if self_components.next() != Some(other_component) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn from_relative_path(path: &FileSystemPath) -> Option<Self> {
|
|
||||||
let path = if path.ends_with("__init__.py") || path.ends_with("__init__.pyi") {
|
|
||||||
path.parent()?
|
|
||||||
} else {
|
|
||||||
path
|
|
||||||
};
|
|
||||||
|
|
||||||
let name = if let Some(parent) = path.parent() {
|
|
||||||
let mut name = String::with_capacity(path.as_str().len());
|
|
||||||
|
|
||||||
for component in parent.components() {
|
|
||||||
name.push_str(component.as_os_str().to_str()?);
|
|
||||||
name.push('.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// SAFETY: Unwrap is safe here or `parent` would have returned `None`.
|
|
||||||
name.push_str(path.file_stem().unwrap());
|
|
||||||
|
|
||||||
smol_str::SmolStr::from(name)
|
|
||||||
} else {
|
|
||||||
smol_str::SmolStr::new(path.file_stem()?)
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(Self(name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for ModuleName {
|
|
||||||
type Target = str;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
self.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq<str> for ModuleName {
|
|
||||||
fn eq(&self, other: &str) -> bool {
|
|
||||||
self.as_str() == other
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq<ModuleName> for str {
|
|
||||||
fn eq(&self, other: &ModuleName) -> bool {
|
|
||||||
self == other.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for ModuleName {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.write_str(&self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Representation of a Python module.
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
|
||||||
pub struct Module {
|
|
||||||
inner: Arc<ModuleInner>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Module {
|
|
||||||
pub(crate) fn new(
|
|
||||||
name: ModuleName,
|
|
||||||
kind: ModuleKind,
|
|
||||||
search_path: ModuleSearchPath,
|
|
||||||
file: VfsFile,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
inner: Arc::new(ModuleInner {
|
|
||||||
name,
|
|
||||||
kind,
|
|
||||||
search_path,
|
|
||||||
file,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The absolute name of the module (e.g. `foo.bar`)
|
|
||||||
pub fn name(&self) -> &ModuleName {
|
|
||||||
&self.inner.name
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The file to the source code that defines this module
|
|
||||||
pub fn file(&self) -> VfsFile {
|
|
||||||
self.inner.file
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The search path from which the module was resolved.
|
|
||||||
pub fn search_path(&self) -> &ModuleSearchPath {
|
|
||||||
&self.inner.search_path
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Determine whether this module is a single-file module or a package
|
|
||||||
pub fn kind(&self) -> ModuleKind {
|
|
||||||
self.inner.kind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for Module {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("Module")
|
|
||||||
.field("name", &self.name())
|
|
||||||
.field("kind", &self.kind())
|
|
||||||
.field("file", &self.file())
|
|
||||||
.field("search_path", &self.search_path())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl salsa::DebugWithDb<dyn Db> for Module {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>, db: &dyn Db) -> std::fmt::Result {
|
|
||||||
f.debug_struct("Module")
|
|
||||||
.field("name", &self.name())
|
|
||||||
.field("kind", &self.kind())
|
|
||||||
.field("file", &self.file().debug(db.upcast()))
|
|
||||||
.field("search_path", &self.search_path())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
|
||||||
struct ModuleInner {
|
|
||||||
name: ModuleName,
|
|
||||||
kind: ModuleKind,
|
|
||||||
search_path: ModuleSearchPath,
|
|
||||||
file: VfsFile,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
pub enum ModuleKind {
|
|
||||||
/// A single-file module (e.g. `foo.py` or `foo.pyi`)
|
|
||||||
Module,
|
|
||||||
|
|
||||||
/// A python package (`foo/__init__.py` or `foo/__init__.pyi`)
|
|
||||||
Package,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A search path in which to search modules.
|
|
||||||
/// Corresponds to a path in [`sys.path`](https://docs.python.org/3/library/sys_path_init.html) at runtime.
|
|
||||||
///
|
|
||||||
/// Cloning a search path is cheap because it's an `Arc`.
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
|
||||||
pub struct ModuleSearchPath {
|
|
||||||
inner: Arc<ModuleSearchPathInner>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleSearchPath {
|
|
||||||
pub fn new<P>(path: P, kind: ModuleSearchPathKind) -> Self
|
|
||||||
where
|
|
||||||
P: Into<VfsPath>,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
inner: Arc::new(ModuleSearchPathInner {
|
|
||||||
path: path.into(),
|
|
||||||
kind,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Determine whether this is a first-party, third-party or standard-library search path
|
|
||||||
pub fn kind(&self) -> ModuleSearchPathKind {
|
|
||||||
self.inner.kind
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the location of the search path on the file system
|
|
||||||
pub fn path(&self) -> &VfsPath {
|
|
||||||
&self.inner.path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for ModuleSearchPath {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("ModuleSearchPath")
|
|
||||||
.field("path", &self.inner.path)
|
|
||||||
.field("kind", &self.kind())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Eq, PartialEq)]
|
|
||||||
struct ModuleSearchPathInner {
|
|
||||||
path: VfsPath,
|
|
||||||
kind: ModuleSearchPathKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Enumeration of the different kinds of search paths type checkers are expected to support.
|
|
||||||
///
|
|
||||||
/// N.B. Although we don't implement `Ord` for this enum, they are ordered in terms of the
|
|
||||||
/// priority that we want to give these modules when resolving them.
|
|
||||||
/// This is roughly [the order given in the typing spec], but typeshed's stubs
|
|
||||||
/// for the standard library are moved higher up to match Python's semantics at runtime.
|
|
||||||
///
|
|
||||||
/// [the order given in the typing spec]: https://typing.readthedocs.io/en/latest/spec/distributing.html#import-resolution-ordering
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
pub enum ModuleSearchPathKind {
|
|
||||||
/// "Extra" paths provided by the user in a config file, env var or CLI flag.
|
|
||||||
/// E.g. mypy's `MYPYPATH` env var, or pyright's `stubPath` configuration setting
|
|
||||||
Extra,
|
|
||||||
|
|
||||||
/// Files in the project we're directly being invoked on
|
|
||||||
FirstParty,
|
|
||||||
|
|
||||||
/// The `stdlib` directory of typeshed (either vendored or custom)
|
|
||||||
StandardLibrary,
|
|
||||||
|
|
||||||
/// Stubs or runtime modules installed in site-packages
|
|
||||||
SitePackagesThirdParty,
|
|
||||||
|
|
||||||
/// Vendored third-party stubs from typeshed
|
|
||||||
VendoredThirdParty,
|
|
||||||
}
|
|
||||||
@@ -1,938 +0,0 @@
|
|||||||
use salsa::DebugWithDb;
|
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use ruff_db::file_system::{FileSystem, FileSystemPath, FileSystemPathBuf};
|
|
||||||
use ruff_db::vfs::{system_path_to_file, vfs_path_to_file, VfsFile, VfsPath};
|
|
||||||
|
|
||||||
use crate::module::{Module, ModuleKind, ModuleName, ModuleSearchPath, ModuleSearchPathKind};
|
|
||||||
use crate::resolver::internal::ModuleResolverSearchPaths;
|
|
||||||
use crate::Db;
|
|
||||||
|
|
||||||
const TYPESHED_STDLIB_DIRECTORY: &str = "stdlib";
|
|
||||||
|
|
||||||
/// Configures the module search paths for the module resolver.
|
|
||||||
///
|
|
||||||
/// Must be called before calling any other module resolution functions.
|
|
||||||
pub fn set_module_resolution_settings(db: &mut dyn Db, config: ModuleResolutionSettings) {
|
|
||||||
// There's no concurrency issue here because we hold a `&mut dyn Db` reference. No other
|
|
||||||
// thread can mutate the `Db` while we're in this call, so using `try_get` to test if
|
|
||||||
// the settings have already been set is safe.
|
|
||||||
if let Some(existing) = ModuleResolverSearchPaths::try_get(db) {
|
|
||||||
existing
|
|
||||||
.set_search_paths(db)
|
|
||||||
.to(config.into_ordered_search_paths());
|
|
||||||
} else {
|
|
||||||
ModuleResolverSearchPaths::new(db, config.into_ordered_search_paths());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resolves a module name to a module.
|
|
||||||
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
|
|
||||||
let interned_name = internal::ModuleNameIngredient::new(db, module_name);
|
|
||||||
|
|
||||||
resolve_module_query(db, interned_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Salsa query that resolves an interned [`ModuleNameIngredient`] to a module.
|
|
||||||
///
|
|
||||||
/// This query should not be called directly. Instead, use [`resolve_module`]. It only exists
|
|
||||||
/// because Salsa requires the module name to be an ingredient.
|
|
||||||
#[salsa::tracked]
|
|
||||||
pub(crate) fn resolve_module_query<'db>(
|
|
||||||
db: &'db dyn Db,
|
|
||||||
module_name: internal::ModuleNameIngredient<'db>,
|
|
||||||
) -> Option<Module> {
|
|
||||||
let _ = tracing::trace_span!("resolve_module", module_name = ?module_name.debug(db)).enter();
|
|
||||||
|
|
||||||
let name = module_name.name(db);
|
|
||||||
|
|
||||||
let (search_path, module_file, kind) = resolve_name(db, name)?;
|
|
||||||
|
|
||||||
let module = Module::new(name.clone(), kind, search_path, module_file);
|
|
||||||
|
|
||||||
Some(module)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resolves the module for the given path.
|
|
||||||
///
|
|
||||||
/// Returns `None` if the path is not a module locatable via `sys.path`.
|
|
||||||
#[tracing::instrument(level = "debug", skip(db))]
|
|
||||||
pub fn path_to_module(db: &dyn Db, path: &VfsPath) -> Option<Module> {
|
|
||||||
// It's not entirely clear on first sight why this method calls `file_to_module` instead of
|
|
||||||
// it being the other way round, considering that the first thing that `file_to_module` does
|
|
||||||
// is to retrieve the file's path.
|
|
||||||
//
|
|
||||||
// The reason is that `file_to_module` is a tracked Salsa query and salsa queries require that
|
|
||||||
// all arguments are Salsa ingredients (something stored in Salsa). `Path`s aren't salsa ingredients but
|
|
||||||
// `VfsFile` is. So what we do here is to retrieve the `path`'s `VfsFile` so that we can make
|
|
||||||
// use of Salsa's caching and invalidation.
|
|
||||||
let file = vfs_path_to_file(db.upcast(), path)?;
|
|
||||||
file_to_module(db, file)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resolves the module for the file with the given id.
|
|
||||||
///
|
|
||||||
/// Returns `None` if the file is not a module locatable via `sys.path`.
|
|
||||||
#[salsa::tracked]
|
|
||||||
#[allow(unused)]
|
|
||||||
pub(crate) fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> {
|
|
||||||
let _ = tracing::trace_span!("file_to_module", file = ?file.debug(db.upcast())).enter();
|
|
||||||
|
|
||||||
let path = file.path(db.upcast());
|
|
||||||
|
|
||||||
let search_paths = module_search_paths(db);
|
|
||||||
|
|
||||||
let relative_path = search_paths
|
|
||||||
.iter()
|
|
||||||
.find_map(|root| match (root.path(), path) {
|
|
||||||
(VfsPath::FileSystem(root_path), VfsPath::FileSystem(path)) => {
|
|
||||||
let relative_path = path.strip_prefix(root_path).ok()?;
|
|
||||||
Some(relative_path)
|
|
||||||
}
|
|
||||||
(VfsPath::Vendored(_), VfsPath::Vendored(_)) => {
|
|
||||||
todo!("Add support for vendored modules")
|
|
||||||
}
|
|
||||||
(VfsPath::Vendored(_), VfsPath::FileSystem(_))
|
|
||||||
| (VfsPath::FileSystem(_), VfsPath::Vendored(_)) => None,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let module_name = ModuleName::from_relative_path(relative_path)?;
|
|
||||||
|
|
||||||
// Resolve the module name to see if Python would resolve the name to the same path.
|
|
||||||
// If it doesn't, then that means that multiple modules have the same name in different
|
|
||||||
// root paths, but that the module corresponding to `path` is in a lower priority search path,
|
|
||||||
// in which case we ignore it.
|
|
||||||
let module = resolve_module(db, module_name)?;
|
|
||||||
|
|
||||||
if file == module.file() {
|
|
||||||
Some(module)
|
|
||||||
} else {
|
|
||||||
// This path is for a module with the same name but with a different precedence. For example:
|
|
||||||
// ```
|
|
||||||
// src/foo.py
|
|
||||||
// src/foo/__init__.py
|
|
||||||
// ```
|
|
||||||
// The module name of `src/foo.py` is `foo`, but the module loaded by Python is `src/foo/__init__.py`.
|
|
||||||
// That means we need to ignore `src/foo.py` even though it resolves to the same module name.
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Configures the search paths that are used to resolve modules.
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
|
||||||
pub struct ModuleResolutionSettings {
|
|
||||||
/// List of user-provided paths that should take first priority in the module resolution.
|
|
||||||
/// Examples in other type checkers are mypy's MYPYPATH environment variable,
|
|
||||||
/// or pyright's stubPath configuration setting.
|
|
||||||
pub extra_paths: Vec<FileSystemPathBuf>,
|
|
||||||
|
|
||||||
/// The root of the workspace, used for finding first-party modules.
|
|
||||||
pub workspace_root: FileSystemPathBuf,
|
|
||||||
|
|
||||||
/// The path to the user's `site-packages` directory, where third-party packages from ``PyPI`` are installed.
|
|
||||||
pub site_packages: Option<FileSystemPathBuf>,
|
|
||||||
|
|
||||||
/// Optional path to standard-library typeshed stubs.
|
|
||||||
/// Currently this has to be a directory that exists on disk.
|
|
||||||
///
|
|
||||||
/// (TODO: fall back to vendored stubs if no custom directory is provided.)
|
|
||||||
pub custom_typeshed: Option<FileSystemPathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleResolutionSettings {
|
|
||||||
/// Implementation of PEP 561's module resolution order
|
|
||||||
/// (with some small, deliberate, differences)
|
|
||||||
fn into_ordered_search_paths(self) -> OrderedSearchPaths {
|
|
||||||
let ModuleResolutionSettings {
|
|
||||||
extra_paths,
|
|
||||||
workspace_root,
|
|
||||||
site_packages,
|
|
||||||
custom_typeshed,
|
|
||||||
} = self;
|
|
||||||
|
|
||||||
let mut paths: Vec<_> = extra_paths
|
|
||||||
.into_iter()
|
|
||||||
.map(|path| ModuleSearchPath::new(path, ModuleSearchPathKind::Extra))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
paths.push(ModuleSearchPath::new(
|
|
||||||
workspace_root,
|
|
||||||
ModuleSearchPathKind::FirstParty,
|
|
||||||
));
|
|
||||||
|
|
||||||
// TODO fallback to vendored typeshed stubs if no custom typeshed directory is provided by the user
|
|
||||||
if let Some(custom_typeshed) = custom_typeshed {
|
|
||||||
paths.push(ModuleSearchPath::new(
|
|
||||||
custom_typeshed.join(TYPESHED_STDLIB_DIRECTORY),
|
|
||||||
ModuleSearchPathKind::StandardLibrary,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO vendor typeshed's third-party stubs as well as the stdlib and fallback to them as a final step
|
|
||||||
if let Some(site_packages) = site_packages {
|
|
||||||
paths.push(ModuleSearchPath::new(
|
|
||||||
site_packages,
|
|
||||||
ModuleSearchPathKind::SitePackagesThirdParty,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
OrderedSearchPaths(paths)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A resolved module resolution order, implementing PEP 561
|
|
||||||
/// (with some small, deliberate differences)
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
||||||
pub(crate) struct OrderedSearchPaths(Vec<ModuleSearchPath>);
|
|
||||||
|
|
||||||
impl Deref for OrderedSearchPaths {
|
|
||||||
type Target = [ModuleSearchPath];
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The singleton methods generated by salsa are all `pub` instead of `pub(crate)` which triggers
|
|
||||||
// `unreachable_pub`. Work around this by creating a module and allow `unreachable_pub` for it.
|
|
||||||
// Salsa also generates uses to `_db` variables for `interned` which triggers `clippy::used_underscore_binding`. Suppress that too
|
|
||||||
// TODO(micha): Contribute a fix for this upstream where the singleton methods have the same visibility as the struct.
|
|
||||||
#[allow(unreachable_pub, clippy::used_underscore_binding)]
|
|
||||||
pub(crate) mod internal {
|
|
||||||
use crate::module::ModuleName;
|
|
||||||
use crate::resolver::OrderedSearchPaths;
|
|
||||||
|
|
||||||
#[salsa::input(singleton)]
|
|
||||||
pub(crate) struct ModuleResolverSearchPaths {
|
|
||||||
#[return_ref]
|
|
||||||
pub(super) search_paths: OrderedSearchPaths,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A thin wrapper around `ModuleName` to make it a Salsa ingredient.
|
|
||||||
///
|
|
||||||
/// This is needed because Salsa requires that all query arguments are salsa ingredients.
|
|
||||||
#[salsa::interned]
|
|
||||||
pub(crate) struct ModuleNameIngredient<'db> {
|
|
||||||
#[return_ref]
|
|
||||||
pub(super) name: ModuleName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn module_search_paths(db: &dyn Db) -> &[ModuleSearchPath] {
|
|
||||||
ModuleResolverSearchPaths::get(db).search_paths(db)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a module name and a list of search paths in which to lookup modules,
|
|
||||||
/// attempt to resolve the module name
|
|
||||||
fn resolve_name(db: &dyn Db, name: &ModuleName) -> Option<(ModuleSearchPath, VfsFile, ModuleKind)> {
|
|
||||||
let search_paths = module_search_paths(db);
|
|
||||||
|
|
||||||
for search_path in search_paths {
|
|
||||||
let mut components = name.components();
|
|
||||||
let module_name = components.next_back()?;
|
|
||||||
|
|
||||||
let VfsPath::FileSystem(fs_search_path) = search_path.path() else {
|
|
||||||
todo!("Vendored search paths are not yet supported");
|
|
||||||
};
|
|
||||||
|
|
||||||
match resolve_package(db.file_system(), fs_search_path, components) {
|
|
||||||
Ok(resolved_package) => {
|
|
||||||
let mut package_path = resolved_package.path;
|
|
||||||
|
|
||||||
package_path.push(module_name);
|
|
||||||
|
|
||||||
// Must be a `__init__.pyi` or `__init__.py` or it isn't a package.
|
|
||||||
let kind = if db.file_system().is_directory(&package_path) {
|
|
||||||
package_path.push("__init__");
|
|
||||||
ModuleKind::Package
|
|
||||||
} else {
|
|
||||||
ModuleKind::Module
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO Implement full https://peps.python.org/pep-0561/#type-checker-module-resolution-order resolution
|
|
||||||
let stub = package_path.with_extension("pyi");
|
|
||||||
|
|
||||||
if let Some(stub) = system_path_to_file(db.upcast(), &stub) {
|
|
||||||
return Some((search_path.clone(), stub, kind));
|
|
||||||
}
|
|
||||||
|
|
||||||
let module = package_path.with_extension("py");
|
|
||||||
|
|
||||||
if let Some(module) = system_path_to_file(db.upcast(), &module) {
|
|
||||||
return Some((search_path.clone(), module, kind));
|
|
||||||
}
|
|
||||||
|
|
||||||
// For regular packages, don't search the next search path. All files of that
|
|
||||||
// package must be in the same location
|
|
||||||
if resolved_package.kind.is_regular_package() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(parent_kind) => {
|
|
||||||
if parent_kind.is_regular_package() {
|
|
||||||
// For regular packages, don't search the next search path.
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn resolve_package<'a, I>(
|
|
||||||
fs: &dyn FileSystem,
|
|
||||||
module_search_path: &FileSystemPath,
|
|
||||||
components: I,
|
|
||||||
) -> Result<ResolvedPackage, PackageKind>
|
|
||||||
where
|
|
||||||
I: Iterator<Item = &'a str>,
|
|
||||||
{
|
|
||||||
let mut package_path = module_search_path.to_path_buf();
|
|
||||||
|
|
||||||
// `true` if inside a folder that is a namespace package (has no `__init__.py`).
|
|
||||||
// Namespace packages are special because they can be spread across multiple search paths.
|
|
||||||
// https://peps.python.org/pep-0420/
|
|
||||||
let mut in_namespace_package = false;
|
|
||||||
|
|
||||||
// `true` if resolving a sub-package. For example, `true` when resolving `bar` of `foo.bar`.
|
|
||||||
let mut in_sub_package = false;
|
|
||||||
|
|
||||||
// For `foo.bar.baz`, test that `foo` and `baz` both contain a `__init__.py`.
|
|
||||||
for folder in components {
|
|
||||||
package_path.push(folder);
|
|
||||||
|
|
||||||
let has_init_py = fs.is_file(&package_path.join("__init__.py"))
|
|
||||||
|| fs.is_file(&package_path.join("__init__.pyi"));
|
|
||||||
|
|
||||||
if has_init_py {
|
|
||||||
in_namespace_package = false;
|
|
||||||
} else if fs.is_directory(&package_path) {
|
|
||||||
// A directory without an `__init__.py` is a namespace package, continue with the next folder.
|
|
||||||
in_namespace_package = true;
|
|
||||||
} else if in_namespace_package {
|
|
||||||
// Package not found but it is part of a namespace package.
|
|
||||||
return Err(PackageKind::Namespace);
|
|
||||||
} else if in_sub_package {
|
|
||||||
// A regular sub package wasn't found.
|
|
||||||
return Err(PackageKind::Regular);
|
|
||||||
} else {
|
|
||||||
// We couldn't find `foo` for `foo.bar.baz`, search the next search path.
|
|
||||||
return Err(PackageKind::Root);
|
|
||||||
}
|
|
||||||
|
|
||||||
in_sub_package = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let kind = if in_namespace_package {
|
|
||||||
PackageKind::Namespace
|
|
||||||
} else if in_sub_package {
|
|
||||||
PackageKind::Regular
|
|
||||||
} else {
|
|
||||||
PackageKind::Root
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(ResolvedPackage {
|
|
||||||
kind,
|
|
||||||
path: package_path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct ResolvedPackage {
|
|
||||||
path: FileSystemPathBuf,
|
|
||||||
kind: PackageKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
||||||
enum PackageKind {
|
|
||||||
/// A root package or module. E.g. `foo` in `foo.bar.baz` or just `foo`.
|
|
||||||
Root,
|
|
||||||
|
|
||||||
/// A regular sub-package where the parent contains an `__init__.py`.
|
|
||||||
///
|
|
||||||
/// For example, `bar` in `foo.bar` when the `foo` directory contains an `__init__.py`.
|
|
||||||
Regular,
|
|
||||||
|
|
||||||
/// A sub-package in a namespace package. A namespace package is a package without an `__init__.py`.
|
|
||||||
///
|
|
||||||
/// For example, `bar` in `foo.bar` if the `foo` directory contains no `__init__.py`.
|
|
||||||
Namespace,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PackageKind {
|
|
||||||
const fn is_regular_package(self) -> bool {
|
|
||||||
matches!(self, PackageKind::Regular)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use ruff_db::file_system::{FileSystemPath, FileSystemPathBuf};
|
|
||||||
use ruff_db::vfs::{system_path_to_file, VfsFile, VfsPath};
|
|
||||||
|
|
||||||
use crate::db::tests::TestDb;
|
|
||||||
use crate::module::{ModuleKind, ModuleName};
|
|
||||||
|
|
||||||
use super::{
|
|
||||||
path_to_module, resolve_module, set_module_resolution_settings, ModuleResolutionSettings,
|
|
||||||
TYPESHED_STDLIB_DIRECTORY,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TestCase {
|
|
||||||
db: TestDb,
|
|
||||||
|
|
||||||
src: FileSystemPathBuf,
|
|
||||||
custom_typeshed: FileSystemPathBuf,
|
|
||||||
site_packages: FileSystemPathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_resolver() -> std::io::Result<TestCase> {
|
|
||||||
let mut db = TestDb::new();
|
|
||||||
|
|
||||||
let src = FileSystemPath::new("src").to_path_buf();
|
|
||||||
let site_packages = FileSystemPath::new("site_packages").to_path_buf();
|
|
||||||
let custom_typeshed = FileSystemPath::new("typeshed").to_path_buf();
|
|
||||||
|
|
||||||
let fs = db.memory_file_system();
|
|
||||||
|
|
||||||
fs.create_directory_all(&src)?;
|
|
||||||
fs.create_directory_all(&site_packages)?;
|
|
||||||
fs.create_directory_all(&custom_typeshed)?;
|
|
||||||
|
|
||||||
let settings = ModuleResolutionSettings {
|
|
||||||
extra_paths: vec![],
|
|
||||||
workspace_root: src.clone(),
|
|
||||||
site_packages: Some(site_packages.clone()),
|
|
||||||
custom_typeshed: Some(custom_typeshed.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
set_module_resolution_settings(&mut db, settings);
|
|
||||||
|
|
||||||
Ok(TestCase {
|
|
||||||
db,
|
|
||||||
src,
|
|
||||||
custom_typeshed,
|
|
||||||
site_packages,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn first_party_module() -> anyhow::Result<()> {
|
|
||||||
let TestCase { db, src, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_module_name = ModuleName::new_static("foo").unwrap();
|
|
||||||
let foo_path = src.join("foo.py");
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_file(&foo_path, "print('Hello, world!')")?;
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, foo_module_name.clone()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(&foo_module),
|
|
||||||
resolve_module(&db, foo_module_name.clone()).as_ref()
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!("foo", foo_module.name());
|
|
||||||
assert_eq!(&src, foo_module.search_path().path());
|
|
||||||
assert_eq!(ModuleKind::Module, foo_module.kind());
|
|
||||||
assert_eq!(&foo_path, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(foo_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_path))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn stdlib() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
db,
|
|
||||||
custom_typeshed,
|
|
||||||
..
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
let stdlib_dir = custom_typeshed.join(TYPESHED_STDLIB_DIRECTORY);
|
|
||||||
let functools_path = stdlib_dir.join("functools.py");
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_file(&functools_path, "def update_wrapper(): ...")?;
|
|
||||||
|
|
||||||
let functools_module_name = ModuleName::new_static("functools").unwrap();
|
|
||||||
let functools_module = resolve_module(&db, functools_module_name.clone()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(&functools_module),
|
|
||||||
resolve_module(&db, functools_module_name).as_ref()
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(&stdlib_dir, functools_module.search_path().path());
|
|
||||||
assert_eq!(ModuleKind::Module, functools_module.kind());
|
|
||||||
assert_eq!(&functools_path.clone(), functools_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(functools_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(functools_path))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn first_party_precedence_over_stdlib() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
db,
|
|
||||||
src,
|
|
||||||
custom_typeshed,
|
|
||||||
..
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
let stdlib_dir = custom_typeshed.join(TYPESHED_STDLIB_DIRECTORY);
|
|
||||||
let stdlib_functools_path = stdlib_dir.join("functools.py");
|
|
||||||
let first_party_functools_path = src.join("functools.py");
|
|
||||||
|
|
||||||
db.memory_file_system().write_files([
|
|
||||||
(&stdlib_functools_path, "def update_wrapper(): ..."),
|
|
||||||
(&first_party_functools_path, "def update_wrapper(): ..."),
|
|
||||||
])?;
|
|
||||||
|
|
||||||
let functools_module_name = ModuleName::new_static("functools").unwrap();
|
|
||||||
let functools_module = resolve_module(&db, functools_module_name.clone()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(&functools_module),
|
|
||||||
resolve_module(&db, functools_module_name).as_ref()
|
|
||||||
);
|
|
||||||
assert_eq!(&src, functools_module.search_path().path());
|
|
||||||
assert_eq!(ModuleKind::Module, functools_module.kind());
|
|
||||||
assert_eq!(
|
|
||||||
&first_party_functools_path.clone(),
|
|
||||||
functools_module.file().path(&db)
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(functools_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(first_party_functools_path))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Port typeshed test case. Porting isn't possible at the moment because the vendored zip
|
|
||||||
// is part of the red knot crate
|
|
||||||
// #[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"));
|
|
||||||
// assert!(!TYPESHED_ZIP_BYTES.is_empty());
|
|
||||||
// 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]
|
|
||||||
fn resolve_package() -> anyhow::Result<()> {
|
|
||||||
let TestCase { src, db, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_dir = src.join("foo");
|
|
||||||
let foo_path = foo_dir.join("__init__.py");
|
|
||||||
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_file(&foo_path, "print('Hello, world!')")?;
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!("foo", foo_module.name());
|
|
||||||
assert_eq!(&src, foo_module.search_path().path());
|
|
||||||
assert_eq!(&foo_path, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(&foo_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_path)).as_ref()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Resolving by directory doesn't resolve to the init file.
|
|
||||||
assert_eq!(None, path_to_module(&db, &VfsPath::FileSystem(foo_dir)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn package_priority_over_module() -> anyhow::Result<()> {
|
|
||||||
let TestCase { db, src, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_dir = src.join("foo");
|
|
||||||
let foo_init = foo_dir.join("__init__.py");
|
|
||||||
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_file(&foo_init, "print('Hello, world!')")?;
|
|
||||||
|
|
||||||
let foo_py = src.join("foo.py");
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_file(&foo_py, "print('Hello, world!')")?;
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(&src, foo_module.search_path().path());
|
|
||||||
assert_eq!(&foo_init, foo_module.file().path(&db));
|
|
||||||
assert_eq!(ModuleKind::Package, foo_module.kind());
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(foo_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_init))
|
|
||||||
);
|
|
||||||
assert_eq!(None, path_to_module(&db, &VfsPath::FileSystem(foo_py)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typing_stub_over_module() -> anyhow::Result<()> {
|
|
||||||
let TestCase { db, src, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_stub = src.join("foo.pyi");
|
|
||||||
let foo_py = src.join("foo.py");
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_files([(&foo_stub, "x: int"), (&foo_py, "print('Hello, world!')")])?;
|
|
||||||
|
|
||||||
let foo = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(&src, foo.search_path().path());
|
|
||||||
assert_eq!(&foo_stub, foo.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(foo),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_stub))
|
|
||||||
);
|
|
||||||
assert_eq!(None, path_to_module(&db, &VfsPath::FileSystem(foo_py)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sub_packages() -> anyhow::Result<()> {
|
|
||||||
let TestCase { db, src, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo = src.join("foo");
|
|
||||||
let bar = foo.join("bar");
|
|
||||||
let baz = bar.join("baz.py");
|
|
||||||
|
|
||||||
db.memory_file_system().write_files([
|
|
||||||
(&foo.join("__init__.py"), ""),
|
|
||||||
(&bar.join("__init__.py"), ""),
|
|
||||||
(&baz, "print('Hello, world!')"),
|
|
||||||
])?;
|
|
||||||
|
|
||||||
let baz_module =
|
|
||||||
resolve_module(&db, ModuleName::new_static("foo.bar.baz").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(&src, baz_module.search_path().path());
|
|
||||||
assert_eq!(&baz, baz_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(baz_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(baz))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn namespace_package() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
db,
|
|
||||||
src,
|
|
||||||
site_packages,
|
|
||||||
..
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
// From [PEP420](https://peps.python.org/pep-0420/#nested-namespace-packages).
|
|
||||||
// But uses `src` for `project1` and `site_packages2` for `project2`.
|
|
||||||
// ```
|
|
||||||
// src
|
|
||||||
// parent
|
|
||||||
// child
|
|
||||||
// one.py
|
|
||||||
// site_packages
|
|
||||||
// parent
|
|
||||||
// child
|
|
||||||
// two.py
|
|
||||||
// ```
|
|
||||||
|
|
||||||
let parent1 = src.join("parent");
|
|
||||||
let child1 = parent1.join("child");
|
|
||||||
let one = child1.join("one.py");
|
|
||||||
|
|
||||||
let parent2 = site_packages.join("parent");
|
|
||||||
let child2 = parent2.join("child");
|
|
||||||
let two = child2.join("two.py");
|
|
||||||
|
|
||||||
db.memory_file_system().write_files([
|
|
||||||
(&one, "print('Hello, world!')"),
|
|
||||||
(&two, "print('Hello, world!')"),
|
|
||||||
])?;
|
|
||||||
|
|
||||||
let one_module =
|
|
||||||
resolve_module(&db, ModuleName::new_static("parent.child.one").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(one_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(one))
|
|
||||||
);
|
|
||||||
|
|
||||||
let two_module =
|
|
||||||
resolve_module(&db, ModuleName::new_static("parent.child.two").unwrap()).unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
Some(two_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(two))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn regular_package_in_namespace_package() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
db,
|
|
||||||
src,
|
|
||||||
site_packages,
|
|
||||||
..
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
// Adopted test case from the [PEP420 examples](https://peps.python.org/pep-0420/#nested-namespace-packages).
|
|
||||||
// The `src/parent/child` package is a regular package. Therefore, `site_packages/parent/child/two.py` should not be resolved.
|
|
||||||
// ```
|
|
||||||
// src
|
|
||||||
// parent
|
|
||||||
// child
|
|
||||||
// one.py
|
|
||||||
// site_packages
|
|
||||||
// parent
|
|
||||||
// child
|
|
||||||
// two.py
|
|
||||||
// ```
|
|
||||||
|
|
||||||
let parent1 = src.join("parent");
|
|
||||||
let child1 = parent1.join("child");
|
|
||||||
let one = child1.join("one.py");
|
|
||||||
|
|
||||||
let parent2 = site_packages.join("parent");
|
|
||||||
let child2 = parent2.join("child");
|
|
||||||
let two = child2.join("two.py");
|
|
||||||
|
|
||||||
db.memory_file_system().write_files([
|
|
||||||
(&child1.join("__init__.py"), "print('Hello, world!')"),
|
|
||||||
(&one, "print('Hello, world!')"),
|
|
||||||
(&two, "print('Hello, world!')"),
|
|
||||||
])?;
|
|
||||||
|
|
||||||
let one_module =
|
|
||||||
resolve_module(&db, ModuleName::new_static("parent.child.one").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(one_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(one))
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
None,
|
|
||||||
resolve_module(&db, ModuleName::new_static("parent.child.two").unwrap())
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn module_search_path_priority() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
db,
|
|
||||||
src,
|
|
||||||
site_packages,
|
|
||||||
..
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_src = src.join("foo.py");
|
|
||||||
let foo_site_packages = site_packages.join("foo.py");
|
|
||||||
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_files([(&foo_src, ""), (&foo_site_packages, "")])?;
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(&src, foo_module.search_path().path());
|
|
||||||
assert_eq!(&foo_src, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(foo_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_src))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
None,
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo_site_packages))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
fn symlink() -> anyhow::Result<()> {
|
|
||||||
let TestCase {
|
|
||||||
mut db,
|
|
||||||
src,
|
|
||||||
site_packages,
|
|
||||||
custom_typeshed,
|
|
||||||
} = create_resolver()?;
|
|
||||||
|
|
||||||
db.with_os_file_system();
|
|
||||||
|
|
||||||
let temp_dir = tempfile::tempdir()?;
|
|
||||||
let root = FileSystemPath::from_std_path(temp_dir.path()).unwrap();
|
|
||||||
|
|
||||||
let src = root.join(src);
|
|
||||||
let site_packages = root.join(site_packages);
|
|
||||||
let custom_typeshed = root.join(custom_typeshed);
|
|
||||||
|
|
||||||
let foo = src.join("foo.py");
|
|
||||||
let bar = src.join("bar.py");
|
|
||||||
|
|
||||||
std::fs::create_dir_all(src.as_std_path())?;
|
|
||||||
std::fs::create_dir_all(site_packages.as_std_path())?;
|
|
||||||
std::fs::create_dir_all(custom_typeshed.as_std_path())?;
|
|
||||||
|
|
||||||
std::fs::write(foo.as_std_path(), "")?;
|
|
||||||
std::os::unix::fs::symlink(foo.as_std_path(), bar.as_std_path())?;
|
|
||||||
|
|
||||||
let settings = ModuleResolutionSettings {
|
|
||||||
extra_paths: vec![],
|
|
||||||
workspace_root: src.clone(),
|
|
||||||
site_packages: Some(site_packages),
|
|
||||||
custom_typeshed: Some(custom_typeshed),
|
|
||||||
};
|
|
||||||
|
|
||||||
set_module_resolution_settings(&mut db, settings);
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
|
|
||||||
let bar_module = resolve_module(&db, ModuleName::new_static("bar").unwrap()).unwrap();
|
|
||||||
|
|
||||||
assert_ne!(foo_module, bar_module);
|
|
||||||
|
|
||||||
assert_eq!(&src, foo_module.search_path().path());
|
|
||||||
assert_eq!(&foo, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
// `foo` and `bar` shouldn't resolve to the same file
|
|
||||||
|
|
||||||
assert_eq!(&src, bar_module.search_path().path());
|
|
||||||
assert_eq!(&bar, bar_module.file().path(&db));
|
|
||||||
assert_eq!(&foo, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
assert_ne!(&foo_module, &bar_module);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Some(foo_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(foo))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
Some(bar_module),
|
|
||||||
path_to_module(&db, &VfsPath::FileSystem(bar))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deleting_an_unrealted_file_doesnt_change_module_resolution() -> anyhow::Result<()> {
|
|
||||||
let TestCase { mut db, src, .. } = create_resolver()?;
|
|
||||||
|
|
||||||
let foo_path = src.join("foo.py");
|
|
||||||
let bar_path = src.join("bar.py");
|
|
||||||
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_files([(&foo_path, "x = 1"), (&bar_path, "y = 2")])?;
|
|
||||||
|
|
||||||
let foo_module_name = ModuleName::new_static("foo").unwrap();
|
|
||||||
let foo_module = resolve_module(&db, foo_module_name.clone()).unwrap();
|
|
||||||
|
|
||||||
let bar = system_path_to_file(&db, &bar_path).expect("bar.py to exist");
|
|
||||||
|
|
||||||
db.clear_salsa_events();
|
|
||||||
|
|
||||||
// Delete `bar.py`
|
|
||||||
db.memory_file_system().remove_file(&bar_path)?;
|
|
||||||
bar.touch(&mut db);
|
|
||||||
|
|
||||||
// Re-query the foo module. The foo module should still be cached because `bar.py` isn't relevant
|
|
||||||
// for resolving `foo`.
|
|
||||||
|
|
||||||
let foo_module2 = resolve_module(&db, foo_module_name);
|
|
||||||
|
|
||||||
assert!(!db
|
|
||||||
.take_salsa_events()
|
|
||||||
.iter()
|
|
||||||
.any(|event| { matches!(event.kind, salsa::EventKind::WillExecute { .. }) }));
|
|
||||||
|
|
||||||
assert_eq!(Some(foo_module), foo_module2);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn adding_a_file_on_which_the_module_resolution_depends_on_invalidates_the_query(
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let TestCase { mut db, src, .. } = create_resolver()?;
|
|
||||||
let foo_path = src.join("foo.py");
|
|
||||||
|
|
||||||
let foo_module_name = ModuleName::new_static("foo").unwrap();
|
|
||||||
assert_eq!(resolve_module(&db, foo_module_name.clone()), None);
|
|
||||||
|
|
||||||
// Now write the foo file
|
|
||||||
db.memory_file_system().write_file(&foo_path, "x = 1")?;
|
|
||||||
VfsFile::touch_path(&mut db, &VfsPath::FileSystem(foo_path.clone()));
|
|
||||||
let foo_file = system_path_to_file(&db, &foo_path).expect("foo.py to exist");
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, foo_module_name).expect("Foo module to resolve");
|
|
||||||
assert_eq!(foo_file, foo_module.file());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn removing_a_file_that_the_module_resolution_depends_on_invalidates_the_query(
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let TestCase { mut db, src, .. } = create_resolver()?;
|
|
||||||
let foo_path = src.join("foo.py");
|
|
||||||
let foo_init_path = src.join("foo/__init__.py");
|
|
||||||
|
|
||||||
db.memory_file_system()
|
|
||||||
.write_files([(&foo_path, "x = 1"), (&foo_init_path, "x = 2")])?;
|
|
||||||
|
|
||||||
let foo_module_name = ModuleName::new_static("foo").unwrap();
|
|
||||||
let foo_module = resolve_module(&db, foo_module_name.clone()).expect("foo module to exist");
|
|
||||||
|
|
||||||
assert_eq!(&foo_init_path, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
// Delete `foo/__init__.py` and the `foo` folder. `foo` should now resolve to `foo.py`
|
|
||||||
db.memory_file_system().remove_file(&foo_init_path)?;
|
|
||||||
db.memory_file_system()
|
|
||||||
.remove_directory(foo_init_path.parent().unwrap())?;
|
|
||||||
VfsFile::touch_path(&mut db, &VfsPath::FileSystem(foo_init_path.clone()));
|
|
||||||
|
|
||||||
let foo_module = resolve_module(&db, foo_module_name).expect("Foo module to resolve");
|
|
||||||
assert_eq!(&foo_path, foo_module.file().path(&db));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
pub(crate) mod versions;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::io::{self, Read};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use ruff_db::vendored::VendoredFileSystem;
|
|
||||||
use ruff_db::vfs::VendoredPath;
|
|
||||||
|
|
||||||
// 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"));
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typeshed_zip_created_at_build_time() {
|
|
||||||
let mut typeshed_zip_archive =
|
|
||||||
zip::ZipArchive::new(io::Cursor::new(TYPESHED_ZIP_BYTES)).unwrap();
|
|
||||||
|
|
||||||
let mut functools_module_stub = typeshed_zip_archive
|
|
||||||
.by_name("stdlib/functools.pyi")
|
|
||||||
.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)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert!(functools_module_stub_source.contains("def update_wrapper("));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typeshed_vfs_consistent_with_vendored_stubs() {
|
|
||||||
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
|
|
||||||
let vendored_typeshed_stubs = VendoredFileSystem::new(TYPESHED_ZIP_BYTES).unwrap();
|
|
||||||
|
|
||||||
let mut empty_iterator = true;
|
|
||||||
for entry in walkdir::WalkDir::new(&vendored_typeshed_dir).min_depth(1) {
|
|
||||||
empty_iterator = false;
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
let absolute_path = entry.path();
|
|
||||||
let file_type = entry.file_type();
|
|
||||||
|
|
||||||
let relative_path = absolute_path
|
|
||||||
.strip_prefix(&vendored_typeshed_dir)
|
|
||||||
.unwrap_or_else(|_| {
|
|
||||||
panic!("Expected {absolute_path:?} to be a child of {vendored_typeshed_dir:?}")
|
|
||||||
});
|
|
||||||
|
|
||||||
let vendored_path = <&VendoredPath>::try_from(relative_path)
|
|
||||||
.unwrap_or_else(|_| panic!("Expected {relative_path:?} to be valid UTF-8"));
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
vendored_typeshed_stubs.exists(vendored_path),
|
|
||||||
"Expected {vendored_path:?} to exist in the `VendoredFileSystem`!
|
|
||||||
|
|
||||||
Vendored file system:
|
|
||||||
|
|
||||||
{vendored_typeshed_stubs:#?}
|
|
||||||
"
|
|
||||||
);
|
|
||||||
|
|
||||||
let vendored_path_kind = vendored_typeshed_stubs
|
|
||||||
.metadata(vendored_path)
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
panic!(
|
|
||||||
"Expected metadata for {vendored_path:?} to be retrievable from the `VendoredFileSystem!
|
|
||||||
|
|
||||||
Vendored file system:
|
|
||||||
|
|
||||||
{vendored_typeshed_stubs:#?}
|
|
||||||
"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.kind();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
vendored_path_kind.is_directory(),
|
|
||||||
file_type.is_dir(),
|
|
||||||
"{vendored_path:?} had type {vendored_path_kind:?}, inconsistent with fs path {relative_path:?}: {file_type:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
!empty_iterator,
|
|
||||||
"Expected there to be at least one file or directory in the vendored typeshed stubs!"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,590 +0,0 @@
|
|||||||
use std::collections::BTreeMap;
|
|
||||||
use std::fmt;
|
|
||||||
use std::num::{NonZeroU16, NonZeroUsize};
|
|
||||||
use std::ops::{RangeFrom, RangeInclusive};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
|
|
||||||
use crate::module::ModuleName;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub struct TypeshedVersionsParseError {
|
|
||||||
line_number: NonZeroU16,
|
|
||||||
reason: TypeshedVersionsParseErrorKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for TypeshedVersionsParseError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
let TypeshedVersionsParseError {
|
|
||||||
line_number,
|
|
||||||
reason,
|
|
||||||
} = self;
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Error while parsing line {line_number} of typeshed's VERSIONS file: {reason}"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for TypeshedVersionsParseError {
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
if let TypeshedVersionsParseErrorKind::IntegerParsingFailure { err, .. } = &self.reason {
|
|
||||||
Some(err)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub enum TypeshedVersionsParseErrorKind {
|
|
||||||
TooManyLines(NonZeroUsize),
|
|
||||||
UnexpectedNumberOfColons,
|
|
||||||
InvalidModuleName(String),
|
|
||||||
UnexpectedNumberOfHyphens,
|
|
||||||
UnexpectedNumberOfPeriods(String),
|
|
||||||
IntegerParsingFailure {
|
|
||||||
version: String,
|
|
||||||
err: std::num::ParseIntError,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for TypeshedVersionsParseErrorKind {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::TooManyLines(num_lines) => write!(
|
|
||||||
f,
|
|
||||||
"File has too many lines ({num_lines}); maximum allowed is {}",
|
|
||||||
NonZeroU16::MAX
|
|
||||||
),
|
|
||||||
Self::UnexpectedNumberOfColons => {
|
|
||||||
f.write_str("Expected every non-comment line to have exactly one colon")
|
|
||||||
}
|
|
||||||
Self::InvalidModuleName(name) => write!(
|
|
||||||
f,
|
|
||||||
"Expected all components of '{name}' to be valid Python identifiers"
|
|
||||||
),
|
|
||||||
Self::UnexpectedNumberOfHyphens => {
|
|
||||||
f.write_str("Expected every non-comment line to have exactly one '-' character")
|
|
||||||
}
|
|
||||||
Self::UnexpectedNumberOfPeriods(format) => write!(
|
|
||||||
f,
|
|
||||||
"Expected all versions to be in the form {{MAJOR}}.{{MINOR}}; got '{format}'"
|
|
||||||
),
|
|
||||||
Self::IntegerParsingFailure { version, err } => write!(
|
|
||||||
f,
|
|
||||||
"Failed to convert '{version}' to a pair of integers due to {err}",
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub struct TypeshedVersions(FxHashMap<ModuleName, PyVersionRange>);
|
|
||||||
|
|
||||||
impl TypeshedVersions {
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.0.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn contains_module(&self, module_name: &ModuleName) -> bool {
|
|
||||||
self.0.contains_key(module_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn module_exists_on_version(
|
|
||||||
&self,
|
|
||||||
module: ModuleName,
|
|
||||||
version: impl Into<PyVersion>,
|
|
||||||
) -> bool {
|
|
||||||
let version = version.into();
|
|
||||||
let mut module: Option<ModuleName> = Some(module);
|
|
||||||
while let Some(module_to_try) = module {
|
|
||||||
if let Some(range) = self.0.get(&module_to_try) {
|
|
||||||
return range.contains(version);
|
|
||||||
}
|
|
||||||
module = module_to_try.parent();
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for TypeshedVersions {
|
|
||||||
type Err = TypeshedVersionsParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let mut map = FxHashMap::default();
|
|
||||||
|
|
||||||
for (line_index, line) in s.lines().enumerate() {
|
|
||||||
// humans expect line numbers to be 1-indexed
|
|
||||||
let line_number = NonZeroUsize::new(line_index.saturating_add(1)).unwrap();
|
|
||||||
|
|
||||||
let Ok(line_number) = NonZeroU16::try_from(line_number) else {
|
|
||||||
return Err(TypeshedVersionsParseError {
|
|
||||||
line_number: NonZeroU16::MAX,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::TooManyLines(line_number),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(content) = line.split('#').map(str::trim).next() else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
if content.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut parts = content.split(':').map(str::trim);
|
|
||||||
let (Some(module_name), Some(rest), None) = (parts.next(), parts.next(), parts.next())
|
|
||||||
else {
|
|
||||||
return Err(TypeshedVersionsParseError {
|
|
||||||
line_number,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfColons,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(module_name) = ModuleName::new(module_name) else {
|
|
||||||
return Err(TypeshedVersionsParseError {
|
|
||||||
line_number,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::InvalidModuleName(
|
|
||||||
module_name.to_string(),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
match PyVersionRange::from_str(rest) {
|
|
||||||
Ok(version) => map.insert(module_name, version),
|
|
||||||
Err(reason) => {
|
|
||||||
return Err(TypeshedVersionsParseError {
|
|
||||||
line_number,
|
|
||||||
reason,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self(map))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for TypeshedVersions {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
let sorted_items: BTreeMap<&ModuleName, &PyVersionRange> = self.0.iter().collect();
|
|
||||||
for (module_name, range) in sorted_items {
|
|
||||||
writeln!(f, "{module_name}: {range}")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
enum PyVersionRange {
|
|
||||||
AvailableFrom(RangeFrom<PyVersion>),
|
|
||||||
AvailableWithin(RangeInclusive<PyVersion>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PyVersionRange {
|
|
||||||
fn contains(&self, version: PyVersion) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::AvailableFrom(inner) => inner.contains(&version),
|
|
||||||
Self::AvailableWithin(inner) => inner.contains(&version),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PyVersionRange {
|
|
||||||
type Err = TypeshedVersionsParseErrorKind;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let mut parts = s.split('-').map(str::trim);
|
|
||||||
match (parts.next(), parts.next(), parts.next()) {
|
|
||||||
(Some(lower), Some(""), None) => Ok(Self::AvailableFrom((lower.parse()?)..)),
|
|
||||||
(Some(lower), Some(upper), None) => {
|
|
||||||
Ok(Self::AvailableWithin((lower.parse()?)..=(upper.parse()?)))
|
|
||||||
}
|
|
||||||
_ => Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for PyVersionRange {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::AvailableFrom(range_from) => write!(f, "{}-", range_from.start),
|
|
||||||
Self::AvailableWithin(range_inclusive) => {
|
|
||||||
write!(f, "{}-{}", range_inclusive.start(), range_inclusive.end())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
||||||
pub struct PyVersion {
|
|
||||||
major: u8,
|
|
||||||
minor: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PyVersion {
|
|
||||||
type Err = TypeshedVersionsParseErrorKind;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let mut parts = s.split('.').map(str::trim);
|
|
||||||
let (Some(major), Some(minor), None) = (parts.next(), parts.next(), parts.next()) else {
|
|
||||||
return Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
|
||||||
s.to_string(),
|
|
||||||
));
|
|
||||||
};
|
|
||||||
let major = match u8::from_str(major) {
|
|
||||||
Ok(major) => major,
|
|
||||||
Err(err) => {
|
|
||||||
return Err(TypeshedVersionsParseErrorKind::IntegerParsingFailure {
|
|
||||||
version: s.to_string(),
|
|
||||||
err,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let minor = match u8::from_str(minor) {
|
|
||||||
Ok(minor) => minor,
|
|
||||||
Err(err) => {
|
|
||||||
return Err(TypeshedVersionsParseErrorKind::IntegerParsingFailure {
|
|
||||||
version: s.to_string(),
|
|
||||||
err,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(Self { major, minor })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for PyVersion {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
let PyVersion { major, minor } = self;
|
|
||||||
write!(f, "{major}.{minor}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: unify with the PythonVersion enum in the linter/formatter crates?
|
|
||||||
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
|
|
||||||
pub enum SupportedPyVersion {
|
|
||||||
Py37,
|
|
||||||
#[default]
|
|
||||||
Py38,
|
|
||||||
Py39,
|
|
||||||
Py310,
|
|
||||||
Py311,
|
|
||||||
Py312,
|
|
||||||
Py313,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SupportedPyVersion> for PyVersion {
|
|
||||||
fn from(value: SupportedPyVersion) -> Self {
|
|
||||||
match value {
|
|
||||||
SupportedPyVersion::Py37 => PyVersion { major: 3, minor: 7 },
|
|
||||||
SupportedPyVersion::Py38 => PyVersion { major: 3, minor: 8 },
|
|
||||||
SupportedPyVersion::Py39 => PyVersion { major: 3, minor: 9 },
|
|
||||||
SupportedPyVersion::Py310 => PyVersion {
|
|
||||||
major: 3,
|
|
||||||
minor: 10,
|
|
||||||
},
|
|
||||||
SupportedPyVersion::Py311 => PyVersion {
|
|
||||||
major: 3,
|
|
||||||
minor: 11,
|
|
||||||
},
|
|
||||||
SupportedPyVersion::Py312 => PyVersion {
|
|
||||||
major: 3,
|
|
||||||
minor: 12,
|
|
||||||
},
|
|
||||||
SupportedPyVersion::Py313 => PyVersion {
|
|
||||||
major: 3,
|
|
||||||
minor: 13,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::num::{IntErrorKind, NonZeroU16};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
use insta::assert_snapshot;
|
|
||||||
|
|
||||||
const TYPESHED_STDLIB_DIR: &str = "stdlib";
|
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
|
||||||
const ONE: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(1) };
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn can_parse_vendored_versions_file() {
|
|
||||||
let versions_data = include_str!(concat!(
|
|
||||||
env!("CARGO_MANIFEST_DIR"),
|
|
||||||
"/vendor/typeshed/stdlib/VERSIONS"
|
|
||||||
));
|
|
||||||
|
|
||||||
let versions = TypeshedVersions::from_str(versions_data).unwrap();
|
|
||||||
assert!(versions.len() > 100);
|
|
||||||
assert!(versions.len() < 1000);
|
|
||||||
|
|
||||||
let asyncio = ModuleName::new_static("asyncio").unwrap();
|
|
||||||
let asyncio_staggered = ModuleName::new_static("asyncio.staggered").unwrap();
|
|
||||||
let audioop = ModuleName::new_static("audioop").unwrap();
|
|
||||||
|
|
||||||
assert!(versions.contains_module(&asyncio));
|
|
||||||
assert!(versions.module_exists_on_version(asyncio, SupportedPyVersion::Py310));
|
|
||||||
|
|
||||||
assert!(versions.contains_module(&asyncio_staggered));
|
|
||||||
assert!(
|
|
||||||
versions.module_exists_on_version(asyncio_staggered.clone(), SupportedPyVersion::Py38)
|
|
||||||
);
|
|
||||||
assert!(!versions.module_exists_on_version(asyncio_staggered, SupportedPyVersion::Py37));
|
|
||||||
|
|
||||||
assert!(versions.contains_module(&audioop));
|
|
||||||
assert!(versions.module_exists_on_version(audioop.clone(), SupportedPyVersion::Py312));
|
|
||||||
assert!(!versions.module_exists_on_version(audioop, SupportedPyVersion::Py313));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typeshed_versions_consistent_with_vendored_stubs() {
|
|
||||||
const VERSIONS_DATA: &str = include_str!("../../vendor/typeshed/stdlib/VERSIONS");
|
|
||||||
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
|
|
||||||
let vendored_typeshed_versions = TypeshedVersions::from_str(VERSIONS_DATA).unwrap();
|
|
||||||
|
|
||||||
let mut empty_iterator = true;
|
|
||||||
|
|
||||||
let stdlib_stubs_path = vendored_typeshed_dir.join(TYPESHED_STDLIB_DIR);
|
|
||||||
|
|
||||||
for entry in std::fs::read_dir(&stdlib_stubs_path).unwrap() {
|
|
||||||
empty_iterator = false;
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
let absolute_path = entry.path();
|
|
||||||
|
|
||||||
let relative_path = absolute_path
|
|
||||||
.strip_prefix(&stdlib_stubs_path)
|
|
||||||
.unwrap_or_else(|_| panic!("Expected path to be a child of {stdlib_stubs_path:?} but found {absolute_path:?}"));
|
|
||||||
|
|
||||||
let relative_path_str = relative_path.as_os_str().to_str().unwrap_or_else(|| {
|
|
||||||
panic!("Expected all typeshed paths to be valid UTF-8; got {relative_path:?}")
|
|
||||||
});
|
|
||||||
if relative_path_str == "VERSIONS" {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let top_level_module = if let Some(extension) = relative_path.extension() {
|
|
||||||
// It was a file; strip off the file extension to get the module name:
|
|
||||||
let extension = extension
|
|
||||||
.to_str()
|
|
||||||
.unwrap_or_else(||panic!("Expected all file extensions to be UTF-8; was not true for {relative_path:?}"));
|
|
||||||
|
|
||||||
relative_path_str
|
|
||||||
.strip_suffix(extension)
|
|
||||||
.and_then(|string| string.strip_suffix('.')).unwrap_or_else(|| {
|
|
||||||
panic!("Expected path {relative_path_str:?} to end with computed extension {extension:?}")
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// It was a directory; no need to do anything to get the module name
|
|
||||||
relative_path_str
|
|
||||||
};
|
|
||||||
|
|
||||||
let top_level_module = ModuleName::new(top_level_module)
|
|
||||||
.unwrap_or_else(|| panic!("{top_level_module:?} was not a valid module name!"));
|
|
||||||
|
|
||||||
assert!(vendored_typeshed_versions.contains_module(&top_level_module));
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
!empty_iterator,
|
|
||||||
"Expected there to be at least one file or directory in the vendored typeshed stubs"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn can_parse_mock_versions_file() {
|
|
||||||
const VERSIONS: &str = "\
|
|
||||||
# a comment
|
|
||||||
# some more comment
|
|
||||||
# yet more comment
|
|
||||||
|
|
||||||
|
|
||||||
# and some more comment
|
|
||||||
|
|
||||||
bar: 2.7-3.10
|
|
||||||
|
|
||||||
# more comment
|
|
||||||
bar.baz: 3.1-3.9
|
|
||||||
foo: 3.8- # trailing comment
|
|
||||||
";
|
|
||||||
let parsed_versions = TypeshedVersions::from_str(VERSIONS).unwrap();
|
|
||||||
assert_eq!(parsed_versions.len(), 3);
|
|
||||||
assert_snapshot!(parsed_versions.to_string(), @r###"
|
|
||||||
bar: 2.7-3.10
|
|
||||||
bar.baz: 3.1-3.9
|
|
||||||
foo: 3.8-
|
|
||||||
"###
|
|
||||||
);
|
|
||||||
|
|
||||||
let foo = ModuleName::new_static("foo").unwrap();
|
|
||||||
let bar = ModuleName::new_static("bar").unwrap();
|
|
||||||
let bar_baz = ModuleName::new_static("bar.baz").unwrap();
|
|
||||||
let spam = ModuleName::new_static("spam").unwrap();
|
|
||||||
|
|
||||||
assert!(parsed_versions.contains_module(&foo));
|
|
||||||
assert!(!parsed_versions.module_exists_on_version(foo.clone(), SupportedPyVersion::Py37));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(foo.clone(), SupportedPyVersion::Py38));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(foo, SupportedPyVersion::Py311));
|
|
||||||
|
|
||||||
assert!(parsed_versions.contains_module(&bar));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(bar.clone(), SupportedPyVersion::Py37));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(bar.clone(), SupportedPyVersion::Py310));
|
|
||||||
assert!(!parsed_versions.module_exists_on_version(bar, SupportedPyVersion::Py311));
|
|
||||||
|
|
||||||
assert!(parsed_versions.contains_module(&bar_baz));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(bar_baz.clone(), SupportedPyVersion::Py37));
|
|
||||||
assert!(parsed_versions.module_exists_on_version(bar_baz.clone(), SupportedPyVersion::Py39));
|
|
||||||
assert!(!parsed_versions.module_exists_on_version(bar_baz, SupportedPyVersion::Py310));
|
|
||||||
|
|
||||||
assert!(!parsed_versions.contains_module(&spam));
|
|
||||||
assert!(!parsed_versions.module_exists_on_version(spam.clone(), SupportedPyVersion::Py37));
|
|
||||||
assert!(!parsed_versions.module_exists_on_version(spam, SupportedPyVersion::Py313));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_huge_versions_file() {
|
|
||||||
let offset = 100;
|
|
||||||
let too_many = u16::MAX as usize + offset;
|
|
||||||
|
|
||||||
let mut massive_versions_file = String::new();
|
|
||||||
for i in 0..too_many {
|
|
||||||
massive_versions_file.push_str(&format!("x{i}: 3.8-\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str(&massive_versions_file),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: NonZeroU16::MAX,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::TooManyLines(
|
|
||||||
NonZeroUsize::new(too_many + 1 - offset).unwrap()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_typeshed_versions_bad_colon_number() {
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo 3.7"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfColons
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo:: 3.7"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfColons
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_typeshed_versions_non_identifier_modules() {
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("not!an!identifier!: 3.7"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::InvalidModuleName(
|
|
||||||
"not!an!identifier!".to_string()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("(also_not).(an_identifier): 3.7"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::InvalidModuleName(
|
|
||||||
"(also_not).(an_identifier)".to_string()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_typeshed_versions_bad_hyphen_number() {
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 3.8"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 3.8--"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 3.8--3.9"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_typeshed_versions_bad_period_number() {
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 38-"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods("38".to_string())
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 3..8-"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
|
||||||
"3..8".to_string()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TypeshedVersions::from_str("foo: 3.8-3..11"),
|
|
||||||
Err(TypeshedVersionsParseError {
|
|
||||||
line_number: ONE,
|
|
||||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
|
||||||
"3..11".to_string()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_typeshed_versions_non_digits() {
|
|
||||||
let err = TypeshedVersions::from_str("foo: 1.two-").unwrap_err();
|
|
||||||
assert_eq!(err.line_number, ONE);
|
|
||||||
let TypeshedVersionsParseErrorKind::IntegerParsingFailure { version, err } = err.reason
|
|
||||||
else {
|
|
||||||
panic!()
|
|
||||||
};
|
|
||||||
assert_eq!(version, "1.two".to_string());
|
|
||||||
assert_eq!(*err.kind(), IntErrorKind::InvalidDigit);
|
|
||||||
|
|
||||||
let err = TypeshedVersions::from_str("foo: 3.8-four.9").unwrap_err();
|
|
||||||
assert_eq!(err.line_number, ONE);
|
|
||||||
let TypeshedVersionsParseErrorKind::IntegerParsingFailure { version, err } = err.reason
|
|
||||||
else {
|
|
||||||
panic!()
|
|
||||||
};
|
|
||||||
assert_eq!(version, "four.9".to_string());
|
|
||||||
assert_eq!(*err.kind(), IntErrorKind::InvalidDigit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,237 +0,0 @@
|
|||||||
The "typeshed" project is licensed under the terms of the Apache license, as
|
|
||||||
reproduced below.
|
|
||||||
|
|
||||||
= = = = =
|
|
||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright {yyyy} {name of copyright owner}
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
|
|
||||||
= = = = =
|
|
||||||
|
|
||||||
Parts of typeshed are licensed under different licenses (like the MIT
|
|
||||||
license), reproduced below.
|
|
||||||
|
|
||||||
= = = = =
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2015 Jukka Lehtosalo and contributors
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
copy of this software and associated documentation files (the "Software"),
|
|
||||||
to deal in the Software without restriction, including without limitation
|
|
||||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
Software is furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
= = = = =
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
# typeshed
|
|
||||||
|
|
||||||
[](https://github.com/python/typeshed/actions/workflows/tests.yml)
|
|
||||||
[](https://gitter.im/python/typing?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
||||||
[](https://github.com/python/typeshed/blob/main/CONTRIBUTING.md)
|
|
||||||
|
|
||||||
## About
|
|
||||||
|
|
||||||
Typeshed contains external type annotations for the Python standard library
|
|
||||||
and Python builtins, as well as third party packages as contributed by
|
|
||||||
people external to those projects.
|
|
||||||
|
|
||||||
This data can e.g. be used for static analysis, type checking, type inference,
|
|
||||||
and autocompletion.
|
|
||||||
|
|
||||||
For information on how to use typeshed, read below. Information for
|
|
||||||
contributors can be found in [CONTRIBUTING.md](CONTRIBUTING.md). **Please read
|
|
||||||
it before submitting pull requests; do not report issues with annotations to
|
|
||||||
the project the stubs are for, but instead report them here to typeshed.**
|
|
||||||
|
|
||||||
Further documentation on stub files, typeshed, and Python's typing system in
|
|
||||||
general, can also be found at https://typing.readthedocs.io/en/latest/.
|
|
||||||
|
|
||||||
Typeshed supports Python versions 3.8 and up.
|
|
||||||
|
|
||||||
## Using
|
|
||||||
|
|
||||||
If you're just using a type checker ([mypy](https://github.com/python/mypy/),
|
|
||||||
[pyright](https://github.com/microsoft/pyright),
|
|
||||||
[pytype](https://github.com/google/pytype/), PyCharm, ...), as opposed to
|
|
||||||
developing it, you don't need to interact with the typeshed repo at
|
|
||||||
all: a copy of standard library part of typeshed is bundled with type checkers.
|
|
||||||
And type stubs for third party packages and modules you are using can
|
|
||||||
be installed from PyPI. For example, if you are using `html5lib` and `requests`,
|
|
||||||
you can install the type stubs using
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ pip install types-html5lib types-requests
|
|
||||||
```
|
|
||||||
|
|
||||||
These PyPI packages follow [PEP 561](http://www.python.org/dev/peps/pep-0561/)
|
|
||||||
and are automatically released (up to once a day) by
|
|
||||||
[typeshed internal machinery](https://github.com/typeshed-internal/stub_uploader).
|
|
||||||
|
|
||||||
Type checkers should be able to use these stub packages when installed. For more
|
|
||||||
details, see the documentation for your type checker.
|
|
||||||
|
|
||||||
### Package versioning for third-party stubs
|
|
||||||
|
|
||||||
Version numbers of third-party stub packages consist of at least four parts.
|
|
||||||
All parts of the stub version, except for the last part, correspond to the
|
|
||||||
version of the runtime package being stubbed. For example, if the `types-foo`
|
|
||||||
package has version `1.2.0.20240309`, this guarantees that the `types-foo` package
|
|
||||||
contains stubs targeted against `foo==1.2.*` and tested against the latest
|
|
||||||
version of `foo` matching that specifier. In this example, the final element
|
|
||||||
of the version number (20240309) indicates that the stub package was pushed on
|
|
||||||
March 9, 2024.
|
|
||||||
|
|
||||||
At typeshed, we try to keep breaking changes to a minimum. However, due to the
|
|
||||||
nature of stubs, any version bump can introduce changes that might make your
|
|
||||||
code fail to type check.
|
|
||||||
|
|
||||||
There are several strategies available for specifying the version of a stubs
|
|
||||||
package you're using, each with its own tradeoffs:
|
|
||||||
|
|
||||||
1. Use the same bounds that you use for the package being stubbed. For example,
|
|
||||||
if you use `requests>=2.30.0,<2.32`, you can use
|
|
||||||
`types-requests>=2.30.0,<2.32`. This ensures that the stubs are compatible
|
|
||||||
with the package you are using, but it carries a small risk of breaking
|
|
||||||
type checking due to changes in the stubs.
|
|
||||||
|
|
||||||
Another risk of this strategy is that stubs often lag behind
|
|
||||||
the package being stubbed. You might want to force the package being stubbed
|
|
||||||
to a certain minimum version because it fixes a critical bug, but if
|
|
||||||
correspondingly updated stubs have not been released, your type
|
|
||||||
checking results may not be fully accurate.
|
|
||||||
2. Pin the stubs to a known good version and update the pin from time to time
|
|
||||||
(either manually, or using a tool such as dependabot or renovate).
|
|
||||||
|
|
||||||
For example, if you use `types-requests==2.31.0.1`, you can have confidence
|
|
||||||
that upgrading dependencies will not break type checking. However, you will
|
|
||||||
miss out on improvements in the stubs that could potentially improve type
|
|
||||||
checking until you update the pin. This strategy also has the risk that the
|
|
||||||
stubs you are using might become incompatible with the package being stubbed.
|
|
||||||
3. Don't pin the stubs. This is the option that demands the least work from
|
|
||||||
you when it comes to updating version pins, and has the advantage that you
|
|
||||||
will automatically benefit from improved stubs whenever a new version of the
|
|
||||||
stubs package is released. However, it carries the risk that the stubs
|
|
||||||
become incompatible with the package being stubbed.
|
|
||||||
|
|
||||||
For example, if a new major version of the package is released, there's a
|
|
||||||
chance the stubs might be updated to reflect the new version of the runtime
|
|
||||||
package before you update the package being stubbed.
|
|
||||||
|
|
||||||
You can also switch between the different strategies as needed. For example,
|
|
||||||
you could default to strategy (1), but fall back to strategy (2) when
|
|
||||||
a problem arises that can't easily be fixed.
|
|
||||||
|
|
||||||
### The `_typeshed` package
|
|
||||||
|
|
||||||
typeshed includes a package `_typeshed` as part of the standard library.
|
|
||||||
This package and its submodules contain utility types, but are not
|
|
||||||
available at runtime. For more information about how to use this package,
|
|
||||||
[see the `stdlib/_typeshed` directory](https://github.com/python/typeshed/tree/main/stdlib/_typeshed).
|
|
||||||
|
|
||||||
## Discussion
|
|
||||||
|
|
||||||
If you've run into behavior in the type checker that suggests the type
|
|
||||||
stubs for a given library are incorrect or incomplete,
|
|
||||||
we want to hear from you!
|
|
||||||
|
|
||||||
Our main forum for discussion is the project's [GitHub issue
|
|
||||||
tracker](https://github.com/python/typeshed/issues). This is the right
|
|
||||||
place to start a discussion of any of the above or most any other
|
|
||||||
topic concerning the project.
|
|
||||||
|
|
||||||
If you have general questions about typing with Python, or you need
|
|
||||||
a review of your type annotations or stubs outside of typeshed, head over to
|
|
||||||
[our discussion forum](https://github.com/python/typing/discussions).
|
|
||||||
For less formal discussion, try the typing chat room on
|
|
||||||
[gitter.im](https://gitter.im/python/typing). Some typeshed maintainers
|
|
||||||
are almost always present; feel free to find us there and we're happy
|
|
||||||
to chat. Substantive technical discussion will be directed to the
|
|
||||||
issue tracker.
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
114409d49b43ba62a179ebb856fa70a5161f751e
|
|
||||||
@@ -1,310 +0,0 @@
|
|||||||
# The structure of this file is as follows:
|
|
||||||
# - Blank lines and comments starting with `#` are ignored.
|
|
||||||
# - Lines contain the name of a module, followed by a colon,
|
|
||||||
# a space, and a version range (for example: `symbol: 3.0-3.9`).
|
|
||||||
#
|
|
||||||
# Version ranges may be of the form "X.Y-A.B" or "X.Y-". The
|
|
||||||
# first form means that a module was introduced in version X.Y and last
|
|
||||||
# available in version A.B. The second form means that the module was
|
|
||||||
# introduced in version X.Y and is still available in the latest
|
|
||||||
# version of Python.
|
|
||||||
#
|
|
||||||
# If a submodule is not listed separately, it has the same lifetime as
|
|
||||||
# its parent module.
|
|
||||||
#
|
|
||||||
# Python versions before 3.0 are ignored, so any module that was already
|
|
||||||
# present in 3.0 will have "3.0" as its minimum version. Version ranges
|
|
||||||
# for unsupported versions of Python 3 are generally accurate but we do
|
|
||||||
# not guarantee their correctness.
|
|
||||||
|
|
||||||
__future__: 3.0-
|
|
||||||
__main__: 3.0-
|
|
||||||
_ast: 3.0-
|
|
||||||
_bisect: 3.0-
|
|
||||||
_bootlocale: 3.4-3.9
|
|
||||||
_codecs: 3.0-
|
|
||||||
_collections_abc: 3.3-
|
|
||||||
_compat_pickle: 3.1-
|
|
||||||
_compression: 3.5-
|
|
||||||
_csv: 3.0-
|
|
||||||
_ctypes: 3.0-
|
|
||||||
_curses: 3.0-
|
|
||||||
_decimal: 3.3-
|
|
||||||
_dummy_thread: 3.0-3.8
|
|
||||||
_dummy_threading: 3.0-3.8
|
|
||||||
_heapq: 3.0-
|
|
||||||
_imp: 3.0-
|
|
||||||
_json: 3.0-
|
|
||||||
_locale: 3.0-
|
|
||||||
_lsprof: 3.0-
|
|
||||||
_markupbase: 3.0-
|
|
||||||
_msi: 3.0-
|
|
||||||
_operator: 3.4-
|
|
||||||
_osx_support: 3.0-
|
|
||||||
_posixsubprocess: 3.2-
|
|
||||||
_py_abc: 3.7-
|
|
||||||
_pydecimal: 3.5-
|
|
||||||
_random: 3.0-
|
|
||||||
_sitebuiltins: 3.4-
|
|
||||||
_socket: 3.0- # present in 3.0 at runtime, but not in typeshed
|
|
||||||
_stat: 3.4-
|
|
||||||
_thread: 3.0-
|
|
||||||
_threading_local: 3.0-
|
|
||||||
_tkinter: 3.0-
|
|
||||||
_tracemalloc: 3.4-
|
|
||||||
_typeshed: 3.0- # not present at runtime, only for type checking
|
|
||||||
_warnings: 3.0-
|
|
||||||
_weakref: 3.0-
|
|
||||||
_weakrefset: 3.0-
|
|
||||||
_winapi: 3.3-
|
|
||||||
abc: 3.0-
|
|
||||||
aifc: 3.0-3.12
|
|
||||||
antigravity: 3.0-
|
|
||||||
argparse: 3.0-
|
|
||||||
array: 3.0-
|
|
||||||
ast: 3.0-
|
|
||||||
asynchat: 3.0-3.11
|
|
||||||
asyncio: 3.4-
|
|
||||||
asyncio.exceptions: 3.8-
|
|
||||||
asyncio.format_helpers: 3.7-
|
|
||||||
asyncio.mixins: 3.10-
|
|
||||||
asyncio.runners: 3.7-
|
|
||||||
asyncio.staggered: 3.8-
|
|
||||||
asyncio.taskgroups: 3.11-
|
|
||||||
asyncio.threads: 3.9-
|
|
||||||
asyncio.timeouts: 3.11-
|
|
||||||
asyncio.trsock: 3.8-
|
|
||||||
asyncore: 3.0-3.11
|
|
||||||
atexit: 3.0-
|
|
||||||
audioop: 3.0-3.12
|
|
||||||
base64: 3.0-
|
|
||||||
bdb: 3.0-
|
|
||||||
binascii: 3.0-
|
|
||||||
binhex: 3.0-3.10
|
|
||||||
bisect: 3.0-
|
|
||||||
builtins: 3.0-
|
|
||||||
bz2: 3.0-
|
|
||||||
cProfile: 3.0-
|
|
||||||
calendar: 3.0-
|
|
||||||
cgi: 3.0-3.12
|
|
||||||
cgitb: 3.0-3.12
|
|
||||||
chunk: 3.0-3.12
|
|
||||||
cmath: 3.0-
|
|
||||||
cmd: 3.0-
|
|
||||||
code: 3.0-
|
|
||||||
codecs: 3.0-
|
|
||||||
codeop: 3.0-
|
|
||||||
collections: 3.0-
|
|
||||||
collections.abc: 3.3-
|
|
||||||
colorsys: 3.0-
|
|
||||||
compileall: 3.0-
|
|
||||||
concurrent: 3.2-
|
|
||||||
configparser: 3.0-
|
|
||||||
contextlib: 3.0-
|
|
||||||
contextvars: 3.7-
|
|
||||||
copy: 3.0-
|
|
||||||
copyreg: 3.0-
|
|
||||||
crypt: 3.0-3.12
|
|
||||||
csv: 3.0-
|
|
||||||
ctypes: 3.0-
|
|
||||||
curses: 3.0-
|
|
||||||
dataclasses: 3.7-
|
|
||||||
datetime: 3.0-
|
|
||||||
dbm: 3.0-
|
|
||||||
decimal: 3.0-
|
|
||||||
difflib: 3.0-
|
|
||||||
dis: 3.0-
|
|
||||||
distutils: 3.0-3.11
|
|
||||||
distutils.command.bdist_msi: 3.0-3.10
|
|
||||||
distutils.command.bdist_wininst: 3.0-3.9
|
|
||||||
doctest: 3.0-
|
|
||||||
dummy_threading: 3.0-3.8
|
|
||||||
email: 3.0-
|
|
||||||
encodings: 3.0-
|
|
||||||
ensurepip: 3.0-
|
|
||||||
enum: 3.4-
|
|
||||||
errno: 3.0-
|
|
||||||
faulthandler: 3.3-
|
|
||||||
fcntl: 3.0-
|
|
||||||
filecmp: 3.0-
|
|
||||||
fileinput: 3.0-
|
|
||||||
fnmatch: 3.0-
|
|
||||||
formatter: 3.0-3.9
|
|
||||||
fractions: 3.0-
|
|
||||||
ftplib: 3.0-
|
|
||||||
functools: 3.0-
|
|
||||||
gc: 3.0-
|
|
||||||
genericpath: 3.0-
|
|
||||||
getopt: 3.0-
|
|
||||||
getpass: 3.0-
|
|
||||||
gettext: 3.0-
|
|
||||||
glob: 3.0-
|
|
||||||
graphlib: 3.9-
|
|
||||||
grp: 3.0-
|
|
||||||
gzip: 3.0-
|
|
||||||
hashlib: 3.0-
|
|
||||||
heapq: 3.0-
|
|
||||||
hmac: 3.0-
|
|
||||||
html: 3.0-
|
|
||||||
http: 3.0-
|
|
||||||
imaplib: 3.0-
|
|
||||||
imghdr: 3.0-3.12
|
|
||||||
imp: 3.0-3.11
|
|
||||||
importlib: 3.0-
|
|
||||||
importlib._abc: 3.10-
|
|
||||||
importlib.metadata: 3.8-
|
|
||||||
importlib.metadata._meta: 3.10-
|
|
||||||
importlib.readers: 3.10-
|
|
||||||
importlib.resources: 3.7-
|
|
||||||
importlib.resources.abc: 3.11-
|
|
||||||
importlib.resources.readers: 3.11-
|
|
||||||
importlib.resources.simple: 3.11-
|
|
||||||
importlib.simple: 3.11-
|
|
||||||
inspect: 3.0-
|
|
||||||
io: 3.0-
|
|
||||||
ipaddress: 3.3-
|
|
||||||
itertools: 3.0-
|
|
||||||
json: 3.0-
|
|
||||||
keyword: 3.0-
|
|
||||||
lib2to3: 3.0-3.12
|
|
||||||
linecache: 3.0-
|
|
||||||
locale: 3.0-
|
|
||||||
logging: 3.0-
|
|
||||||
lzma: 3.3-
|
|
||||||
mailbox: 3.0-
|
|
||||||
mailcap: 3.0-3.12
|
|
||||||
marshal: 3.0-
|
|
||||||
math: 3.0-
|
|
||||||
mimetypes: 3.0-
|
|
||||||
mmap: 3.0-
|
|
||||||
modulefinder: 3.0-
|
|
||||||
msilib: 3.0-3.12
|
|
||||||
msvcrt: 3.0-
|
|
||||||
multiprocessing: 3.0-
|
|
||||||
multiprocessing.resource_tracker: 3.8-
|
|
||||||
multiprocessing.shared_memory: 3.8-
|
|
||||||
netrc: 3.0-
|
|
||||||
nis: 3.0-3.12
|
|
||||||
nntplib: 3.0-3.12
|
|
||||||
nt: 3.0-
|
|
||||||
ntpath: 3.0-
|
|
||||||
nturl2path: 3.0-
|
|
||||||
numbers: 3.0-
|
|
||||||
opcode: 3.0-
|
|
||||||
operator: 3.0-
|
|
||||||
optparse: 3.0-
|
|
||||||
os: 3.0-
|
|
||||||
ossaudiodev: 3.0-3.12
|
|
||||||
parser: 3.0-3.9
|
|
||||||
pathlib: 3.4-
|
|
||||||
pdb: 3.0-
|
|
||||||
pickle: 3.0-
|
|
||||||
pickletools: 3.0-
|
|
||||||
pipes: 3.0-3.12
|
|
||||||
pkgutil: 3.0-
|
|
||||||
platform: 3.0-
|
|
||||||
plistlib: 3.0-
|
|
||||||
poplib: 3.0-
|
|
||||||
posix: 3.0-
|
|
||||||
posixpath: 3.0-
|
|
||||||
pprint: 3.0-
|
|
||||||
profile: 3.0-
|
|
||||||
pstats: 3.0-
|
|
||||||
pty: 3.0-
|
|
||||||
pwd: 3.0-
|
|
||||||
py_compile: 3.0-
|
|
||||||
pyclbr: 3.0-
|
|
||||||
pydoc: 3.0-
|
|
||||||
pydoc_data: 3.0-
|
|
||||||
pyexpat: 3.0-
|
|
||||||
queue: 3.0-
|
|
||||||
quopri: 3.0-
|
|
||||||
random: 3.0-
|
|
||||||
re: 3.0-
|
|
||||||
readline: 3.0-
|
|
||||||
reprlib: 3.0-
|
|
||||||
resource: 3.0-
|
|
||||||
rlcompleter: 3.0-
|
|
||||||
runpy: 3.0-
|
|
||||||
sched: 3.0-
|
|
||||||
secrets: 3.6-
|
|
||||||
select: 3.0-
|
|
||||||
selectors: 3.4-
|
|
||||||
shelve: 3.0-
|
|
||||||
shlex: 3.0-
|
|
||||||
shutil: 3.0-
|
|
||||||
signal: 3.0-
|
|
||||||
site: 3.0-
|
|
||||||
smtpd: 3.0-3.11
|
|
||||||
smtplib: 3.0-
|
|
||||||
sndhdr: 3.0-3.12
|
|
||||||
socket: 3.0-
|
|
||||||
socketserver: 3.0-
|
|
||||||
spwd: 3.0-3.12
|
|
||||||
sqlite3: 3.0-
|
|
||||||
sre_compile: 3.0-
|
|
||||||
sre_constants: 3.0-
|
|
||||||
sre_parse: 3.0-
|
|
||||||
ssl: 3.0-
|
|
||||||
stat: 3.0-
|
|
||||||
statistics: 3.4-
|
|
||||||
string: 3.0-
|
|
||||||
stringprep: 3.0-
|
|
||||||
struct: 3.0-
|
|
||||||
subprocess: 3.0-
|
|
||||||
sunau: 3.0-3.12
|
|
||||||
symbol: 3.0-3.9
|
|
||||||
symtable: 3.0-
|
|
||||||
sys: 3.0-
|
|
||||||
sys._monitoring: 3.12- # Doesn't actually exist. See comments in the stub.
|
|
||||||
sysconfig: 3.0-
|
|
||||||
syslog: 3.0-
|
|
||||||
tabnanny: 3.0-
|
|
||||||
tarfile: 3.0-
|
|
||||||
telnetlib: 3.0-3.12
|
|
||||||
tempfile: 3.0-
|
|
||||||
termios: 3.0-
|
|
||||||
textwrap: 3.0-
|
|
||||||
this: 3.0-
|
|
||||||
threading: 3.0-
|
|
||||||
time: 3.0-
|
|
||||||
timeit: 3.0-
|
|
||||||
tkinter: 3.0-
|
|
||||||
tkinter.tix: 3.0-3.12
|
|
||||||
token: 3.0-
|
|
||||||
tokenize: 3.0-
|
|
||||||
tomllib: 3.11-
|
|
||||||
trace: 3.0-
|
|
||||||
traceback: 3.0-
|
|
||||||
tracemalloc: 3.4-
|
|
||||||
tty: 3.0-
|
|
||||||
turtle: 3.0-
|
|
||||||
types: 3.0-
|
|
||||||
typing: 3.5-
|
|
||||||
typing_extensions: 3.0-
|
|
||||||
unicodedata: 3.0-
|
|
||||||
unittest: 3.0-
|
|
||||||
unittest._log: 3.9-
|
|
||||||
unittest.async_case: 3.8-
|
|
||||||
urllib: 3.0-
|
|
||||||
uu: 3.0-3.12
|
|
||||||
uuid: 3.0-
|
|
||||||
venv: 3.3-
|
|
||||||
warnings: 3.0-
|
|
||||||
wave: 3.0-
|
|
||||||
weakref: 3.0-
|
|
||||||
webbrowser: 3.0-
|
|
||||||
winreg: 3.0-
|
|
||||||
winsound: 3.0-
|
|
||||||
wsgiref: 3.0-
|
|
||||||
wsgiref.types: 3.11-
|
|
||||||
xdrlib: 3.0-3.12
|
|
||||||
xml: 3.0-
|
|
||||||
xmlrpc: 3.0-
|
|
||||||
xxlimited: 3.2-
|
|
||||||
zipapp: 3.5-
|
|
||||||
zipfile: 3.0-
|
|
||||||
zipfile._path: 3.12-
|
|
||||||
zipimport: 3.0-
|
|
||||||
zlib: 3.0-
|
|
||||||
zoneinfo: 3.9-
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
_VersionInfo: TypeAlias = tuple[int, int, int, str, int]
|
|
||||||
|
|
||||||
class _Feature:
|
|
||||||
def __init__(self, optionalRelease: _VersionInfo, mandatoryRelease: _VersionInfo | None, compiler_flag: int) -> None: ...
|
|
||||||
def getOptionalRelease(self) -> _VersionInfo: ...
|
|
||||||
def getMandatoryRelease(self) -> _VersionInfo | None: ...
|
|
||||||
compiler_flag: int
|
|
||||||
|
|
||||||
absolute_import: _Feature
|
|
||||||
division: _Feature
|
|
||||||
generators: _Feature
|
|
||||||
nested_scopes: _Feature
|
|
||||||
print_function: _Feature
|
|
||||||
unicode_literals: _Feature
|
|
||||||
with_statement: _Feature
|
|
||||||
barry_as_FLUFL: _Feature
|
|
||||||
generator_stop: _Feature
|
|
||||||
annotations: _Feature
|
|
||||||
|
|
||||||
all_feature_names: list[str] # undocumented
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"all_feature_names",
|
|
||||||
"absolute_import",
|
|
||||||
"division",
|
|
||||||
"generators",
|
|
||||||
"nested_scopes",
|
|
||||||
"print_function",
|
|
||||||
"unicode_literals",
|
|
||||||
"with_statement",
|
|
||||||
"barry_as_FLUFL",
|
|
||||||
"generator_stop",
|
|
||||||
"annotations",
|
|
||||||
]
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
from typing import Any
|
|
||||||
|
|
||||||
def __getattr__(name: str) -> Any: ...
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,84 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import SupportsLenAndGetItem, SupportsRichComparisonT
|
|
||||||
from collections.abc import Callable, MutableSequence
|
|
||||||
from typing import TypeVar, overload
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
@overload
|
|
||||||
def bisect_left(
|
|
||||||
a: SupportsLenAndGetItem[SupportsRichComparisonT],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: None = None,
|
|
||||||
) -> int: ...
|
|
||||||
@overload
|
|
||||||
def bisect_left(
|
|
||||||
a: SupportsLenAndGetItem[_T],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: Callable[[_T], SupportsRichComparisonT],
|
|
||||||
) -> int: ...
|
|
||||||
@overload
|
|
||||||
def bisect_right(
|
|
||||||
a: SupportsLenAndGetItem[SupportsRichComparisonT],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: None = None,
|
|
||||||
) -> int: ...
|
|
||||||
@overload
|
|
||||||
def bisect_right(
|
|
||||||
a: SupportsLenAndGetItem[_T],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: Callable[[_T], SupportsRichComparisonT],
|
|
||||||
) -> int: ...
|
|
||||||
@overload
|
|
||||||
def insort_left(
|
|
||||||
a: MutableSequence[SupportsRichComparisonT],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: None = None,
|
|
||||||
) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insort_left(
|
|
||||||
a: MutableSequence[_T], x: _T, lo: int = 0, hi: int | None = None, *, key: Callable[[_T], SupportsRichComparisonT]
|
|
||||||
) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insort_right(
|
|
||||||
a: MutableSequence[SupportsRichComparisonT],
|
|
||||||
x: SupportsRichComparisonT,
|
|
||||||
lo: int = 0,
|
|
||||||
hi: int | None = None,
|
|
||||||
*,
|
|
||||||
key: None = None,
|
|
||||||
) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insort_right(
|
|
||||||
a: MutableSequence[_T], x: _T, lo: int = 0, hi: int | None = None, *, key: Callable[[_T], SupportsRichComparisonT]
|
|
||||||
) -> None: ...
|
|
||||||
|
|
||||||
else:
|
|
||||||
def bisect_left(
|
|
||||||
a: SupportsLenAndGetItem[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
|
|
||||||
) -> int: ...
|
|
||||||
def bisect_right(
|
|
||||||
a: SupportsLenAndGetItem[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
|
|
||||||
) -> int: ...
|
|
||||||
def insort_left(
|
|
||||||
a: MutableSequence[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
|
|
||||||
) -> None: ...
|
|
||||||
def insort_right(
|
|
||||||
a: MutableSequence[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
|
|
||||||
) -> None: ...
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
def getpreferredencoding(do_setlocale: bool = True) -> str: ...
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import codecs
|
|
||||||
import sys
|
|
||||||
from _typeshed import ReadableBuffer
|
|
||||||
from collections.abc import Callable
|
|
||||||
from typing import Literal, overload
|
|
||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
# This type is not exposed; it is defined in unicodeobject.c
|
|
||||||
class _EncodingMap:
|
|
||||||
def size(self) -> int: ...
|
|
||||||
|
|
||||||
_CharMap: TypeAlias = dict[int, int] | _EncodingMap
|
|
||||||
_Handler: TypeAlias = Callable[[UnicodeError], tuple[str | bytes, int]]
|
|
||||||
_SearchFunction: TypeAlias = Callable[[str], codecs.CodecInfo | None]
|
|
||||||
|
|
||||||
def register(search_function: _SearchFunction, /) -> None: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
def unregister(search_function: _SearchFunction, /) -> None: ...
|
|
||||||
|
|
||||||
def register_error(errors: str, handler: _Handler, /) -> None: ...
|
|
||||||
def lookup_error(name: str, /) -> _Handler: ...
|
|
||||||
|
|
||||||
# The type ignore on `encode` and `decode` is to avoid issues with overlapping overloads, for more details, see #300
|
|
||||||
# https://docs.python.org/3/library/codecs.html#binary-transforms
|
|
||||||
_BytesToBytesEncoding: TypeAlias = Literal[
|
|
||||||
"base64",
|
|
||||||
"base_64",
|
|
||||||
"base64_codec",
|
|
||||||
"bz2",
|
|
||||||
"bz2_codec",
|
|
||||||
"hex",
|
|
||||||
"hex_codec",
|
|
||||||
"quopri",
|
|
||||||
"quotedprintable",
|
|
||||||
"quoted_printable",
|
|
||||||
"quopri_codec",
|
|
||||||
"uu",
|
|
||||||
"uu_codec",
|
|
||||||
"zip",
|
|
||||||
"zlib",
|
|
||||||
"zlib_codec",
|
|
||||||
]
|
|
||||||
# https://docs.python.org/3/library/codecs.html#text-transforms
|
|
||||||
_StrToStrEncoding: TypeAlias = Literal["rot13", "rot_13"]
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def encode(obj: ReadableBuffer, encoding: _BytesToBytesEncoding, errors: str = "strict") -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def encode(obj: str, encoding: _StrToStrEncoding, errors: str = "strict") -> str: ... # type: ignore[overload-overlap]
|
|
||||||
@overload
|
|
||||||
def encode(obj: str, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def decode(obj: ReadableBuffer, encoding: _BytesToBytesEncoding, errors: str = "strict") -> bytes: ... # type: ignore[overload-overlap]
|
|
||||||
@overload
|
|
||||||
def decode(obj: str, encoding: _StrToStrEncoding, errors: str = "strict") -> str: ...
|
|
||||||
|
|
||||||
# these are documented as text encodings but in practice they also accept str as input
|
|
||||||
@overload
|
|
||||||
def decode(
|
|
||||||
obj: str,
|
|
||||||
encoding: Literal["unicode_escape", "unicode-escape", "raw_unicode_escape", "raw-unicode-escape"],
|
|
||||||
errors: str = "strict",
|
|
||||||
) -> str: ...
|
|
||||||
|
|
||||||
# hex is officially documented as a bytes to bytes encoding, but it appears to also work with str
|
|
||||||
@overload
|
|
||||||
def decode(obj: str, encoding: Literal["hex", "hex_codec"], errors: str = "strict") -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def decode(obj: ReadableBuffer, encoding: str = "utf-8", errors: str = "strict") -> str: ...
|
|
||||||
def lookup(encoding: str, /) -> codecs.CodecInfo: ...
|
|
||||||
def charmap_build(map: str, /) -> _CharMap: ...
|
|
||||||
def ascii_decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...
|
|
||||||
def ascii_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def charmap_decode(data: ReadableBuffer, errors: str | None = None, mapping: _CharMap | None = None, /) -> tuple[str, int]: ...
|
|
||||||
def charmap_encode(str: str, errors: str | None = None, mapping: _CharMap | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def escape_decode(data: str | ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...
|
|
||||||
def escape_encode(data: bytes, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def latin_1_decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...
|
|
||||||
def latin_1_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def raw_unicode_escape_decode(
|
|
||||||
data: str | ReadableBuffer, errors: str | None = None, final: bool = True, /
|
|
||||||
) -> tuple[str, int]: ...
|
|
||||||
|
|
||||||
else:
|
|
||||||
def raw_unicode_escape_decode(data: str | ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...
|
|
||||||
|
|
||||||
def raw_unicode_escape_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def readbuffer_encode(data: str | ReadableBuffer, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def unicode_escape_decode(
|
|
||||||
data: str | ReadableBuffer, errors: str | None = None, final: bool = True, /
|
|
||||||
) -> tuple[str, int]: ...
|
|
||||||
|
|
||||||
else:
|
|
||||||
def unicode_escape_decode(data: str | ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...
|
|
||||||
|
|
||||||
def unicode_escape_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_16_be_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_16_be_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_16_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_16_encode(str: str, errors: str | None = None, byteorder: int = 0, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_16_ex_decode(
|
|
||||||
data: ReadableBuffer, errors: str | None = None, byteorder: int = 0, final: bool = False, /
|
|
||||||
) -> tuple[str, int, int]: ...
|
|
||||||
def utf_16_le_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_16_le_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_32_be_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_32_be_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_32_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_32_encode(str: str, errors: str | None = None, byteorder: int = 0, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_32_ex_decode(
|
|
||||||
data: ReadableBuffer, errors: str | None = None, byteorder: int = 0, final: bool = False, /
|
|
||||||
) -> tuple[str, int, int]: ...
|
|
||||||
def utf_32_le_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_32_le_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_7_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_7_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def utf_8_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def utf_8_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
|
||||||
def mbcs_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def mbcs_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def code_page_decode(
|
|
||||||
codepage: int, data: ReadableBuffer, errors: str | None = None, final: bool = False, /
|
|
||||||
) -> tuple[str, int]: ...
|
|
||||||
def code_page_encode(code_page: int, str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
def oem_decode(data: ReadableBuffer, errors: str | None = None, final: bool = False, /) -> tuple[str, int]: ...
|
|
||||||
def oem_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
import sys
|
|
||||||
from abc import abstractmethod
|
|
||||||
from types import MappingProxyType
|
|
||||||
from typing import ( # noqa: Y022,Y038,Y057
|
|
||||||
AbstractSet as Set,
|
|
||||||
AsyncGenerator as AsyncGenerator,
|
|
||||||
AsyncIterable as AsyncIterable,
|
|
||||||
AsyncIterator as AsyncIterator,
|
|
||||||
Awaitable as Awaitable,
|
|
||||||
ByteString as ByteString,
|
|
||||||
Callable as Callable,
|
|
||||||
Collection as Collection,
|
|
||||||
Container as Container,
|
|
||||||
Coroutine as Coroutine,
|
|
||||||
Generator as Generator,
|
|
||||||
Generic,
|
|
||||||
Hashable as Hashable,
|
|
||||||
ItemsView as ItemsView,
|
|
||||||
Iterable as Iterable,
|
|
||||||
Iterator as Iterator,
|
|
||||||
KeysView as KeysView,
|
|
||||||
Mapping as Mapping,
|
|
||||||
MappingView as MappingView,
|
|
||||||
MutableMapping as MutableMapping,
|
|
||||||
MutableSequence as MutableSequence,
|
|
||||||
MutableSet as MutableSet,
|
|
||||||
Protocol,
|
|
||||||
Reversible as Reversible,
|
|
||||||
Sequence as Sequence,
|
|
||||||
Sized as Sized,
|
|
||||||
TypeVar,
|
|
||||||
ValuesView as ValuesView,
|
|
||||||
final,
|
|
||||||
runtime_checkable,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"Awaitable",
|
|
||||||
"Coroutine",
|
|
||||||
"AsyncIterable",
|
|
||||||
"AsyncIterator",
|
|
||||||
"AsyncGenerator",
|
|
||||||
"Hashable",
|
|
||||||
"Iterable",
|
|
||||||
"Iterator",
|
|
||||||
"Generator",
|
|
||||||
"Reversible",
|
|
||||||
"Sized",
|
|
||||||
"Container",
|
|
||||||
"Callable",
|
|
||||||
"Collection",
|
|
||||||
"Set",
|
|
||||||
"MutableSet",
|
|
||||||
"Mapping",
|
|
||||||
"MutableMapping",
|
|
||||||
"MappingView",
|
|
||||||
"KeysView",
|
|
||||||
"ItemsView",
|
|
||||||
"ValuesView",
|
|
||||||
"Sequence",
|
|
||||||
"MutableSequence",
|
|
||||||
"ByteString",
|
|
||||||
]
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
__all__ += ["Buffer"]
|
|
||||||
|
|
||||||
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
|
|
||||||
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
|
|
||||||
|
|
||||||
@final
|
|
||||||
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
|
|
||||||
def __eq__(self, value: object, /) -> bool: ...
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
@property
|
|
||||||
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
@property
|
|
||||||
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class dict_items(ItemsView[_KT_co, _VT_co]): # undocumented
|
|
||||||
def __eq__(self, value: object, /) -> bool: ...
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
@property
|
|
||||||
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
@runtime_checkable
|
|
||||||
class Buffer(Protocol):
|
|
||||||
@abstractmethod
|
|
||||||
def __buffer__(self, flags: int, /) -> memoryview: ...
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
IMPORT_MAPPING: dict[str, str]
|
|
||||||
NAME_MAPPING: dict[tuple[str, str], tuple[str, str]]
|
|
||||||
PYTHON2_EXCEPTIONS: tuple[str, ...]
|
|
||||||
MULTIPROCESSING_EXCEPTIONS: tuple[str, ...]
|
|
||||||
REVERSE_IMPORT_MAPPING: dict[str, str]
|
|
||||||
REVERSE_NAME_MAPPING: dict[tuple[str, str], tuple[str, str]]
|
|
||||||
PYTHON3_OSERROR_EXCEPTIONS: tuple[str, ...]
|
|
||||||
PYTHON3_IMPORTERROR_EXCEPTIONS: tuple[str, ...]
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
from _typeshed import WriteableBuffer
|
|
||||||
from collections.abc import Callable
|
|
||||||
from io import DEFAULT_BUFFER_SIZE, BufferedIOBase, RawIOBase
|
|
||||||
from typing import Any, Protocol
|
|
||||||
|
|
||||||
BUFFER_SIZE = DEFAULT_BUFFER_SIZE
|
|
||||||
|
|
||||||
class _Reader(Protocol):
|
|
||||||
def read(self, n: int, /) -> bytes: ...
|
|
||||||
def seekable(self) -> bool: ...
|
|
||||||
def seek(self, n: int, /) -> Any: ...
|
|
||||||
|
|
||||||
class BaseStream(BufferedIOBase): ...
|
|
||||||
|
|
||||||
class DecompressReader(RawIOBase):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
fp: _Reader,
|
|
||||||
decomp_factory: Callable[..., object],
|
|
||||||
trailing_error: type[Exception] | tuple[type[Exception], ...] = (),
|
|
||||||
**decomp_args: Any,
|
|
||||||
) -> None: ...
|
|
||||||
def readinto(self, b: WriteableBuffer) -> int: ...
|
|
||||||
def read(self, size: int = -1) -> bytes: ...
|
|
||||||
def seek(self, offset: int, whence: int = 0) -> int: ...
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import SupportsWrite
|
|
||||||
from collections.abc import Iterable, Iterator
|
|
||||||
from typing import Any, Final, Literal
|
|
||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
__version__: Final[str]
|
|
||||||
|
|
||||||
QUOTE_ALL: Literal[1]
|
|
||||||
QUOTE_MINIMAL: Literal[0]
|
|
||||||
QUOTE_NONE: Literal[3]
|
|
||||||
QUOTE_NONNUMERIC: Literal[2]
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
QUOTE_STRINGS: Literal[4]
|
|
||||||
QUOTE_NOTNULL: Literal[5]
|
|
||||||
|
|
||||||
# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
|
|
||||||
# However, using literals in situations like these can cause false-positives (see #7258)
|
|
||||||
_QuotingType: TypeAlias = int
|
|
||||||
|
|
||||||
class Error(Exception): ...
|
|
||||||
|
|
||||||
class Dialect:
|
|
||||||
delimiter: str
|
|
||||||
quotechar: str | None
|
|
||||||
escapechar: str | None
|
|
||||||
doublequote: bool
|
|
||||||
skipinitialspace: bool
|
|
||||||
lineterminator: str
|
|
||||||
quoting: _QuotingType
|
|
||||||
strict: bool
|
|
||||||
def __init__(self) -> None: ...
|
|
||||||
|
|
||||||
_DialectLike: TypeAlias = str | Dialect | type[Dialect]
|
|
||||||
|
|
||||||
class _reader(Iterator[list[str]]):
|
|
||||||
@property
|
|
||||||
def dialect(self) -> Dialect: ...
|
|
||||||
line_num: int
|
|
||||||
def __next__(self) -> list[str]: ...
|
|
||||||
|
|
||||||
class _writer:
|
|
||||||
@property
|
|
||||||
def dialect(self) -> Dialect: ...
|
|
||||||
def writerow(self, row: Iterable[Any]) -> Any: ...
|
|
||||||
def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...
|
|
||||||
|
|
||||||
def writer(
|
|
||||||
csvfile: SupportsWrite[str],
|
|
||||||
dialect: _DialectLike = "excel",
|
|
||||||
*,
|
|
||||||
delimiter: str = ",",
|
|
||||||
quotechar: str | None = '"',
|
|
||||||
escapechar: str | None = None,
|
|
||||||
doublequote: bool = True,
|
|
||||||
skipinitialspace: bool = False,
|
|
||||||
lineterminator: str = "\r\n",
|
|
||||||
quoting: _QuotingType = 0,
|
|
||||||
strict: bool = False,
|
|
||||||
) -> _writer: ...
|
|
||||||
def reader(
|
|
||||||
csvfile: Iterable[str],
|
|
||||||
dialect: _DialectLike = "excel",
|
|
||||||
*,
|
|
||||||
delimiter: str = ",",
|
|
||||||
quotechar: str | None = '"',
|
|
||||||
escapechar: str | None = None,
|
|
||||||
doublequote: bool = True,
|
|
||||||
skipinitialspace: bool = False,
|
|
||||||
lineterminator: str = "\r\n",
|
|
||||||
quoting: _QuotingType = 0,
|
|
||||||
strict: bool = False,
|
|
||||||
) -> _reader: ...
|
|
||||||
def register_dialect(
|
|
||||||
name: str,
|
|
||||||
dialect: type[Dialect] = ...,
|
|
||||||
*,
|
|
||||||
delimiter: str = ",",
|
|
||||||
quotechar: str | None = '"',
|
|
||||||
escapechar: str | None = None,
|
|
||||||
doublequote: bool = True,
|
|
||||||
skipinitialspace: bool = False,
|
|
||||||
lineterminator: str = "\r\n",
|
|
||||||
quoting: _QuotingType = 0,
|
|
||||||
strict: bool = False,
|
|
||||||
) -> None: ...
|
|
||||||
def unregister_dialect(name: str) -> None: ...
|
|
||||||
def get_dialect(name: str) -> Dialect: ...
|
|
||||||
def list_dialects() -> list[str]: ...
|
|
||||||
def field_size_limit(new_limit: int = ...) -> int: ...
|
|
||||||
@@ -1,211 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import ReadableBuffer, WriteableBuffer
|
|
||||||
from abc import abstractmethod
|
|
||||||
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
|
|
||||||
from ctypes import CDLL, ArgumentError as ArgumentError
|
|
||||||
from typing import Any, ClassVar, Generic, TypeVar, overload
|
|
||||||
from typing_extensions import Self, TypeAlias
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
from types import GenericAlias
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
_CT = TypeVar("_CT", bound=_CData)
|
|
||||||
|
|
||||||
FUNCFLAG_CDECL: int
|
|
||||||
FUNCFLAG_PYTHONAPI: int
|
|
||||||
FUNCFLAG_USE_ERRNO: int
|
|
||||||
FUNCFLAG_USE_LASTERROR: int
|
|
||||||
RTLD_GLOBAL: int
|
|
||||||
RTLD_LOCAL: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
CTYPES_MAX_ARGCOUNT: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
SIZEOF_TIME_T: int
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
|
||||||
# Description, Source, HelpFile, HelpContext, scode
|
|
||||||
_COMError_Details: TypeAlias = tuple[str | None, str | None, str | None, int | None, int | None]
|
|
||||||
|
|
||||||
class COMError(Exception):
|
|
||||||
hresult: int
|
|
||||||
text: str | None
|
|
||||||
details: _COMError_Details
|
|
||||||
|
|
||||||
def __init__(self, hresult: int, text: str | None, details: _COMError_Details) -> None: ...
|
|
||||||
|
|
||||||
def CopyComPointer(src: _PointerLike, dst: _PointerLike | _CArgObject) -> int: ...
|
|
||||||
|
|
||||||
FUNCFLAG_HRESULT: int
|
|
||||||
FUNCFLAG_STDCALL: int
|
|
||||||
|
|
||||||
def FormatError(code: int = ...) -> str: ...
|
|
||||||
def get_last_error() -> int: ...
|
|
||||||
def set_last_error(value: int) -> int: ...
|
|
||||||
def LoadLibrary(name: str, load_flags: int = 0, /) -> int: ...
|
|
||||||
def FreeLibrary(handle: int, /) -> None: ...
|
|
||||||
|
|
||||||
class _CDataMeta(type):
|
|
||||||
# By default mypy complains about the following two methods, because strictly speaking cls
|
|
||||||
# might not be a Type[_CT]. However this can never actually happen, because the only class that
|
|
||||||
# uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
|
|
||||||
def __mul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc]
|
|
||||||
def __rmul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc]
|
|
||||||
|
|
||||||
class _CData(metaclass=_CDataMeta):
|
|
||||||
_b_base_: int
|
|
||||||
_b_needsfree_: bool
|
|
||||||
_objects: Mapping[Any, int] | None
|
|
||||||
# At runtime the following classmethods are available only on classes, not
|
|
||||||
# on instances. This can't be reflected properly in the type system:
|
|
||||||
#
|
|
||||||
# Structure.from_buffer(...) # valid at runtime
|
|
||||||
# Structure(...).from_buffer(...) # invalid at runtime
|
|
||||||
#
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_buffer(cls, source: WriteableBuffer, offset: int = ...) -> Self: ...
|
|
||||||
@classmethod
|
|
||||||
def from_buffer_copy(cls, source: ReadableBuffer, offset: int = ...) -> Self: ...
|
|
||||||
@classmethod
|
|
||||||
def from_address(cls, address: int) -> Self: ...
|
|
||||||
@classmethod
|
|
||||||
def from_param(cls, obj: Any) -> Self | _CArgObject: ...
|
|
||||||
@classmethod
|
|
||||||
def in_dll(cls, library: CDLL, name: str) -> Self: ...
|
|
||||||
def __buffer__(self, flags: int, /) -> memoryview: ...
|
|
||||||
def __release_buffer__(self, buffer: memoryview, /) -> None: ...
|
|
||||||
|
|
||||||
class _SimpleCData(_CData, Generic[_T]):
|
|
||||||
value: _T
|
|
||||||
# The TypeVar can be unsolved here,
|
|
||||||
# but we can't use overloads without creating many, many mypy false-positive errors
|
|
||||||
def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse]
|
|
||||||
|
|
||||||
class _CanCastTo(_CData): ...
|
|
||||||
class _PointerLike(_CanCastTo): ...
|
|
||||||
|
|
||||||
class _Pointer(_PointerLike, _CData, Generic[_CT]):
|
|
||||||
_type_: type[_CT]
|
|
||||||
contents: _CT
|
|
||||||
@overload
|
|
||||||
def __init__(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __init__(self, arg: _CT) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __getitem__(self, key: int, /) -> Any: ...
|
|
||||||
@overload
|
|
||||||
def __getitem__(self, key: slice, /) -> list[Any]: ...
|
|
||||||
def __setitem__(self, key: int, value: Any, /) -> None: ...
|
|
||||||
|
|
||||||
def POINTER(type: type[_CT]) -> type[_Pointer[_CT]]: ...
|
|
||||||
def pointer(arg: _CT, /) -> _Pointer[_CT]: ...
|
|
||||||
|
|
||||||
class _CArgObject: ...
|
|
||||||
|
|
||||||
def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
|
|
||||||
|
|
||||||
_ECT: TypeAlias = Callable[[_CData | None, CFuncPtr, tuple[_CData, ...]], _CData]
|
|
||||||
_PF: TypeAlias = tuple[int] | tuple[int, str | None] | tuple[int, str | None, Any]
|
|
||||||
|
|
||||||
class CFuncPtr(_PointerLike, _CData):
|
|
||||||
restype: type[_CData] | Callable[[int], Any] | None
|
|
||||||
argtypes: Sequence[type[_CData]]
|
|
||||||
errcheck: _ECT
|
|
||||||
# Abstract attribute that must be defined on subclasses
|
|
||||||
_flags_: ClassVar[int]
|
|
||||||
@overload
|
|
||||||
def __init__(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __init__(self, address: int, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __init__(self, callable: Callable[..., Any], /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] | None = ..., /) -> None: ...
|
|
||||||
if sys.platform == "win32":
|
|
||||||
@overload
|
|
||||||
def __init__(
|
|
||||||
self, vtbl_index: int, name: str, paramflags: tuple[_PF, ...] | None = ..., iid: _CData | None = ..., /
|
|
||||||
) -> None: ...
|
|
||||||
|
|
||||||
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
|
|
||||||
|
|
||||||
_GetT = TypeVar("_GetT")
|
|
||||||
_SetT = TypeVar("_SetT")
|
|
||||||
|
|
||||||
class _CField(Generic[_CT, _GetT, _SetT]):
|
|
||||||
offset: int
|
|
||||||
size: int
|
|
||||||
@overload
|
|
||||||
def __get__(self, instance: None, owner: type[Any] | None, /) -> Self: ...
|
|
||||||
@overload
|
|
||||||
def __get__(self, instance: Any, owner: type[Any] | None, /) -> _GetT: ...
|
|
||||||
def __set__(self, instance: Any, value: _SetT, /) -> None: ...
|
|
||||||
|
|
||||||
class _StructUnionMeta(_CDataMeta):
|
|
||||||
_fields_: Sequence[tuple[str, type[_CData]] | tuple[str, type[_CData], int]]
|
|
||||||
_pack_: int
|
|
||||||
_anonymous_: Sequence[str]
|
|
||||||
def __getattr__(self, name: str) -> _CField[Any, Any, Any]: ...
|
|
||||||
|
|
||||||
class _StructUnionBase(_CData, metaclass=_StructUnionMeta):
|
|
||||||
def __init__(self, *args: Any, **kw: Any) -> None: ...
|
|
||||||
def __getattr__(self, name: str) -> Any: ...
|
|
||||||
def __setattr__(self, name: str, value: Any) -> None: ...
|
|
||||||
|
|
||||||
class Union(_StructUnionBase): ...
|
|
||||||
class Structure(_StructUnionBase): ...
|
|
||||||
|
|
||||||
class Array(_CData, Generic[_CT]):
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def _length_(self) -> int: ...
|
|
||||||
@_length_.setter
|
|
||||||
def _length_(self, value: int) -> None: ...
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def _type_(self) -> type[_CT]: ...
|
|
||||||
@_type_.setter
|
|
||||||
def _type_(self, value: type[_CT]) -> None: ...
|
|
||||||
# Note: only available if _CT == c_char
|
|
||||||
@property
|
|
||||||
def raw(self) -> bytes: ...
|
|
||||||
@raw.setter
|
|
||||||
def raw(self, value: ReadableBuffer) -> None: ...
|
|
||||||
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
|
|
||||||
# TODO These methods cannot be annotated correctly at the moment.
|
|
||||||
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT
|
|
||||||
# here, because of a special feature of ctypes.
|
|
||||||
# By default, when accessing an element of an Array[_CT], the returned object has type _CT.
|
|
||||||
# However, when _CT is a "simple type" like c_int, ctypes automatically "unboxes" the object
|
|
||||||
# and converts it to the corresponding Python primitive. For example, when accessing an element
|
|
||||||
# of an Array[c_int], a Python int object is returned, not a c_int.
|
|
||||||
# This behavior does *not* apply to subclasses of "simple types".
|
|
||||||
# If MyInt is a subclass of c_int, then accessing an element of an Array[MyInt] returns
|
|
||||||
# a MyInt, not an int.
|
|
||||||
# This special behavior is not easy to model in a stub, so for now all places where
|
|
||||||
# the array element type would belong are annotated with Any instead.
|
|
||||||
def __init__(self, *args: Any) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __getitem__(self, key: int, /) -> Any: ...
|
|
||||||
@overload
|
|
||||||
def __getitem__(self, key: slice, /) -> list[Any]: ...
|
|
||||||
@overload
|
|
||||||
def __setitem__(self, key: int, value: Any, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def __setitem__(self, key: slice, value: Iterable[Any], /) -> None: ...
|
|
||||||
def __iter__(self) -> Iterator[Any]: ...
|
|
||||||
# Can't inherit from Sized because the metaclass conflict between
|
|
||||||
# Sized and _CData prevents using _CDataMeta.
|
|
||||||
def __len__(self) -> int: ...
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
|
|
||||||
|
|
||||||
def addressof(obj: _CData) -> int: ...
|
|
||||||
def alignment(obj_or_type: _CData | type[_CData]) -> int: ...
|
|
||||||
def get_errno() -> int: ...
|
|
||||||
def resize(obj: _CData, size: int) -> None: ...
|
|
||||||
def set_errno(value: int) -> int: ...
|
|
||||||
def sizeof(obj_or_type: _CData | type[_CData]) -> int: ...
|
|
||||||
@@ -1,565 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import ReadOnlyBuffer, SupportsRead
|
|
||||||
from typing import IO, Any, NamedTuple, final, overload
|
|
||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
# NOTE: This module is ordinarily only available on Unix, but the windows-curses
|
|
||||||
# package makes it available on Windows as well with the same contents.
|
|
||||||
|
|
||||||
# Handled by PyCurses_ConvertToChtype in _cursesmodule.c.
|
|
||||||
_ChType: TypeAlias = str | bytes | int
|
|
||||||
|
|
||||||
# ACS codes are only initialized after initscr is called
|
|
||||||
ACS_BBSS: int
|
|
||||||
ACS_BLOCK: int
|
|
||||||
ACS_BOARD: int
|
|
||||||
ACS_BSBS: int
|
|
||||||
ACS_BSSB: int
|
|
||||||
ACS_BSSS: int
|
|
||||||
ACS_BTEE: int
|
|
||||||
ACS_BULLET: int
|
|
||||||
ACS_CKBOARD: int
|
|
||||||
ACS_DARROW: int
|
|
||||||
ACS_DEGREE: int
|
|
||||||
ACS_DIAMOND: int
|
|
||||||
ACS_GEQUAL: int
|
|
||||||
ACS_HLINE: int
|
|
||||||
ACS_LANTERN: int
|
|
||||||
ACS_LARROW: int
|
|
||||||
ACS_LEQUAL: int
|
|
||||||
ACS_LLCORNER: int
|
|
||||||
ACS_LRCORNER: int
|
|
||||||
ACS_LTEE: int
|
|
||||||
ACS_NEQUAL: int
|
|
||||||
ACS_PI: int
|
|
||||||
ACS_PLMINUS: int
|
|
||||||
ACS_PLUS: int
|
|
||||||
ACS_RARROW: int
|
|
||||||
ACS_RTEE: int
|
|
||||||
ACS_S1: int
|
|
||||||
ACS_S3: int
|
|
||||||
ACS_S7: int
|
|
||||||
ACS_S9: int
|
|
||||||
ACS_SBBS: int
|
|
||||||
ACS_SBSB: int
|
|
||||||
ACS_SBSS: int
|
|
||||||
ACS_SSBB: int
|
|
||||||
ACS_SSBS: int
|
|
||||||
ACS_SSSB: int
|
|
||||||
ACS_SSSS: int
|
|
||||||
ACS_STERLING: int
|
|
||||||
ACS_TTEE: int
|
|
||||||
ACS_UARROW: int
|
|
||||||
ACS_ULCORNER: int
|
|
||||||
ACS_URCORNER: int
|
|
||||||
ACS_VLINE: int
|
|
||||||
ALL_MOUSE_EVENTS: int
|
|
||||||
A_ALTCHARSET: int
|
|
||||||
A_ATTRIBUTES: int
|
|
||||||
A_BLINK: int
|
|
||||||
A_BOLD: int
|
|
||||||
A_CHARTEXT: int
|
|
||||||
A_COLOR: int
|
|
||||||
A_DIM: int
|
|
||||||
A_HORIZONTAL: int
|
|
||||||
A_INVIS: int
|
|
||||||
A_ITALIC: int
|
|
||||||
A_LEFT: int
|
|
||||||
A_LOW: int
|
|
||||||
A_NORMAL: int
|
|
||||||
A_PROTECT: int
|
|
||||||
A_REVERSE: int
|
|
||||||
A_RIGHT: int
|
|
||||||
A_STANDOUT: int
|
|
||||||
A_TOP: int
|
|
||||||
A_UNDERLINE: int
|
|
||||||
A_VERTICAL: int
|
|
||||||
BUTTON1_CLICKED: int
|
|
||||||
BUTTON1_DOUBLE_CLICKED: int
|
|
||||||
BUTTON1_PRESSED: int
|
|
||||||
BUTTON1_RELEASED: int
|
|
||||||
BUTTON1_TRIPLE_CLICKED: int
|
|
||||||
BUTTON2_CLICKED: int
|
|
||||||
BUTTON2_DOUBLE_CLICKED: int
|
|
||||||
BUTTON2_PRESSED: int
|
|
||||||
BUTTON2_RELEASED: int
|
|
||||||
BUTTON2_TRIPLE_CLICKED: int
|
|
||||||
BUTTON3_CLICKED: int
|
|
||||||
BUTTON3_DOUBLE_CLICKED: int
|
|
||||||
BUTTON3_PRESSED: int
|
|
||||||
BUTTON3_RELEASED: int
|
|
||||||
BUTTON3_TRIPLE_CLICKED: int
|
|
||||||
BUTTON4_CLICKED: int
|
|
||||||
BUTTON4_DOUBLE_CLICKED: int
|
|
||||||
BUTTON4_PRESSED: int
|
|
||||||
BUTTON4_RELEASED: int
|
|
||||||
BUTTON4_TRIPLE_CLICKED: int
|
|
||||||
# Darwin ncurses doesn't provide BUTTON5_* constants
|
|
||||||
if sys.version_info >= (3, 10) and sys.platform != "darwin":
|
|
||||||
BUTTON5_PRESSED: int
|
|
||||||
BUTTON5_RELEASED: int
|
|
||||||
BUTTON5_CLICKED: int
|
|
||||||
BUTTON5_DOUBLE_CLICKED: int
|
|
||||||
BUTTON5_TRIPLE_CLICKED: int
|
|
||||||
BUTTON_ALT: int
|
|
||||||
BUTTON_CTRL: int
|
|
||||||
BUTTON_SHIFT: int
|
|
||||||
COLOR_BLACK: int
|
|
||||||
COLOR_BLUE: int
|
|
||||||
COLOR_CYAN: int
|
|
||||||
COLOR_GREEN: int
|
|
||||||
COLOR_MAGENTA: int
|
|
||||||
COLOR_RED: int
|
|
||||||
COLOR_WHITE: int
|
|
||||||
COLOR_YELLOW: int
|
|
||||||
ERR: int
|
|
||||||
KEY_A1: int
|
|
||||||
KEY_A3: int
|
|
||||||
KEY_B2: int
|
|
||||||
KEY_BACKSPACE: int
|
|
||||||
KEY_BEG: int
|
|
||||||
KEY_BREAK: int
|
|
||||||
KEY_BTAB: int
|
|
||||||
KEY_C1: int
|
|
||||||
KEY_C3: int
|
|
||||||
KEY_CANCEL: int
|
|
||||||
KEY_CATAB: int
|
|
||||||
KEY_CLEAR: int
|
|
||||||
KEY_CLOSE: int
|
|
||||||
KEY_COMMAND: int
|
|
||||||
KEY_COPY: int
|
|
||||||
KEY_CREATE: int
|
|
||||||
KEY_CTAB: int
|
|
||||||
KEY_DC: int
|
|
||||||
KEY_DL: int
|
|
||||||
KEY_DOWN: int
|
|
||||||
KEY_EIC: int
|
|
||||||
KEY_END: int
|
|
||||||
KEY_ENTER: int
|
|
||||||
KEY_EOL: int
|
|
||||||
KEY_EOS: int
|
|
||||||
KEY_EXIT: int
|
|
||||||
KEY_F0: int
|
|
||||||
KEY_F1: int
|
|
||||||
KEY_F10: int
|
|
||||||
KEY_F11: int
|
|
||||||
KEY_F12: int
|
|
||||||
KEY_F13: int
|
|
||||||
KEY_F14: int
|
|
||||||
KEY_F15: int
|
|
||||||
KEY_F16: int
|
|
||||||
KEY_F17: int
|
|
||||||
KEY_F18: int
|
|
||||||
KEY_F19: int
|
|
||||||
KEY_F2: int
|
|
||||||
KEY_F20: int
|
|
||||||
KEY_F21: int
|
|
||||||
KEY_F22: int
|
|
||||||
KEY_F23: int
|
|
||||||
KEY_F24: int
|
|
||||||
KEY_F25: int
|
|
||||||
KEY_F26: int
|
|
||||||
KEY_F27: int
|
|
||||||
KEY_F28: int
|
|
||||||
KEY_F29: int
|
|
||||||
KEY_F3: int
|
|
||||||
KEY_F30: int
|
|
||||||
KEY_F31: int
|
|
||||||
KEY_F32: int
|
|
||||||
KEY_F33: int
|
|
||||||
KEY_F34: int
|
|
||||||
KEY_F35: int
|
|
||||||
KEY_F36: int
|
|
||||||
KEY_F37: int
|
|
||||||
KEY_F38: int
|
|
||||||
KEY_F39: int
|
|
||||||
KEY_F4: int
|
|
||||||
KEY_F40: int
|
|
||||||
KEY_F41: int
|
|
||||||
KEY_F42: int
|
|
||||||
KEY_F43: int
|
|
||||||
KEY_F44: int
|
|
||||||
KEY_F45: int
|
|
||||||
KEY_F46: int
|
|
||||||
KEY_F47: int
|
|
||||||
KEY_F48: int
|
|
||||||
KEY_F49: int
|
|
||||||
KEY_F5: int
|
|
||||||
KEY_F50: int
|
|
||||||
KEY_F51: int
|
|
||||||
KEY_F52: int
|
|
||||||
KEY_F53: int
|
|
||||||
KEY_F54: int
|
|
||||||
KEY_F55: int
|
|
||||||
KEY_F56: int
|
|
||||||
KEY_F57: int
|
|
||||||
KEY_F58: int
|
|
||||||
KEY_F59: int
|
|
||||||
KEY_F6: int
|
|
||||||
KEY_F60: int
|
|
||||||
KEY_F61: int
|
|
||||||
KEY_F62: int
|
|
||||||
KEY_F63: int
|
|
||||||
KEY_F7: int
|
|
||||||
KEY_F8: int
|
|
||||||
KEY_F9: int
|
|
||||||
KEY_FIND: int
|
|
||||||
KEY_HELP: int
|
|
||||||
KEY_HOME: int
|
|
||||||
KEY_IC: int
|
|
||||||
KEY_IL: int
|
|
||||||
KEY_LEFT: int
|
|
||||||
KEY_LL: int
|
|
||||||
KEY_MARK: int
|
|
||||||
KEY_MAX: int
|
|
||||||
KEY_MESSAGE: int
|
|
||||||
KEY_MIN: int
|
|
||||||
KEY_MOUSE: int
|
|
||||||
KEY_MOVE: int
|
|
||||||
KEY_NEXT: int
|
|
||||||
KEY_NPAGE: int
|
|
||||||
KEY_OPEN: int
|
|
||||||
KEY_OPTIONS: int
|
|
||||||
KEY_PPAGE: int
|
|
||||||
KEY_PREVIOUS: int
|
|
||||||
KEY_PRINT: int
|
|
||||||
KEY_REDO: int
|
|
||||||
KEY_REFERENCE: int
|
|
||||||
KEY_REFRESH: int
|
|
||||||
KEY_REPLACE: int
|
|
||||||
KEY_RESET: int
|
|
||||||
KEY_RESIZE: int
|
|
||||||
KEY_RESTART: int
|
|
||||||
KEY_RESUME: int
|
|
||||||
KEY_RIGHT: int
|
|
||||||
KEY_SAVE: int
|
|
||||||
KEY_SBEG: int
|
|
||||||
KEY_SCANCEL: int
|
|
||||||
KEY_SCOMMAND: int
|
|
||||||
KEY_SCOPY: int
|
|
||||||
KEY_SCREATE: int
|
|
||||||
KEY_SDC: int
|
|
||||||
KEY_SDL: int
|
|
||||||
KEY_SELECT: int
|
|
||||||
KEY_SEND: int
|
|
||||||
KEY_SEOL: int
|
|
||||||
KEY_SEXIT: int
|
|
||||||
KEY_SF: int
|
|
||||||
KEY_SFIND: int
|
|
||||||
KEY_SHELP: int
|
|
||||||
KEY_SHOME: int
|
|
||||||
KEY_SIC: int
|
|
||||||
KEY_SLEFT: int
|
|
||||||
KEY_SMESSAGE: int
|
|
||||||
KEY_SMOVE: int
|
|
||||||
KEY_SNEXT: int
|
|
||||||
KEY_SOPTIONS: int
|
|
||||||
KEY_SPREVIOUS: int
|
|
||||||
KEY_SPRINT: int
|
|
||||||
KEY_SR: int
|
|
||||||
KEY_SREDO: int
|
|
||||||
KEY_SREPLACE: int
|
|
||||||
KEY_SRESET: int
|
|
||||||
KEY_SRIGHT: int
|
|
||||||
KEY_SRSUME: int
|
|
||||||
KEY_SSAVE: int
|
|
||||||
KEY_SSUSPEND: int
|
|
||||||
KEY_STAB: int
|
|
||||||
KEY_SUNDO: int
|
|
||||||
KEY_SUSPEND: int
|
|
||||||
KEY_UNDO: int
|
|
||||||
KEY_UP: int
|
|
||||||
OK: int
|
|
||||||
REPORT_MOUSE_POSITION: int
|
|
||||||
_C_API: Any
|
|
||||||
version: bytes
|
|
||||||
|
|
||||||
def baudrate() -> int: ...
|
|
||||||
def beep() -> None: ...
|
|
||||||
def can_change_color() -> bool: ...
|
|
||||||
def cbreak(flag: bool = True, /) -> None: ...
|
|
||||||
def color_content(color_number: int, /) -> tuple[int, int, int]: ...
|
|
||||||
def color_pair(pair_number: int, /) -> int: ...
|
|
||||||
def curs_set(visibility: int, /) -> int: ...
|
|
||||||
def def_prog_mode() -> None: ...
|
|
||||||
def def_shell_mode() -> None: ...
|
|
||||||
def delay_output(ms: int, /) -> None: ...
|
|
||||||
def doupdate() -> None: ...
|
|
||||||
def echo(flag: bool = True, /) -> None: ...
|
|
||||||
def endwin() -> None: ...
|
|
||||||
def erasechar() -> bytes: ...
|
|
||||||
def filter() -> None: ...
|
|
||||||
def flash() -> None: ...
|
|
||||||
def flushinp() -> None: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def get_escdelay() -> int: ...
|
|
||||||
def get_tabsize() -> int: ...
|
|
||||||
|
|
||||||
def getmouse() -> tuple[int, int, int, int, int]: ...
|
|
||||||
def getsyx() -> tuple[int, int]: ...
|
|
||||||
def getwin(file: SupportsRead[bytes], /) -> _CursesWindow: ...
|
|
||||||
def halfdelay(tenths: int, /) -> None: ...
|
|
||||||
def has_colors() -> bool: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
def has_extended_color_support() -> bool: ...
|
|
||||||
|
|
||||||
def has_ic() -> bool: ...
|
|
||||||
def has_il() -> bool: ...
|
|
||||||
def has_key(key: int, /) -> bool: ...
|
|
||||||
def init_color(color_number: int, r: int, g: int, b: int, /) -> None: ...
|
|
||||||
def init_pair(pair_number: int, fg: int, bg: int, /) -> None: ...
|
|
||||||
def initscr() -> _CursesWindow: ...
|
|
||||||
def intrflush(flag: bool, /) -> None: ...
|
|
||||||
def is_term_resized(nlines: int, ncols: int, /) -> bool: ...
|
|
||||||
def isendwin() -> bool: ...
|
|
||||||
def keyname(key: int, /) -> bytes: ...
|
|
||||||
def killchar() -> bytes: ...
|
|
||||||
def longname() -> bytes: ...
|
|
||||||
def meta(yes: bool, /) -> None: ...
|
|
||||||
def mouseinterval(interval: int, /) -> None: ...
|
|
||||||
def mousemask(newmask: int, /) -> tuple[int, int]: ...
|
|
||||||
def napms(ms: int, /) -> int: ...
|
|
||||||
def newpad(nlines: int, ncols: int, /) -> _CursesWindow: ...
|
|
||||||
def newwin(nlines: int, ncols: int, begin_y: int = ..., begin_x: int = ..., /) -> _CursesWindow: ...
|
|
||||||
def nl(flag: bool = True, /) -> None: ...
|
|
||||||
def nocbreak() -> None: ...
|
|
||||||
def noecho() -> None: ...
|
|
||||||
def nonl() -> None: ...
|
|
||||||
def noqiflush() -> None: ...
|
|
||||||
def noraw() -> None: ...
|
|
||||||
def pair_content(pair_number: int, /) -> tuple[int, int]: ...
|
|
||||||
def pair_number(attr: int, /) -> int: ...
|
|
||||||
def putp(string: ReadOnlyBuffer, /) -> None: ...
|
|
||||||
def qiflush(flag: bool = True, /) -> None: ...
|
|
||||||
def raw(flag: bool = True, /) -> None: ...
|
|
||||||
def reset_prog_mode() -> None: ...
|
|
||||||
def reset_shell_mode() -> None: ...
|
|
||||||
def resetty() -> None: ...
|
|
||||||
def resize_term(nlines: int, ncols: int, /) -> None: ...
|
|
||||||
def resizeterm(nlines: int, ncols: int, /) -> None: ...
|
|
||||||
def savetty() -> None: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def set_escdelay(ms: int, /) -> None: ...
|
|
||||||
def set_tabsize(size: int, /) -> None: ...
|
|
||||||
|
|
||||||
def setsyx(y: int, x: int, /) -> None: ...
|
|
||||||
def setupterm(term: str | None = None, fd: int = -1) -> None: ...
|
|
||||||
def start_color() -> None: ...
|
|
||||||
def termattrs() -> int: ...
|
|
||||||
def termname() -> bytes: ...
|
|
||||||
def tigetflag(capname: str, /) -> int: ...
|
|
||||||
def tigetnum(capname: str, /) -> int: ...
|
|
||||||
def tigetstr(capname: str, /) -> bytes | None: ...
|
|
||||||
def tparm(
|
|
||||||
str: ReadOnlyBuffer,
|
|
||||||
i1: int = 0,
|
|
||||||
i2: int = 0,
|
|
||||||
i3: int = 0,
|
|
||||||
i4: int = 0,
|
|
||||||
i5: int = 0,
|
|
||||||
i6: int = 0,
|
|
||||||
i7: int = 0,
|
|
||||||
i8: int = 0,
|
|
||||||
i9: int = 0,
|
|
||||||
/,
|
|
||||||
) -> bytes: ...
|
|
||||||
def typeahead(fd: int, /) -> None: ...
|
|
||||||
def unctrl(ch: _ChType, /) -> bytes: ...
|
|
||||||
|
|
||||||
if sys.version_info < (3, 12) or sys.platform != "darwin":
|
|
||||||
# The support for macos was dropped in 3.12
|
|
||||||
def unget_wch(ch: int | str, /) -> None: ...
|
|
||||||
|
|
||||||
def ungetch(ch: _ChType, /) -> None: ...
|
|
||||||
def ungetmouse(id: int, x: int, y: int, z: int, bstate: int, /) -> None: ...
|
|
||||||
def update_lines_cols() -> None: ...
|
|
||||||
def use_default_colors() -> None: ...
|
|
||||||
def use_env(flag: bool, /) -> None: ...
|
|
||||||
|
|
||||||
class error(Exception): ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class _CursesWindow:
|
|
||||||
encoding: str
|
|
||||||
@overload
|
|
||||||
def addch(self, ch: _ChType, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def addch(self, y: int, x: int, ch: _ChType, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def addnstr(self, str: str, n: int, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def addnstr(self, y: int, x: int, str: str, n: int, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def addstr(self, str: str, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def addstr(self, y: int, x: int, str: str, attr: int = ...) -> None: ...
|
|
||||||
def attroff(self, attr: int, /) -> None: ...
|
|
||||||
def attron(self, attr: int, /) -> None: ...
|
|
||||||
def attrset(self, attr: int, /) -> None: ...
|
|
||||||
def bkgd(self, ch: _ChType, attr: int = ..., /) -> None: ...
|
|
||||||
def bkgdset(self, ch: _ChType, attr: int = ..., /) -> None: ...
|
|
||||||
def border(
|
|
||||||
self,
|
|
||||||
ls: _ChType = ...,
|
|
||||||
rs: _ChType = ...,
|
|
||||||
ts: _ChType = ...,
|
|
||||||
bs: _ChType = ...,
|
|
||||||
tl: _ChType = ...,
|
|
||||||
tr: _ChType = ...,
|
|
||||||
bl: _ChType = ...,
|
|
||||||
br: _ChType = ...,
|
|
||||||
) -> None: ...
|
|
||||||
@overload
|
|
||||||
def box(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def box(self, vertch: _ChType = ..., horch: _ChType = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def chgat(self, attr: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def chgat(self, num: int, attr: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def chgat(self, y: int, x: int, attr: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def chgat(self, y: int, x: int, num: int, attr: int) -> None: ...
|
|
||||||
def clear(self) -> None: ...
|
|
||||||
def clearok(self, yes: int) -> None: ...
|
|
||||||
def clrtobot(self) -> None: ...
|
|
||||||
def clrtoeol(self) -> None: ...
|
|
||||||
def cursyncup(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def delch(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def delch(self, y: int, x: int) -> None: ...
|
|
||||||
def deleteln(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def derwin(self, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
@overload
|
|
||||||
def derwin(self, nlines: int, ncols: int, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
def echochar(self, ch: _ChType, attr: int = ..., /) -> None: ...
|
|
||||||
def enclose(self, y: int, x: int, /) -> bool: ...
|
|
||||||
def erase(self) -> None: ...
|
|
||||||
def getbegyx(self) -> tuple[int, int]: ...
|
|
||||||
def getbkgd(self) -> tuple[int, int]: ...
|
|
||||||
@overload
|
|
||||||
def getch(self) -> int: ...
|
|
||||||
@overload
|
|
||||||
def getch(self, y: int, x: int) -> int: ...
|
|
||||||
if sys.version_info < (3, 12) or sys.platform != "darwin":
|
|
||||||
# The support for macos was dropped in 3.12
|
|
||||||
@overload
|
|
||||||
def get_wch(self) -> int | str: ...
|
|
||||||
@overload
|
|
||||||
def get_wch(self, y: int, x: int) -> int | str: ...
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def getkey(self) -> str: ...
|
|
||||||
@overload
|
|
||||||
def getkey(self, y: int, x: int) -> str: ...
|
|
||||||
def getmaxyx(self) -> tuple[int, int]: ...
|
|
||||||
def getparyx(self) -> tuple[int, int]: ...
|
|
||||||
@overload
|
|
||||||
def getstr(self) -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def getstr(self, n: int) -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def getstr(self, y: int, x: int) -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def getstr(self, y: int, x: int, n: int) -> bytes: ...
|
|
||||||
def getyx(self) -> tuple[int, int]: ...
|
|
||||||
@overload
|
|
||||||
def hline(self, ch: _ChType, n: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def hline(self, y: int, x: int, ch: _ChType, n: int) -> None: ...
|
|
||||||
def idcok(self, flag: bool) -> None: ...
|
|
||||||
def idlok(self, yes: bool) -> None: ...
|
|
||||||
def immedok(self, flag: bool) -> None: ...
|
|
||||||
@overload
|
|
||||||
def inch(self) -> int: ...
|
|
||||||
@overload
|
|
||||||
def inch(self, y: int, x: int) -> int: ...
|
|
||||||
@overload
|
|
||||||
def insch(self, ch: _ChType, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insch(self, y: int, x: int, ch: _ChType, attr: int = ...) -> None: ...
|
|
||||||
def insdelln(self, nlines: int) -> None: ...
|
|
||||||
def insertln(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insnstr(self, str: str, n: int, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insnstr(self, y: int, x: int, str: str, n: int, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insstr(self, str: str, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def insstr(self, y: int, x: int, str: str, attr: int = ...) -> None: ...
|
|
||||||
@overload
|
|
||||||
def instr(self, n: int = ...) -> bytes: ...
|
|
||||||
@overload
|
|
||||||
def instr(self, y: int, x: int, n: int = ...) -> bytes: ...
|
|
||||||
def is_linetouched(self, line: int, /) -> bool: ...
|
|
||||||
def is_wintouched(self) -> bool: ...
|
|
||||||
def keypad(self, yes: bool) -> None: ...
|
|
||||||
def leaveok(self, yes: bool) -> None: ...
|
|
||||||
def move(self, new_y: int, new_x: int) -> None: ...
|
|
||||||
def mvderwin(self, y: int, x: int) -> None: ...
|
|
||||||
def mvwin(self, new_y: int, new_x: int) -> None: ...
|
|
||||||
def nodelay(self, yes: bool) -> None: ...
|
|
||||||
def notimeout(self, yes: bool) -> None: ...
|
|
||||||
@overload
|
|
||||||
def noutrefresh(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def noutrefresh(self, pminrow: int, pmincol: int, sminrow: int, smincol: int, smaxrow: int, smaxcol: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def overlay(self, destwin: _CursesWindow) -> None: ...
|
|
||||||
@overload
|
|
||||||
def overlay(
|
|
||||||
self, destwin: _CursesWindow, sminrow: int, smincol: int, dminrow: int, dmincol: int, dmaxrow: int, dmaxcol: int
|
|
||||||
) -> None: ...
|
|
||||||
@overload
|
|
||||||
def overwrite(self, destwin: _CursesWindow) -> None: ...
|
|
||||||
@overload
|
|
||||||
def overwrite(
|
|
||||||
self, destwin: _CursesWindow, sminrow: int, smincol: int, dminrow: int, dmincol: int, dmaxrow: int, dmaxcol: int
|
|
||||||
) -> None: ...
|
|
||||||
def putwin(self, file: IO[Any], /) -> None: ...
|
|
||||||
def redrawln(self, beg: int, num: int, /) -> None: ...
|
|
||||||
def redrawwin(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def refresh(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def refresh(self, pminrow: int, pmincol: int, sminrow: int, smincol: int, smaxrow: int, smaxcol: int) -> None: ...
|
|
||||||
def resize(self, nlines: int, ncols: int) -> None: ...
|
|
||||||
def scroll(self, lines: int = ...) -> None: ...
|
|
||||||
def scrollok(self, flag: bool) -> None: ...
|
|
||||||
def setscrreg(self, top: int, bottom: int, /) -> None: ...
|
|
||||||
def standend(self) -> None: ...
|
|
||||||
def standout(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def subpad(self, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
@overload
|
|
||||||
def subpad(self, nlines: int, ncols: int, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
@overload
|
|
||||||
def subwin(self, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
@overload
|
|
||||||
def subwin(self, nlines: int, ncols: int, begin_y: int, begin_x: int) -> _CursesWindow: ...
|
|
||||||
def syncdown(self) -> None: ...
|
|
||||||
def syncok(self, flag: bool) -> None: ...
|
|
||||||
def syncup(self) -> None: ...
|
|
||||||
def timeout(self, delay: int) -> None: ...
|
|
||||||
def touchline(self, start: int, count: int, changed: bool = ...) -> None: ...
|
|
||||||
def touchwin(self) -> None: ...
|
|
||||||
def untouchwin(self) -> None: ...
|
|
||||||
@overload
|
|
||||||
def vline(self, ch: _ChType, n: int) -> None: ...
|
|
||||||
@overload
|
|
||||||
def vline(self, y: int, x: int, ch: _ChType, n: int) -> None: ...
|
|
||||||
|
|
||||||
class _ncurses_version(NamedTuple):
|
|
||||||
major: int
|
|
||||||
minor: int
|
|
||||||
patch: int
|
|
||||||
|
|
||||||
ncurses_version: _ncurses_version
|
|
||||||
window = _CursesWindow # undocumented
|
|
||||||
@@ -1,281 +0,0 @@
|
|||||||
import numbers
|
|
||||||
import sys
|
|
||||||
from collections.abc import Container, Sequence
|
|
||||||
from types import TracebackType
|
|
||||||
from typing import Any, ClassVar, Final, Literal, NamedTuple, overload
|
|
||||||
from typing_extensions import Self, TypeAlias
|
|
||||||
|
|
||||||
_Decimal: TypeAlias = Decimal | int
|
|
||||||
_DecimalNew: TypeAlias = Decimal | float | str | tuple[int, Sequence[int], int]
|
|
||||||
_ComparableNum: TypeAlias = Decimal | float | numbers.Rational
|
|
||||||
|
|
||||||
__version__: Final[str]
|
|
||||||
__libmpdec_version__: Final[str]
|
|
||||||
|
|
||||||
class DecimalTuple(NamedTuple):
|
|
||||||
sign: int
|
|
||||||
digits: tuple[int, ...]
|
|
||||||
exponent: int | Literal["n", "N", "F"]
|
|
||||||
|
|
||||||
ROUND_DOWN: str
|
|
||||||
ROUND_HALF_UP: str
|
|
||||||
ROUND_HALF_EVEN: str
|
|
||||||
ROUND_CEILING: str
|
|
||||||
ROUND_FLOOR: str
|
|
||||||
ROUND_UP: str
|
|
||||||
ROUND_HALF_DOWN: str
|
|
||||||
ROUND_05UP: str
|
|
||||||
HAVE_CONTEXTVAR: bool
|
|
||||||
HAVE_THREADS: bool
|
|
||||||
MAX_EMAX: int
|
|
||||||
MAX_PREC: int
|
|
||||||
MIN_EMIN: int
|
|
||||||
MIN_ETINY: int
|
|
||||||
|
|
||||||
class DecimalException(ArithmeticError): ...
|
|
||||||
class Clamped(DecimalException): ...
|
|
||||||
class InvalidOperation(DecimalException): ...
|
|
||||||
class ConversionSyntax(InvalidOperation): ...
|
|
||||||
class DivisionByZero(DecimalException, ZeroDivisionError): ...
|
|
||||||
class DivisionImpossible(InvalidOperation): ...
|
|
||||||
class DivisionUndefined(InvalidOperation, ZeroDivisionError): ...
|
|
||||||
class Inexact(DecimalException): ...
|
|
||||||
class InvalidContext(InvalidOperation): ...
|
|
||||||
class Rounded(DecimalException): ...
|
|
||||||
class Subnormal(DecimalException): ...
|
|
||||||
class Overflow(Inexact, Rounded): ...
|
|
||||||
class Underflow(Inexact, Rounded, Subnormal): ...
|
|
||||||
class FloatOperation(DecimalException, TypeError): ...
|
|
||||||
|
|
||||||
def setcontext(context: Context, /) -> None: ...
|
|
||||||
def getcontext() -> Context: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
def localcontext(
|
|
||||||
ctx: Context | None = None,
|
|
||||||
*,
|
|
||||||
prec: int | None = ...,
|
|
||||||
rounding: str | None = ...,
|
|
||||||
Emin: int | None = ...,
|
|
||||||
Emax: int | None = ...,
|
|
||||||
capitals: int | None = ...,
|
|
||||||
clamp: int | None = ...,
|
|
||||||
traps: dict[_TrapType, bool] | None = ...,
|
|
||||||
flags: dict[_TrapType, bool] | None = ...,
|
|
||||||
) -> _ContextManager: ...
|
|
||||||
|
|
||||||
else:
|
|
||||||
def localcontext(ctx: Context | None = None) -> _ContextManager: ...
|
|
||||||
|
|
||||||
class Decimal:
|
|
||||||
def __new__(cls, value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
|
|
||||||
@classmethod
|
|
||||||
def from_float(cls, f: float, /) -> Self: ...
|
|
||||||
def __bool__(self) -> bool: ...
|
|
||||||
def compare(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def __hash__(self) -> int: ...
|
|
||||||
def as_tuple(self) -> DecimalTuple: ...
|
|
||||||
def as_integer_ratio(self) -> tuple[int, int]: ...
|
|
||||||
def to_eng_string(self, context: Context | None = None) -> str: ...
|
|
||||||
def __abs__(self) -> Decimal: ...
|
|
||||||
def __add__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __divmod__(self, value: _Decimal, /) -> tuple[Decimal, Decimal]: ...
|
|
||||||
def __eq__(self, value: object, /) -> bool: ...
|
|
||||||
def __floordiv__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __ge__(self, value: _ComparableNum, /) -> bool: ...
|
|
||||||
def __gt__(self, value: _ComparableNum, /) -> bool: ...
|
|
||||||
def __le__(self, value: _ComparableNum, /) -> bool: ...
|
|
||||||
def __lt__(self, value: _ComparableNum, /) -> bool: ...
|
|
||||||
def __mod__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __mul__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __neg__(self) -> Decimal: ...
|
|
||||||
def __pos__(self) -> Decimal: ...
|
|
||||||
def __pow__(self, value: _Decimal, mod: _Decimal | None = None, /) -> Decimal: ...
|
|
||||||
def __radd__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __rdivmod__(self, value: _Decimal, /) -> tuple[Decimal, Decimal]: ...
|
|
||||||
def __rfloordiv__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __rmod__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __rmul__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __rsub__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __rtruediv__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __sub__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def __truediv__(self, value: _Decimal, /) -> Decimal: ...
|
|
||||||
def remainder_near(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def __float__(self) -> float: ...
|
|
||||||
def __int__(self) -> int: ...
|
|
||||||
def __trunc__(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def real(self) -> Decimal: ...
|
|
||||||
@property
|
|
||||||
def imag(self) -> Decimal: ...
|
|
||||||
def conjugate(self) -> Decimal: ...
|
|
||||||
def __complex__(self) -> complex: ...
|
|
||||||
@overload
|
|
||||||
def __round__(self) -> int: ...
|
|
||||||
@overload
|
|
||||||
def __round__(self, ndigits: int, /) -> Decimal: ...
|
|
||||||
def __floor__(self) -> int: ...
|
|
||||||
def __ceil__(self) -> int: ...
|
|
||||||
def fma(self, other: _Decimal, third: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def __rpow__(self, value: _Decimal, mod: Context | None = None, /) -> Decimal: ...
|
|
||||||
def normalize(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def quantize(self, exp: _Decimal, rounding: str | None = None, context: Context | None = None) -> Decimal: ...
|
|
||||||
def same_quantum(self, other: _Decimal, context: Context | None = None) -> bool: ...
|
|
||||||
def to_integral_exact(self, rounding: str | None = None, context: Context | None = None) -> Decimal: ...
|
|
||||||
def to_integral_value(self, rounding: str | None = None, context: Context | None = None) -> Decimal: ...
|
|
||||||
def to_integral(self, rounding: str | None = None, context: Context | None = None) -> Decimal: ...
|
|
||||||
def sqrt(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def max(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def min(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def adjusted(self) -> int: ...
|
|
||||||
def canonical(self) -> Decimal: ...
|
|
||||||
def compare_signal(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def compare_total(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def compare_total_mag(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def copy_abs(self) -> Decimal: ...
|
|
||||||
def copy_negate(self) -> Decimal: ...
|
|
||||||
def copy_sign(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def exp(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def is_canonical(self) -> bool: ...
|
|
||||||
def is_finite(self) -> bool: ...
|
|
||||||
def is_infinite(self) -> bool: ...
|
|
||||||
def is_nan(self) -> bool: ...
|
|
||||||
def is_normal(self, context: Context | None = None) -> bool: ...
|
|
||||||
def is_qnan(self) -> bool: ...
|
|
||||||
def is_signed(self) -> bool: ...
|
|
||||||
def is_snan(self) -> bool: ...
|
|
||||||
def is_subnormal(self, context: Context | None = None) -> bool: ...
|
|
||||||
def is_zero(self) -> bool: ...
|
|
||||||
def ln(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def log10(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def logb(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def logical_and(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def logical_invert(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def logical_or(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def logical_xor(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def max_mag(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def min_mag(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def next_minus(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def next_plus(self, context: Context | None = None) -> Decimal: ...
|
|
||||||
def next_toward(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def number_class(self, context: Context | None = None) -> str: ...
|
|
||||||
def radix(self) -> Decimal: ...
|
|
||||||
def rotate(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def scaleb(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def shift(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
|
|
||||||
def __reduce__(self) -> tuple[type[Self], tuple[str]]: ...
|
|
||||||
def __copy__(self) -> Self: ...
|
|
||||||
def __deepcopy__(self, memo: Any, /) -> Self: ...
|
|
||||||
def __format__(self, specifier: str, context: Context | None = ..., /) -> str: ...
|
|
||||||
|
|
||||||
class _ContextManager:
|
|
||||||
new_context: Context
|
|
||||||
saved_context: Context
|
|
||||||
def __init__(self, new_context: Context) -> None: ...
|
|
||||||
def __enter__(self) -> Context: ...
|
|
||||||
def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ...
|
|
||||||
|
|
||||||
_TrapType: TypeAlias = type[DecimalException]
|
|
||||||
|
|
||||||
class Context:
|
|
||||||
# TODO: Context doesn't allow you to delete *any* attributes from instances of the class at runtime,
|
|
||||||
# even settable attributes like `prec` and `rounding`,
|
|
||||||
# but that's inexpressable in the stub.
|
|
||||||
# Type checkers either ignore it or misinterpret it
|
|
||||||
# if you add a `def __delattr__(self, name: str, /) -> NoReturn` method to the stub
|
|
||||||
prec: int
|
|
||||||
rounding: str
|
|
||||||
Emin: int
|
|
||||||
Emax: int
|
|
||||||
capitals: int
|
|
||||||
clamp: int
|
|
||||||
traps: dict[_TrapType, bool]
|
|
||||||
flags: dict[_TrapType, bool]
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
prec: int | None = ...,
|
|
||||||
rounding: str | None = ...,
|
|
||||||
Emin: int | None = ...,
|
|
||||||
Emax: int | None = ...,
|
|
||||||
capitals: int | None = ...,
|
|
||||||
clamp: int | None = ...,
|
|
||||||
flags: None | dict[_TrapType, bool] | Container[_TrapType] = ...,
|
|
||||||
traps: None | dict[_TrapType, bool] | Container[_TrapType] = ...,
|
|
||||||
_ignored_flags: list[_TrapType] | None = ...,
|
|
||||||
) -> None: ...
|
|
||||||
def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ...
|
|
||||||
def clear_flags(self) -> None: ...
|
|
||||||
def clear_traps(self) -> None: ...
|
|
||||||
def copy(self) -> Context: ...
|
|
||||||
def __copy__(self) -> Context: ...
|
|
||||||
# see https://github.com/python/cpython/issues/94107
|
|
||||||
__hash__: ClassVar[None] # type: ignore[assignment]
|
|
||||||
def Etiny(self) -> int: ...
|
|
||||||
def Etop(self) -> int: ...
|
|
||||||
def create_decimal(self, num: _DecimalNew = "0", /) -> Decimal: ...
|
|
||||||
def create_decimal_from_float(self, f: float, /) -> Decimal: ...
|
|
||||||
def abs(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def add(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def canonical(self, x: Decimal, /) -> Decimal: ...
|
|
||||||
def compare(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def compare_signal(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def compare_total(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def compare_total_mag(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def copy_abs(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def copy_decimal(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def copy_negate(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def copy_sign(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def divide(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def divide_int(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def divmod(self, x: _Decimal, y: _Decimal, /) -> tuple[Decimal, Decimal]: ...
|
|
||||||
def exp(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def fma(self, x: _Decimal, y: _Decimal, z: _Decimal, /) -> Decimal: ...
|
|
||||||
def is_canonical(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_finite(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_infinite(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_nan(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_normal(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_qnan(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_signed(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_snan(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_subnormal(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def is_zero(self, x: _Decimal, /) -> bool: ...
|
|
||||||
def ln(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def log10(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def logb(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def logical_and(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def logical_invert(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def logical_or(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def logical_xor(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def max(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def max_mag(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def min(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def min_mag(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def minus(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def multiply(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def next_minus(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def next_plus(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def next_toward(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def normalize(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def number_class(self, x: _Decimal, /) -> str: ...
|
|
||||||
def plus(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def power(self, a: _Decimal, b: _Decimal, modulo: _Decimal | None = None) -> Decimal: ...
|
|
||||||
def quantize(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def radix(self) -> Decimal: ...
|
|
||||||
def remainder(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def remainder_near(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def rotate(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def same_quantum(self, x: _Decimal, y: _Decimal, /) -> bool: ...
|
|
||||||
def scaleb(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def shift(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def sqrt(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def subtract(self, x: _Decimal, y: _Decimal, /) -> Decimal: ...
|
|
||||||
def to_eng_string(self, x: _Decimal, /) -> str: ...
|
|
||||||
def to_sci_string(self, x: _Decimal, /) -> str: ...
|
|
||||||
def to_integral_exact(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def to_integral_value(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
def to_integral(self, x: _Decimal, /) -> Decimal: ...
|
|
||||||
|
|
||||||
DefaultContext: Context
|
|
||||||
BasicContext: Context
|
|
||||||
ExtendedContext: Context
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
from collections.abc import Callable
|
|
||||||
from types import TracebackType
|
|
||||||
from typing import Any, NoReturn, overload
|
|
||||||
from typing_extensions import TypeVarTuple, Unpack
|
|
||||||
|
|
||||||
__all__ = ["error", "start_new_thread", "exit", "get_ident", "allocate_lock", "interrupt_main", "LockType", "RLock"]
|
|
||||||
|
|
||||||
_Ts = TypeVarTuple("_Ts")
|
|
||||||
|
|
||||||
TIMEOUT_MAX: int
|
|
||||||
error = RuntimeError
|
|
||||||
|
|
||||||
@overload
|
|
||||||
def start_new_thread(function: Callable[[Unpack[_Ts]], object], args: tuple[Unpack[_Ts]]) -> None: ...
|
|
||||||
@overload
|
|
||||||
def start_new_thread(function: Callable[..., object], args: tuple[Any, ...], kwargs: dict[str, Any]) -> None: ...
|
|
||||||
def exit() -> NoReturn: ...
|
|
||||||
def get_ident() -> int: ...
|
|
||||||
def allocate_lock() -> LockType: ...
|
|
||||||
def stack_size(size: int | None = None) -> int: ...
|
|
||||||
|
|
||||||
class LockType:
|
|
||||||
locked_status: bool
|
|
||||||
def acquire(self, waitflag: bool | None = None, timeout: int = -1) -> bool: ...
|
|
||||||
def __enter__(self, waitflag: bool | None = None, timeout: int = -1) -> bool: ...
|
|
||||||
def __exit__(self, typ: type[BaseException] | None, val: BaseException | None, tb: TracebackType | None) -> None: ...
|
|
||||||
def release(self) -> bool: ...
|
|
||||||
def locked(self) -> bool: ...
|
|
||||||
|
|
||||||
class RLock(LockType):
|
|
||||||
def release(self) -> None: ... # type: ignore[override]
|
|
||||||
|
|
||||||
def interrupt_main() -> None: ...
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _thread import _excepthook, _ExceptHookArgs
|
|
||||||
from _typeshed import ProfileFunction, TraceFunction
|
|
||||||
from collections.abc import Callable, Iterable, Mapping
|
|
||||||
from types import TracebackType
|
|
||||||
from typing import Any, TypeVar
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"get_ident",
|
|
||||||
"active_count",
|
|
||||||
"Condition",
|
|
||||||
"current_thread",
|
|
||||||
"enumerate",
|
|
||||||
"main_thread",
|
|
||||||
"TIMEOUT_MAX",
|
|
||||||
"Event",
|
|
||||||
"Lock",
|
|
||||||
"RLock",
|
|
||||||
"Semaphore",
|
|
||||||
"BoundedSemaphore",
|
|
||||||
"Thread",
|
|
||||||
"Barrier",
|
|
||||||
"BrokenBarrierError",
|
|
||||||
"Timer",
|
|
||||||
"ThreadError",
|
|
||||||
"setprofile",
|
|
||||||
"settrace",
|
|
||||||
"local",
|
|
||||||
"stack_size",
|
|
||||||
"ExceptHookArgs",
|
|
||||||
"excepthook",
|
|
||||||
]
|
|
||||||
|
|
||||||
def active_count() -> int: ...
|
|
||||||
def current_thread() -> Thread: ...
|
|
||||||
def currentThread() -> Thread: ...
|
|
||||||
def get_ident() -> int: ...
|
|
||||||
def enumerate() -> list[Thread]: ...
|
|
||||||
def main_thread() -> Thread: ...
|
|
||||||
def settrace(func: TraceFunction) -> None: ...
|
|
||||||
def setprofile(func: ProfileFunction | None) -> None: ...
|
|
||||||
def stack_size(size: int | None = None) -> int: ...
|
|
||||||
|
|
||||||
TIMEOUT_MAX: float
|
|
||||||
|
|
||||||
class ThreadError(Exception): ...
|
|
||||||
|
|
||||||
class local:
|
|
||||||
def __getattribute__(self, name: str) -> Any: ...
|
|
||||||
def __setattr__(self, name: str, value: Any) -> None: ...
|
|
||||||
def __delattr__(self, name: str) -> None: ...
|
|
||||||
|
|
||||||
class Thread:
|
|
||||||
name: str
|
|
||||||
daemon: bool
|
|
||||||
@property
|
|
||||||
def ident(self) -> int | None: ...
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
group: None = None,
|
|
||||||
target: Callable[..., object] | None = None,
|
|
||||||
name: str | None = None,
|
|
||||||
args: Iterable[Any] = (),
|
|
||||||
kwargs: Mapping[str, Any] | None = None,
|
|
||||||
*,
|
|
||||||
daemon: bool | None = None,
|
|
||||||
) -> None: ...
|
|
||||||
def start(self) -> None: ...
|
|
||||||
def run(self) -> None: ...
|
|
||||||
def join(self, timeout: float | None = None) -> None: ...
|
|
||||||
def getName(self) -> str: ...
|
|
||||||
def setName(self, name: str) -> None: ...
|
|
||||||
@property
|
|
||||||
def native_id(self) -> int | None: ... # only available on some platforms
|
|
||||||
def is_alive(self) -> bool: ...
|
|
||||||
if sys.version_info < (3, 9):
|
|
||||||
def isAlive(self) -> bool: ...
|
|
||||||
|
|
||||||
def isDaemon(self) -> bool: ...
|
|
||||||
def setDaemon(self, daemonic: bool) -> None: ...
|
|
||||||
|
|
||||||
class _DummyThread(Thread): ...
|
|
||||||
|
|
||||||
class Lock:
|
|
||||||
def __enter__(self) -> bool: ...
|
|
||||||
def __exit__(
|
|
||||||
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
|
|
||||||
) -> bool | None: ...
|
|
||||||
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
|
|
||||||
def release(self) -> None: ...
|
|
||||||
def locked(self) -> bool: ...
|
|
||||||
|
|
||||||
class _RLock:
|
|
||||||
def __enter__(self) -> bool: ...
|
|
||||||
def __exit__(
|
|
||||||
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
|
|
||||||
) -> bool | None: ...
|
|
||||||
def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ...
|
|
||||||
def release(self) -> None: ...
|
|
||||||
|
|
||||||
RLock = _RLock
|
|
||||||
|
|
||||||
class Condition:
|
|
||||||
def __init__(self, lock: Lock | _RLock | None = None) -> None: ...
|
|
||||||
def __enter__(self) -> bool: ...
|
|
||||||
def __exit__(
|
|
||||||
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
|
|
||||||
) -> bool | None: ...
|
|
||||||
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
|
|
||||||
def release(self) -> None: ...
|
|
||||||
def wait(self, timeout: float | None = None) -> bool: ...
|
|
||||||
def wait_for(self, predicate: Callable[[], _T], timeout: float | None = None) -> _T: ...
|
|
||||||
def notify(self, n: int = 1) -> None: ...
|
|
||||||
def notify_all(self) -> None: ...
|
|
||||||
def notifyAll(self) -> None: ...
|
|
||||||
|
|
||||||
class Semaphore:
|
|
||||||
def __init__(self, value: int = 1) -> None: ...
|
|
||||||
def __exit__(
|
|
||||||
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
|
|
||||||
) -> bool | None: ...
|
|
||||||
def acquire(self, blocking: bool = True, timeout: float | None = None) -> bool: ...
|
|
||||||
def __enter__(self, blocking: bool = True, timeout: float | None = None) -> bool: ...
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
def release(self, n: int = ...) -> None: ...
|
|
||||||
else:
|
|
||||||
def release(self) -> None: ...
|
|
||||||
|
|
||||||
class BoundedSemaphore(Semaphore): ...
|
|
||||||
|
|
||||||
class Event:
|
|
||||||
def is_set(self) -> bool: ...
|
|
||||||
def set(self) -> None: ...
|
|
||||||
def clear(self) -> None: ...
|
|
||||||
def wait(self, timeout: float | None = None) -> bool: ...
|
|
||||||
|
|
||||||
excepthook = _excepthook
|
|
||||||
ExceptHookArgs = _ExceptHookArgs
|
|
||||||
|
|
||||||
class Timer(Thread):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
interval: float,
|
|
||||||
function: Callable[..., object],
|
|
||||||
args: Iterable[Any] | None = None,
|
|
||||||
kwargs: Mapping[str, Any] | None = None,
|
|
||||||
) -> None: ...
|
|
||||||
def cancel(self) -> None: ...
|
|
||||||
|
|
||||||
class Barrier:
|
|
||||||
@property
|
|
||||||
def parties(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def n_waiting(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def broken(self) -> bool: ...
|
|
||||||
def __init__(self, parties: int, action: Callable[[], None] | None = None, timeout: float | None = None) -> None: ...
|
|
||||||
def wait(self, timeout: float | None = None) -> int: ...
|
|
||||||
def reset(self) -> None: ...
|
|
||||||
def abort(self) -> None: ...
|
|
||||||
|
|
||||||
class BrokenBarrierError(RuntimeError): ...
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
from typing import Any, Final, TypeVar
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
|
|
||||||
__about__: Final[str]
|
|
||||||
|
|
||||||
def heapify(heap: list[Any], /) -> None: ...
|
|
||||||
def heappop(heap: list[_T], /) -> _T: ...
|
|
||||||
def heappush(heap: list[_T], item: _T, /) -> None: ...
|
|
||||||
def heappushpop(heap: list[_T], item: _T, /) -> _T: ...
|
|
||||||
def heapreplace(heap: list[_T], item: _T, /) -> _T: ...
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import sys
|
|
||||||
import types
|
|
||||||
from _typeshed import ReadableBuffer
|
|
||||||
from importlib.machinery import ModuleSpec
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
check_hash_based_pycs: str
|
|
||||||
|
|
||||||
def source_hash(key: int, source: ReadableBuffer) -> bytes: ...
|
|
||||||
def create_builtin(spec: ModuleSpec, /) -> types.ModuleType: ...
|
|
||||||
def create_dynamic(spec: ModuleSpec, file: Any = None, /) -> types.ModuleType: ...
|
|
||||||
def acquire_lock() -> None: ...
|
|
||||||
def exec_builtin(mod: types.ModuleType, /) -> int: ...
|
|
||||||
def exec_dynamic(mod: types.ModuleType, /) -> int: ...
|
|
||||||
def extension_suffixes() -> list[str]: ...
|
|
||||||
def init_frozen(name: str, /) -> types.ModuleType: ...
|
|
||||||
def is_builtin(name: str, /) -> int: ...
|
|
||||||
def is_frozen(name: str, /) -> bool: ...
|
|
||||||
def is_frozen_package(name: str, /) -> bool: ...
|
|
||||||
def lock_held() -> bool: ...
|
|
||||||
def release_lock() -> None: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
def find_frozen(name: str, /, *, withdata: bool = False) -> tuple[memoryview | None, bool, str | None] | None: ...
|
|
||||||
def get_frozen_object(name: str, data: ReadableBuffer | None = None, /) -> types.CodeType: ...
|
|
||||||
|
|
||||||
else:
|
|
||||||
def get_frozen_object(name: str, /) -> types.CodeType: ...
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
from collections.abc import Callable
|
|
||||||
from typing import Any, final
|
|
||||||
|
|
||||||
@final
|
|
||||||
class make_encoder:
|
|
||||||
@property
|
|
||||||
def sort_keys(self) -> bool: ...
|
|
||||||
@property
|
|
||||||
def skipkeys(self) -> bool: ...
|
|
||||||
@property
|
|
||||||
def key_separator(self) -> str: ...
|
|
||||||
@property
|
|
||||||
def indent(self) -> int | None: ...
|
|
||||||
@property
|
|
||||||
def markers(self) -> dict[int, Any] | None: ...
|
|
||||||
@property
|
|
||||||
def default(self) -> Callable[[Any], Any]: ...
|
|
||||||
@property
|
|
||||||
def encoder(self) -> Callable[[str], str]: ...
|
|
||||||
@property
|
|
||||||
def item_separator(self) -> str: ...
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
markers: dict[int, Any] | None,
|
|
||||||
default: Callable[[Any], Any],
|
|
||||||
encoder: Callable[[str], str],
|
|
||||||
indent: int | None,
|
|
||||||
key_separator: str,
|
|
||||||
item_separator: str,
|
|
||||||
sort_keys: bool,
|
|
||||||
skipkeys: bool,
|
|
||||||
allow_nan: bool,
|
|
||||||
) -> None: ...
|
|
||||||
def __call__(self, obj: object, _current_indent_level: int) -> Any: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class make_scanner:
|
|
||||||
object_hook: Any
|
|
||||||
object_pairs_hook: Any
|
|
||||||
parse_int: Any
|
|
||||||
parse_constant: Any
|
|
||||||
parse_float: Any
|
|
||||||
strict: bool
|
|
||||||
# TODO: 'context' needs the attrs above (ducktype), but not __call__.
|
|
||||||
def __init__(self, context: make_scanner) -> None: ...
|
|
||||||
def __call__(self, string: str, index: int) -> tuple[Any, int]: ...
|
|
||||||
|
|
||||||
def encode_basestring_ascii(s: str, /) -> str: ...
|
|
||||||
def scanstring(string: str, end: int, strict: bool = ...) -> tuple[str, int]: ...
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import StrPath
|
|
||||||
from collections.abc import Mapping
|
|
||||||
|
|
||||||
LC_CTYPE: int
|
|
||||||
LC_COLLATE: int
|
|
||||||
LC_TIME: int
|
|
||||||
LC_MONETARY: int
|
|
||||||
LC_NUMERIC: int
|
|
||||||
LC_ALL: int
|
|
||||||
CHAR_MAX: int
|
|
||||||
|
|
||||||
def setlocale(category: int, locale: str | None = None, /) -> str: ...
|
|
||||||
def localeconv() -> Mapping[str, int | str | list[int]]: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
def getencoding() -> str: ...
|
|
||||||
|
|
||||||
def strcoll(os1: str, os2: str, /) -> int: ...
|
|
||||||
def strxfrm(string: str, /) -> str: ...
|
|
||||||
|
|
||||||
# native gettext functions
|
|
||||||
# https://docs.python.org/3/library/locale.html#access-to-message-catalogs
|
|
||||||
# https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Modules/_localemodule.c#L626
|
|
||||||
if sys.platform != "win32":
|
|
||||||
LC_MESSAGES: int
|
|
||||||
|
|
||||||
ABDAY_1: int
|
|
||||||
ABDAY_2: int
|
|
||||||
ABDAY_3: int
|
|
||||||
ABDAY_4: int
|
|
||||||
ABDAY_5: int
|
|
||||||
ABDAY_6: int
|
|
||||||
ABDAY_7: int
|
|
||||||
|
|
||||||
ABMON_1: int
|
|
||||||
ABMON_2: int
|
|
||||||
ABMON_3: int
|
|
||||||
ABMON_4: int
|
|
||||||
ABMON_5: int
|
|
||||||
ABMON_6: int
|
|
||||||
ABMON_7: int
|
|
||||||
ABMON_8: int
|
|
||||||
ABMON_9: int
|
|
||||||
ABMON_10: int
|
|
||||||
ABMON_11: int
|
|
||||||
ABMON_12: int
|
|
||||||
|
|
||||||
DAY_1: int
|
|
||||||
DAY_2: int
|
|
||||||
DAY_3: int
|
|
||||||
DAY_4: int
|
|
||||||
DAY_5: int
|
|
||||||
DAY_6: int
|
|
||||||
DAY_7: int
|
|
||||||
|
|
||||||
ERA: int
|
|
||||||
ERA_D_T_FMT: int
|
|
||||||
ERA_D_FMT: int
|
|
||||||
ERA_T_FMT: int
|
|
||||||
|
|
||||||
MON_1: int
|
|
||||||
MON_2: int
|
|
||||||
MON_3: int
|
|
||||||
MON_4: int
|
|
||||||
MON_5: int
|
|
||||||
MON_6: int
|
|
||||||
MON_7: int
|
|
||||||
MON_8: int
|
|
||||||
MON_9: int
|
|
||||||
MON_10: int
|
|
||||||
MON_11: int
|
|
||||||
MON_12: int
|
|
||||||
|
|
||||||
CODESET: int
|
|
||||||
D_T_FMT: int
|
|
||||||
D_FMT: int
|
|
||||||
T_FMT: int
|
|
||||||
T_FMT_AMPM: int
|
|
||||||
AM_STR: int
|
|
||||||
PM_STR: int
|
|
||||||
|
|
||||||
RADIXCHAR: int
|
|
||||||
THOUSEP: int
|
|
||||||
YESEXPR: int
|
|
||||||
NOEXPR: int
|
|
||||||
CRNCYSTR: int
|
|
||||||
ALT_DIGITS: int
|
|
||||||
|
|
||||||
def nl_langinfo(key: int, /) -> str: ...
|
|
||||||
|
|
||||||
# This is dependent on `libintl.h` which is a part of `gettext`
|
|
||||||
# system dependency. These functions might be missing.
|
|
||||||
# But, we always say that they are present.
|
|
||||||
def gettext(msg: str, /) -> str: ...
|
|
||||||
def dgettext(domain: str | None, msg: str, /) -> str: ...
|
|
||||||
def dcgettext(domain: str | None, msg: str, category: int, /) -> str: ...
|
|
||||||
def textdomain(domain: str | None, /) -> str: ...
|
|
||||||
def bindtextdomain(domain: str, dir: StrPath | None, /) -> str: ...
|
|
||||||
def bind_textdomain_codeset(domain: str, codeset: str | None, /) -> str | None: ...
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import structseq
|
|
||||||
from collections.abc import Callable
|
|
||||||
from types import CodeType
|
|
||||||
from typing import Any, Final, final
|
|
||||||
|
|
||||||
class Profiler:
|
|
||||||
def __init__(
|
|
||||||
self, timer: Callable[[], float] | None = None, timeunit: float = 0.0, subcalls: bool = True, builtins: bool = True
|
|
||||||
) -> None: ...
|
|
||||||
def getstats(self) -> list[profiler_entry]: ...
|
|
||||||
def enable(self, subcalls: bool = True, builtins: bool = True) -> None: ...
|
|
||||||
def disable(self) -> None: ...
|
|
||||||
def clear(self) -> None: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class profiler_entry(structseq[Any], tuple[CodeType | str, int, int, float, float, list[profiler_subentry]]):
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
__match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime", "calls")
|
|
||||||
code: CodeType | str
|
|
||||||
callcount: int
|
|
||||||
reccallcount: int
|
|
||||||
totaltime: float
|
|
||||||
inlinetime: float
|
|
||||||
calls: list[profiler_subentry]
|
|
||||||
|
|
||||||
@final
|
|
||||||
class profiler_subentry(structseq[Any], tuple[CodeType | str, int, int, float, float]):
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
__match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime")
|
|
||||||
code: CodeType | str
|
|
||||||
callcount: int
|
|
||||||
reccallcount: int
|
|
||||||
totaltime: float
|
|
||||||
inlinetime: float
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import sys
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
class ParserBase:
|
|
||||||
def reset(self) -> None: ...
|
|
||||||
def getpos(self) -> tuple[int, int]: ...
|
|
||||||
def unknown_decl(self, data: str) -> None: ...
|
|
||||||
def parse_comment(self, i: int, report: int = 1) -> int: ... # undocumented
|
|
||||||
def parse_declaration(self, i: int) -> int: ... # undocumented
|
|
||||||
def parse_marked_section(self, i: int, report: int = 1) -> int: ... # undocumented
|
|
||||||
def updatepos(self, i: int, j: int) -> int: ... # undocumented
|
|
||||||
if sys.version_info < (3, 10):
|
|
||||||
# Removed from ParserBase: https://bugs.python.org/issue31844
|
|
||||||
def error(self, message: str) -> Any: ... # undocumented
|
|
||||||
lineno: int # undocumented
|
|
||||||
offset: int # undocumented
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
import sys
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
|
||||||
class MSIError(Exception): ...
|
|
||||||
# Actual typename View, not exposed by the implementation
|
|
||||||
class _View:
|
|
||||||
def Execute(self, params: _Record | None = ...) -> None: ...
|
|
||||||
def GetColumnInfo(self, kind: int) -> _Record: ...
|
|
||||||
def Fetch(self) -> _Record: ...
|
|
||||||
def Modify(self, mode: int, record: _Record) -> None: ...
|
|
||||||
def Close(self) -> None: ...
|
|
||||||
# Don't exist at runtime
|
|
||||||
__new__: None # type: ignore[assignment]
|
|
||||||
__init__: None # type: ignore[assignment]
|
|
||||||
|
|
||||||
# Actual typename SummaryInformation, not exposed by the implementation
|
|
||||||
class _SummaryInformation:
|
|
||||||
def GetProperty(self, field: int) -> int | bytes | None: ...
|
|
||||||
def GetPropertyCount(self) -> int: ...
|
|
||||||
def SetProperty(self, field: int, value: int | str) -> None: ...
|
|
||||||
def Persist(self) -> None: ...
|
|
||||||
# Don't exist at runtime
|
|
||||||
__new__: None # type: ignore[assignment]
|
|
||||||
__init__: None # type: ignore[assignment]
|
|
||||||
|
|
||||||
# Actual typename Database, not exposed by the implementation
|
|
||||||
class _Database:
|
|
||||||
def OpenView(self, sql: str) -> _View: ...
|
|
||||||
def Commit(self) -> None: ...
|
|
||||||
def GetSummaryInformation(self, updateCount: int) -> _SummaryInformation: ...
|
|
||||||
def Close(self) -> None: ...
|
|
||||||
# Don't exist at runtime
|
|
||||||
__new__: None # type: ignore[assignment]
|
|
||||||
__init__: None # type: ignore[assignment]
|
|
||||||
|
|
||||||
# Actual typename Record, not exposed by the implementation
|
|
||||||
class _Record:
|
|
||||||
def GetFieldCount(self) -> int: ...
|
|
||||||
def GetInteger(self, field: int) -> int: ...
|
|
||||||
def GetString(self, field: int) -> str: ...
|
|
||||||
def SetString(self, field: int, str: str) -> None: ...
|
|
||||||
def SetStream(self, field: int, stream: str) -> None: ...
|
|
||||||
def SetInteger(self, field: int, int: int) -> None: ...
|
|
||||||
def ClearData(self) -> None: ...
|
|
||||||
# Don't exist at runtime
|
|
||||||
__new__: None # type: ignore[assignment]
|
|
||||||
__init__: None # type: ignore[assignment]
|
|
||||||
|
|
||||||
def UuidCreate() -> str: ...
|
|
||||||
def FCICreate(cabname: str, files: list[str], /) -> None: ...
|
|
||||||
def OpenDatabase(path: str, persist: int, /) -> _Database: ...
|
|
||||||
def CreateRecord(count: int, /) -> _Record: ...
|
|
||||||
|
|
||||||
MSICOLINFO_NAMES: int
|
|
||||||
MSICOLINFO_TYPES: int
|
|
||||||
MSIDBOPEN_CREATE: int
|
|
||||||
MSIDBOPEN_CREATEDIRECT: int
|
|
||||||
MSIDBOPEN_DIRECT: int
|
|
||||||
MSIDBOPEN_PATCHFILE: int
|
|
||||||
MSIDBOPEN_READONLY: int
|
|
||||||
MSIDBOPEN_TRANSACT: int
|
|
||||||
MSIMODIFY_ASSIGN: int
|
|
||||||
MSIMODIFY_DELETE: int
|
|
||||||
MSIMODIFY_INSERT: int
|
|
||||||
MSIMODIFY_INSERT_TEMPORARY: int
|
|
||||||
MSIMODIFY_MERGE: int
|
|
||||||
MSIMODIFY_REFRESH: int
|
|
||||||
MSIMODIFY_REPLACE: int
|
|
||||||
MSIMODIFY_SEEK: int
|
|
||||||
MSIMODIFY_UPDATE: int
|
|
||||||
MSIMODIFY_VALIDATE: int
|
|
||||||
MSIMODIFY_VALIDATE_DELETE: int
|
|
||||||
MSIMODIFY_VALIDATE_FIELD: int
|
|
||||||
MSIMODIFY_VALIDATE_NEW: int
|
|
||||||
|
|
||||||
PID_APPNAME: int
|
|
||||||
PID_AUTHOR: int
|
|
||||||
PID_CHARCOUNT: int
|
|
||||||
PID_CODEPAGE: int
|
|
||||||
PID_COMMENTS: int
|
|
||||||
PID_CREATE_DTM: int
|
|
||||||
PID_KEYWORDS: int
|
|
||||||
PID_LASTAUTHOR: int
|
|
||||||
PID_LASTPRINTED: int
|
|
||||||
PID_LASTSAVE_DTM: int
|
|
||||||
PID_PAGECOUNT: int
|
|
||||||
PID_REVNUMBER: int
|
|
||||||
PID_SECURITY: int
|
|
||||||
PID_SUBJECT: int
|
|
||||||
PID_TEMPLATE: int
|
|
||||||
PID_TITLE: int
|
|
||||||
PID_WORDCOUNT: int
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import SupportsGetItem
|
|
||||||
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
|
|
||||||
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, SupportsIndex, TypeVar, final, overload
|
|
||||||
from typing_extensions import ParamSpec, TypeAlias, TypeVarTuple, Unpack
|
|
||||||
|
|
||||||
_R = TypeVar("_R")
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
_T_co = TypeVar("_T_co", covariant=True)
|
|
||||||
_T1 = TypeVar("_T1")
|
|
||||||
_T2 = TypeVar("_T2")
|
|
||||||
_K = TypeVar("_K")
|
|
||||||
_V = TypeVar("_V")
|
|
||||||
_P = ParamSpec("_P")
|
|
||||||
_Ts = TypeVarTuple("_Ts")
|
|
||||||
|
|
||||||
# The following protocols return "Any" instead of bool, since the comparison
|
|
||||||
# operators can be overloaded to return an arbitrary object. For example,
|
|
||||||
# the numpy.array comparison dunders return another numpy.array.
|
|
||||||
|
|
||||||
class _SupportsDunderLT(Protocol):
|
|
||||||
def __lt__(self, other: Any, /) -> Any: ...
|
|
||||||
|
|
||||||
class _SupportsDunderGT(Protocol):
|
|
||||||
def __gt__(self, other: Any, /) -> Any: ...
|
|
||||||
|
|
||||||
class _SupportsDunderLE(Protocol):
|
|
||||||
def __le__(self, other: Any, /) -> Any: ...
|
|
||||||
|
|
||||||
class _SupportsDunderGE(Protocol):
|
|
||||||
def __ge__(self, other: Any, /) -> Any: ...
|
|
||||||
|
|
||||||
_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
|
|
||||||
|
|
||||||
class _SupportsInversion(Protocol[_T_co]):
|
|
||||||
def __invert__(self) -> _T_co: ...
|
|
||||||
|
|
||||||
class _SupportsNeg(Protocol[_T_co]):
|
|
||||||
def __neg__(self) -> _T_co: ...
|
|
||||||
|
|
||||||
class _SupportsPos(Protocol[_T_co]):
|
|
||||||
def __pos__(self) -> _T_co: ...
|
|
||||||
|
|
||||||
# All four comparison functions must have the same signature, or we get false-positive errors
|
|
||||||
def lt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
|
|
||||||
def le(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
|
|
||||||
def eq(a: object, b: object, /) -> Any: ...
|
|
||||||
def ne(a: object, b: object, /) -> Any: ...
|
|
||||||
def ge(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
|
|
||||||
def gt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
|
|
||||||
def not_(a: object, /) -> bool: ...
|
|
||||||
def truth(a: object, /) -> bool: ...
|
|
||||||
def is_(a: object, b: object, /) -> bool: ...
|
|
||||||
def is_not(a: object, b: object, /) -> bool: ...
|
|
||||||
def abs(a: SupportsAbs[_T], /) -> _T: ...
|
|
||||||
def add(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def and_(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def floordiv(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def index(a: SupportsIndex, /) -> int: ...
|
|
||||||
def inv(a: _SupportsInversion[_T_co], /) -> _T_co: ...
|
|
||||||
def invert(a: _SupportsInversion[_T_co], /) -> _T_co: ...
|
|
||||||
def lshift(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def mod(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def mul(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def matmul(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def neg(a: _SupportsNeg[_T_co], /) -> _T_co: ...
|
|
||||||
def or_(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def pos(a: _SupportsPos[_T_co], /) -> _T_co: ...
|
|
||||||
def pow(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def rshift(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def sub(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def truediv(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def xor(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def concat(a: Sequence[_T], b: Sequence[_T], /) -> Sequence[_T]: ...
|
|
||||||
def contains(a: Container[object], b: object, /) -> bool: ...
|
|
||||||
def countOf(a: Iterable[object], b: object, /) -> int: ...
|
|
||||||
@overload
|
|
||||||
def delitem(a: MutableSequence[Any], b: SupportsIndex, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def delitem(a: MutableSequence[Any], b: slice, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def delitem(a: MutableMapping[_K, Any], b: _K, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def getitem(a: Sequence[_T], b: slice, /) -> Sequence[_T]: ...
|
|
||||||
@overload
|
|
||||||
def getitem(a: SupportsGetItem[_K, _V], b: _K, /) -> _V: ...
|
|
||||||
def indexOf(a: Iterable[_T], b: _T, /) -> int: ...
|
|
||||||
@overload
|
|
||||||
def setitem(a: MutableSequence[_T], b: SupportsIndex, c: _T, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def setitem(a: MutableSequence[_T], b: slice, c: Sequence[_T], /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def setitem(a: MutableMapping[_K, _V], b: _K, c: _V, /) -> None: ...
|
|
||||||
def length_hint(obj: object, default: int = 0, /) -> int: ...
|
|
||||||
@final
|
|
||||||
class attrgetter(Generic[_T_co]):
|
|
||||||
@overload
|
|
||||||
def __new__(cls, attr: str, /) -> attrgetter[Any]: ...
|
|
||||||
@overload
|
|
||||||
def __new__(cls, attr: str, attr2: str, /) -> attrgetter[tuple[Any, Any]]: ...
|
|
||||||
@overload
|
|
||||||
def __new__(cls, attr: str, attr2: str, attr3: str, /) -> attrgetter[tuple[Any, Any, Any]]: ...
|
|
||||||
@overload
|
|
||||||
def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ...
|
|
||||||
@overload
|
|
||||||
def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ...
|
|
||||||
def __call__(self, obj: Any, /) -> _T_co: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class itemgetter(Generic[_T_co]):
|
|
||||||
@overload
|
|
||||||
def __new__(cls, item: _T, /) -> itemgetter[_T]: ...
|
|
||||||
@overload
|
|
||||||
def __new__(cls, item1: _T1, item2: _T2, /, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
|
|
||||||
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
|
|
||||||
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
|
|
||||||
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"
|
|
||||||
# preventing [_T_co, ...] instead of [Any, ...]
|
|
||||||
#
|
|
||||||
# A suspected mypy issue prevents using [..., _T] instead of [..., Any] here.
|
|
||||||
# https://github.com/python/mypy/issues/14032
|
|
||||||
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
|
|
||||||
|
|
||||||
@final
|
|
||||||
class methodcaller:
|
|
||||||
def __init__(self, name: str, /, *args: Any, **kwargs: Any) -> None: ...
|
|
||||||
def __call__(self, obj: Any) -> Any: ...
|
|
||||||
|
|
||||||
def iadd(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def iand(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def iconcat(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def ifloordiv(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def ilshift(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def imod(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def imul(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def imatmul(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def ior(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def ipow(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def irshift(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def isub(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def itruediv(a: Any, b: Any, /) -> Any: ...
|
|
||||||
def ixor(a: Any, b: Any, /) -> Any: ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
def call(obj: Callable[_P, _R], /, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
|
|
||||||
|
|
||||||
def _compare_digest(a: AnyStr, b: AnyStr, /) -> bool: ...
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
from collections.abc import Iterable, Sequence
|
|
||||||
from typing import TypeVar
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
_K = TypeVar("_K")
|
|
||||||
_V = TypeVar("_V")
|
|
||||||
|
|
||||||
__all__ = ["compiler_fixup", "customize_config_vars", "customize_compiler", "get_platform_osx"]
|
|
||||||
|
|
||||||
_UNIVERSAL_CONFIG_VARS: tuple[str, ...] # undocumented
|
|
||||||
_COMPILER_CONFIG_VARS: tuple[str, ...] # undocumented
|
|
||||||
_INITPRE: str # undocumented
|
|
||||||
|
|
||||||
def _find_executable(executable: str, path: str | None = None) -> str | None: ... # undocumented
|
|
||||||
def _read_output(commandstring: str, capture_stderr: bool = False) -> str | None: ... # undocumented
|
|
||||||
def _find_build_tool(toolname: str) -> str: ... # undocumented
|
|
||||||
|
|
||||||
_SYSTEM_VERSION: str | None # undocumented
|
|
||||||
|
|
||||||
def _get_system_version() -> str: ... # undocumented
|
|
||||||
def _remove_original_values(_config_vars: dict[str, str]) -> None: ... # undocumented
|
|
||||||
def _save_modified_value(_config_vars: dict[str, str], cv: str, newvalue: str) -> None: ... # undocumented
|
|
||||||
def _supports_universal_builds() -> bool: ... # undocumented
|
|
||||||
def _find_appropriate_compiler(_config_vars: dict[str, str]) -> dict[str, str]: ... # undocumented
|
|
||||||
def _remove_universal_flags(_config_vars: dict[str, str]) -> dict[str, str]: ... # undocumented
|
|
||||||
def _remove_unsupported_archs(_config_vars: dict[str, str]) -> dict[str, str]: ... # undocumented
|
|
||||||
def _override_all_archs(_config_vars: dict[str, str]) -> dict[str, str]: ... # undocumented
|
|
||||||
def _check_for_unavailable_sdk(_config_vars: dict[str, str]) -> dict[str, str]: ... # undocumented
|
|
||||||
def compiler_fixup(compiler_so: Iterable[str], cc_args: Sequence[str]) -> list[str]: ...
|
|
||||||
def customize_config_vars(_config_vars: dict[str, str]) -> dict[str, str]: ...
|
|
||||||
def customize_compiler(_config_vars: dict[str, str]) -> dict[str, str]: ...
|
|
||||||
def get_platform_osx(
|
|
||||||
_config_vars: dict[str, str], osname: _T, release: _K, machine: _V
|
|
||||||
) -> tuple[str | _T, str | _K, str | _V]: ...
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import StrOrBytesPath
|
|
||||||
from collections.abc import Callable, Sequence
|
|
||||||
from typing import SupportsIndex
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
def cloexec_pipe() -> tuple[int, int]: ...
|
|
||||||
def fork_exec(
|
|
||||||
args: Sequence[StrOrBytesPath] | None,
|
|
||||||
executable_list: Sequence[bytes],
|
|
||||||
close_fds: bool,
|
|
||||||
pass_fds: tuple[int, ...],
|
|
||||||
cwd: str,
|
|
||||||
env: Sequence[bytes] | None,
|
|
||||||
p2cread: int,
|
|
||||||
p2cwrite: int,
|
|
||||||
c2pread: int,
|
|
||||||
c2pwrite: int,
|
|
||||||
errread: int,
|
|
||||||
errwrite: int,
|
|
||||||
errpipe_read: int,
|
|
||||||
errpipe_write: int,
|
|
||||||
restore_signals: int,
|
|
||||||
call_setsid: int,
|
|
||||||
pgid_to_set: int,
|
|
||||||
gid: SupportsIndex | None,
|
|
||||||
extra_groups: list[int] | None,
|
|
||||||
uid: SupportsIndex | None,
|
|
||||||
child_umask: int,
|
|
||||||
preexec_fn: Callable[[], None],
|
|
||||||
allow_vfork: bool,
|
|
||||||
/,
|
|
||||||
) -> int: ...
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import _typeshed
|
|
||||||
from typing import Any, NewType, TypeVar
|
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
|
||||||
|
|
||||||
_CacheToken = NewType("_CacheToken", int)
|
|
||||||
|
|
||||||
def get_cache_token() -> _CacheToken: ...
|
|
||||||
|
|
||||||
class ABCMeta(type):
|
|
||||||
def __new__(
|
|
||||||
mcls: type[_typeshed.Self], name: str, bases: tuple[type[Any], ...], namespace: dict[str, Any], /
|
|
||||||
) -> _typeshed.Self: ...
|
|
||||||
def register(cls, subclass: type[_T]) -> type[_T]: ...
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
# This is a slight lie, the implementations aren't exactly identical
|
|
||||||
# However, in all likelihood, the differences are inconsequential
|
|
||||||
from _decimal import *
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"Decimal",
|
|
||||||
"Context",
|
|
||||||
"DecimalTuple",
|
|
||||||
"DefaultContext",
|
|
||||||
"BasicContext",
|
|
||||||
"ExtendedContext",
|
|
||||||
"DecimalException",
|
|
||||||
"Clamped",
|
|
||||||
"InvalidOperation",
|
|
||||||
"DivisionByZero",
|
|
||||||
"Inexact",
|
|
||||||
"Rounded",
|
|
||||||
"Subnormal",
|
|
||||||
"Overflow",
|
|
||||||
"Underflow",
|
|
||||||
"FloatOperation",
|
|
||||||
"DivisionImpossible",
|
|
||||||
"InvalidContext",
|
|
||||||
"ConversionSyntax",
|
|
||||||
"DivisionUndefined",
|
|
||||||
"ROUND_DOWN",
|
|
||||||
"ROUND_HALF_UP",
|
|
||||||
"ROUND_HALF_EVEN",
|
|
||||||
"ROUND_CEILING",
|
|
||||||
"ROUND_FLOOR",
|
|
||||||
"ROUND_UP",
|
|
||||||
"ROUND_HALF_DOWN",
|
|
||||||
"ROUND_05UP",
|
|
||||||
"setcontext",
|
|
||||||
"getcontext",
|
|
||||||
"localcontext",
|
|
||||||
"MAX_PREC",
|
|
||||||
"MAX_EMAX",
|
|
||||||
"MIN_EMIN",
|
|
||||||
"MIN_ETINY",
|
|
||||||
"HAVE_THREADS",
|
|
||||||
"HAVE_CONTEXTVAR",
|
|
||||||
]
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
# Actually Tuple[(int,) * 625]
|
|
||||||
_State: TypeAlias = tuple[int, ...]
|
|
||||||
|
|
||||||
class Random:
|
|
||||||
def __init__(self, seed: object = ...) -> None: ...
|
|
||||||
def seed(self, n: object = None, /) -> None: ...
|
|
||||||
def getstate(self) -> _State: ...
|
|
||||||
def setstate(self, state: _State, /) -> None: ...
|
|
||||||
def random(self) -> float: ...
|
|
||||||
def getrandbits(self, k: int, /) -> int: ...
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
from collections.abc import Iterable
|
|
||||||
from typing import ClassVar, Literal, NoReturn
|
|
||||||
|
|
||||||
class Quitter:
|
|
||||||
name: str
|
|
||||||
eof: str
|
|
||||||
def __init__(self, name: str, eof: str) -> None: ...
|
|
||||||
def __call__(self, code: int | None = None) -> NoReturn: ...
|
|
||||||
|
|
||||||
class _Printer:
|
|
||||||
MAXLINES: ClassVar[Literal[23]]
|
|
||||||
def __init__(self, name: str, data: str, files: Iterable[str] = (), dirs: Iterable[str] = ()) -> None: ...
|
|
||||||
def __call__(self) -> None: ...
|
|
||||||
|
|
||||||
class _Helper:
|
|
||||||
def __call__(self, request: object) -> None: ...
|
|
||||||
@@ -1,803 +0,0 @@
|
|||||||
import sys
|
|
||||||
from _typeshed import ReadableBuffer, WriteableBuffer
|
|
||||||
from collections.abc import Iterable
|
|
||||||
from typing import Any, SupportsIndex, overload
|
|
||||||
from typing_extensions import TypeAlias
|
|
||||||
|
|
||||||
_CMSG: TypeAlias = tuple[int, int, bytes]
|
|
||||||
_CMSGArg: TypeAlias = tuple[int, int, ReadableBuffer]
|
|
||||||
|
|
||||||
# Addresses can be either tuples of varying lengths (AF_INET, AF_INET6,
|
|
||||||
# AF_NETLINK, AF_TIPC) or strings/buffers (AF_UNIX).
|
|
||||||
# See getsockaddrarg() in socketmodule.c.
|
|
||||||
_Address: TypeAlias = tuple[Any, ...] | str | ReadableBuffer
|
|
||||||
_RetAddress: TypeAlias = Any
|
|
||||||
|
|
||||||
# ===== Constants =====
|
|
||||||
# This matches the order in the CPython documentation
|
|
||||||
# https://docs.python.org/3/library/socket.html#constants
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
AF_UNIX: int
|
|
||||||
|
|
||||||
AF_INET: int
|
|
||||||
AF_INET6: int
|
|
||||||
|
|
||||||
AF_UNSPEC: int
|
|
||||||
|
|
||||||
SOCK_STREAM: int
|
|
||||||
SOCK_DGRAM: int
|
|
||||||
SOCK_RAW: int
|
|
||||||
SOCK_RDM: int
|
|
||||||
SOCK_SEQPACKET: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.27
|
|
||||||
SOCK_CLOEXEC: int
|
|
||||||
SOCK_NONBLOCK: int
|
|
||||||
|
|
||||||
# --------------------
|
|
||||||
# Many constants of these forms, documented in the Unix documentation on
|
|
||||||
# sockets and/or the IP protocol, are also defined in the socket module.
|
|
||||||
# SO_*
|
|
||||||
# socket.SOMAXCONN
|
|
||||||
# MSG_*
|
|
||||||
# SOL_*
|
|
||||||
# SCM_*
|
|
||||||
# IPPROTO_*
|
|
||||||
# IPPORT_*
|
|
||||||
# INADDR_*
|
|
||||||
# IP_*
|
|
||||||
# IPV6_*
|
|
||||||
# EAI_*
|
|
||||||
# AI_*
|
|
||||||
# NI_*
|
|
||||||
# TCP_*
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
SO_ACCEPTCONN: int
|
|
||||||
SO_BROADCAST: int
|
|
||||||
SO_DEBUG: int
|
|
||||||
SO_DONTROUTE: int
|
|
||||||
SO_ERROR: int
|
|
||||||
SO_KEEPALIVE: int
|
|
||||||
SO_LINGER: int
|
|
||||||
SO_OOBINLINE: int
|
|
||||||
SO_RCVBUF: int
|
|
||||||
SO_RCVLOWAT: int
|
|
||||||
SO_RCVTIMEO: int
|
|
||||||
SO_REUSEADDR: int
|
|
||||||
SO_SNDBUF: int
|
|
||||||
SO_SNDLOWAT: int
|
|
||||||
SO_SNDTIMEO: int
|
|
||||||
SO_TYPE: int
|
|
||||||
SO_USELOOPBACK: int
|
|
||||||
if sys.platform == "win32":
|
|
||||||
SO_EXCLUSIVEADDRUSE: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
SO_REUSEPORT: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
SO_BINDTODEVICE: int
|
|
||||||
SO_DOMAIN: int
|
|
||||||
SO_MARK: int
|
|
||||||
SO_PASSCRED: int
|
|
||||||
SO_PASSSEC: int
|
|
||||||
SO_PEERCRED: int
|
|
||||||
SO_PEERSEC: int
|
|
||||||
SO_PRIORITY: int
|
|
||||||
SO_PROTOCOL: int
|
|
||||||
SO_SETFIB: int
|
|
||||||
|
|
||||||
SOMAXCONN: int
|
|
||||||
|
|
||||||
MSG_CTRUNC: int
|
|
||||||
MSG_DONTROUTE: int
|
|
||||||
MSG_OOB: int
|
|
||||||
MSG_PEEK: int
|
|
||||||
MSG_TRUNC: int
|
|
||||||
MSG_WAITALL: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
MSG_DONTWAIT: int
|
|
||||||
MSG_EOF: int
|
|
||||||
MSG_EOR: int
|
|
||||||
MSG_NOSIGNAL: int # Sometimes this exists on darwin, sometimes not
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
MSG_BCAST: int
|
|
||||||
MSG_ERRQUEUE: int
|
|
||||||
MSG_MCAST: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
MSG_BTAG: int
|
|
||||||
MSG_CMSG_CLOEXEC: int
|
|
||||||
MSG_CONFIRM: int
|
|
||||||
MSG_ETAG: int
|
|
||||||
MSG_FASTOPEN: int
|
|
||||||
MSG_MORE: int
|
|
||||||
MSG_NOTIFICATION: int
|
|
||||||
|
|
||||||
SOL_IP: int
|
|
||||||
SOL_SOCKET: int
|
|
||||||
SOL_TCP: int
|
|
||||||
SOL_UDP: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
SOL_ATALK: int
|
|
||||||
SOL_AX25: int
|
|
||||||
SOL_HCI: int
|
|
||||||
SOL_IPX: int
|
|
||||||
SOL_NETROM: int
|
|
||||||
SOL_ROSE: int
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
SCM_CREDS: int
|
|
||||||
SCM_RIGHTS: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
SCM_CREDENTIALS: int
|
|
||||||
|
|
||||||
IPPROTO_ICMP: int
|
|
||||||
IPPROTO_IP: int
|
|
||||||
IPPROTO_RAW: int
|
|
||||||
IPPROTO_TCP: int
|
|
||||||
IPPROTO_UDP: int
|
|
||||||
IPPROTO_AH: int
|
|
||||||
IPPROTO_DSTOPTS: int
|
|
||||||
IPPROTO_EGP: int
|
|
||||||
IPPROTO_ESP: int
|
|
||||||
IPPROTO_FRAGMENT: int
|
|
||||||
IPPROTO_GGP: int
|
|
||||||
IPPROTO_HOPOPTS: int
|
|
||||||
IPPROTO_ICMPV6: int
|
|
||||||
IPPROTO_IDP: int
|
|
||||||
IPPROTO_IGMP: int
|
|
||||||
IPPROTO_IPV4: int
|
|
||||||
IPPROTO_IPV6: int
|
|
||||||
IPPROTO_MAX: int
|
|
||||||
IPPROTO_ND: int
|
|
||||||
IPPROTO_NONE: int
|
|
||||||
IPPROTO_PIM: int
|
|
||||||
IPPROTO_PUP: int
|
|
||||||
IPPROTO_ROUTING: int
|
|
||||||
IPPROTO_SCTP: int
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
IPPROTO_CBT: int
|
|
||||||
IPPROTO_ICLFXBM: int
|
|
||||||
IPPROTO_IGP: int
|
|
||||||
IPPROTO_L2TP: int
|
|
||||||
IPPROTO_PGM: int
|
|
||||||
IPPROTO_RDP: int
|
|
||||||
IPPROTO_ST: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
IPPROTO_EON: int
|
|
||||||
IPPROTO_GRE: int
|
|
||||||
IPPROTO_HELLO: int
|
|
||||||
IPPROTO_IPCOMP: int
|
|
||||||
IPPROTO_IPIP: int
|
|
||||||
IPPROTO_RSVP: int
|
|
||||||
IPPROTO_TP: int
|
|
||||||
IPPROTO_XTP: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
IPPROTO_BIP: int
|
|
||||||
IPPROTO_MOBILE: int
|
|
||||||
IPPROTO_VRRP: int
|
|
||||||
if sys.version_info >= (3, 9) and sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.20, FreeBSD >= 10.1
|
|
||||||
IPPROTO_UDPLITE: int
|
|
||||||
if sys.version_info >= (3, 10) and sys.platform == "linux":
|
|
||||||
IPPROTO_MPTCP: int
|
|
||||||
|
|
||||||
IPPORT_RESERVED: int
|
|
||||||
IPPORT_USERRESERVED: int
|
|
||||||
|
|
||||||
INADDR_ALLHOSTS_GROUP: int
|
|
||||||
INADDR_ANY: int
|
|
||||||
INADDR_BROADCAST: int
|
|
||||||
INADDR_LOOPBACK: int
|
|
||||||
INADDR_MAX_LOCAL_GROUP: int
|
|
||||||
INADDR_NONE: int
|
|
||||||
INADDR_UNSPEC_GROUP: int
|
|
||||||
|
|
||||||
IP_ADD_MEMBERSHIP: int
|
|
||||||
IP_DROP_MEMBERSHIP: int
|
|
||||||
IP_HDRINCL: int
|
|
||||||
IP_MULTICAST_IF: int
|
|
||||||
IP_MULTICAST_LOOP: int
|
|
||||||
IP_MULTICAST_TTL: int
|
|
||||||
IP_OPTIONS: int
|
|
||||||
IP_RECVDSTADDR: int
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
IP_RECVTOS: int
|
|
||||||
elif sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
IP_RECVTOS: int
|
|
||||||
IP_TOS: int
|
|
||||||
IP_TTL: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
IP_DEFAULT_MULTICAST_LOOP: int
|
|
||||||
IP_DEFAULT_MULTICAST_TTL: int
|
|
||||||
IP_MAX_MEMBERSHIPS: int
|
|
||||||
IP_RECVOPTS: int
|
|
||||||
IP_RECVRETOPTS: int
|
|
||||||
IP_RETOPTS: int
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
IP_TRANSPARENT: int
|
|
||||||
IP_BIND_ADDRESS_NO_PORT: int
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
IP_ADD_SOURCE_MEMBERSHIP: int
|
|
||||||
IP_BLOCK_SOURCE: int
|
|
||||||
IP_DROP_SOURCE_MEMBERSHIP: int
|
|
||||||
IP_PKTINFO: int
|
|
||||||
IP_UNBLOCK_SOURCE: int
|
|
||||||
|
|
||||||
IPV6_CHECKSUM: int
|
|
||||||
IPV6_JOIN_GROUP: int
|
|
||||||
IPV6_LEAVE_GROUP: int
|
|
||||||
IPV6_MULTICAST_HOPS: int
|
|
||||||
IPV6_MULTICAST_IF: int
|
|
||||||
IPV6_MULTICAST_LOOP: int
|
|
||||||
IPV6_RECVTCLASS: int
|
|
||||||
IPV6_TCLASS: int
|
|
||||||
IPV6_UNICAST_HOPS: int
|
|
||||||
IPV6_V6ONLY: int
|
|
||||||
if sys.version_info >= (3, 9) or sys.platform != "darwin":
|
|
||||||
IPV6_DONTFRAG: int
|
|
||||||
IPV6_HOPLIMIT: int
|
|
||||||
IPV6_HOPOPTS: int
|
|
||||||
IPV6_PKTINFO: int
|
|
||||||
IPV6_RECVRTHDR: int
|
|
||||||
IPV6_RTHDR: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
IPV6_RTHDR_TYPE_0: int
|
|
||||||
if sys.version_info >= (3, 9) or sys.platform != "darwin":
|
|
||||||
IPV6_DSTOPTS: int
|
|
||||||
IPV6_NEXTHOP: int
|
|
||||||
IPV6_PATHMTU: int
|
|
||||||
IPV6_RECVDSTOPTS: int
|
|
||||||
IPV6_RECVHOPLIMIT: int
|
|
||||||
IPV6_RECVHOPOPTS: int
|
|
||||||
IPV6_RECVPATHMTU: int
|
|
||||||
IPV6_RECVPKTINFO: int
|
|
||||||
IPV6_RTHDRDSTOPTS: int
|
|
||||||
IPV6_USE_MIN_MTU: int
|
|
||||||
|
|
||||||
EAI_AGAIN: int
|
|
||||||
EAI_BADFLAGS: int
|
|
||||||
EAI_FAIL: int
|
|
||||||
EAI_FAMILY: int
|
|
||||||
EAI_MEMORY: int
|
|
||||||
EAI_NODATA: int
|
|
||||||
EAI_NONAME: int
|
|
||||||
EAI_SERVICE: int
|
|
||||||
EAI_SOCKTYPE: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
EAI_ADDRFAMILY: int
|
|
||||||
EAI_BADHINTS: int
|
|
||||||
EAI_MAX: int
|
|
||||||
EAI_OVERFLOW: int
|
|
||||||
EAI_PROTOCOL: int
|
|
||||||
EAI_SYSTEM: int
|
|
||||||
|
|
||||||
AI_ADDRCONFIG: int
|
|
||||||
AI_ALL: int
|
|
||||||
AI_CANONNAME: int
|
|
||||||
AI_NUMERICHOST: int
|
|
||||||
AI_NUMERICSERV: int
|
|
||||||
AI_PASSIVE: int
|
|
||||||
AI_V4MAPPED: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
AI_DEFAULT: int
|
|
||||||
AI_MASK: int
|
|
||||||
AI_V4MAPPED_CFG: int
|
|
||||||
|
|
||||||
NI_DGRAM: int
|
|
||||||
NI_MAXHOST: int
|
|
||||||
NI_MAXSERV: int
|
|
||||||
NI_NAMEREQD: int
|
|
||||||
NI_NOFQDN: int
|
|
||||||
NI_NUMERICHOST: int
|
|
||||||
NI_NUMERICSERV: int
|
|
||||||
|
|
||||||
TCP_FASTOPEN: int
|
|
||||||
TCP_KEEPCNT: int
|
|
||||||
TCP_KEEPINTVL: int
|
|
||||||
TCP_MAXSEG: int
|
|
||||||
TCP_NODELAY: int
|
|
||||||
if sys.platform != "win32":
|
|
||||||
TCP_NOTSENT_LOWAT: int
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
TCP_KEEPIDLE: int
|
|
||||||
if sys.version_info >= (3, 10) and sys.platform == "darwin":
|
|
||||||
TCP_KEEPALIVE: int
|
|
||||||
if sys.version_info >= (3, 11) and sys.platform == "darwin":
|
|
||||||
TCP_CONNECTION_INFO: int
|
|
||||||
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
TCP_CONGESTION: int
|
|
||||||
TCP_CORK: int
|
|
||||||
TCP_DEFER_ACCEPT: int
|
|
||||||
TCP_INFO: int
|
|
||||||
TCP_LINGER2: int
|
|
||||||
TCP_QUICKACK: int
|
|
||||||
TCP_SYNCNT: int
|
|
||||||
TCP_USER_TIMEOUT: int
|
|
||||||
TCP_WINDOW_CLAMP: int
|
|
||||||
|
|
||||||
# --------------------
|
|
||||||
# Specifically documented constants
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.25, NetBSD >= 8
|
|
||||||
AF_CAN: int
|
|
||||||
PF_CAN: int
|
|
||||||
SOL_CAN_BASE: int
|
|
||||||
SOL_CAN_RAW: int
|
|
||||||
CAN_EFF_FLAG: int
|
|
||||||
CAN_EFF_MASK: int
|
|
||||||
CAN_ERR_FLAG: int
|
|
||||||
CAN_ERR_MASK: int
|
|
||||||
CAN_RAW: int
|
|
||||||
CAN_RAW_ERR_FILTER: int
|
|
||||||
CAN_RAW_FILTER: int
|
|
||||||
CAN_RAW_LOOPBACK: int
|
|
||||||
CAN_RAW_RECV_OWN_MSGS: int
|
|
||||||
CAN_RTR_FLAG: int
|
|
||||||
CAN_SFF_MASK: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.25
|
|
||||||
CAN_BCM: int
|
|
||||||
CAN_BCM_TX_SETUP: int
|
|
||||||
CAN_BCM_TX_DELETE: int
|
|
||||||
CAN_BCM_TX_READ: int
|
|
||||||
CAN_BCM_TX_SEND: int
|
|
||||||
CAN_BCM_RX_SETUP: int
|
|
||||||
CAN_BCM_RX_DELETE: int
|
|
||||||
CAN_BCM_RX_READ: int
|
|
||||||
CAN_BCM_TX_STATUS: int
|
|
||||||
CAN_BCM_TX_EXPIRED: int
|
|
||||||
CAN_BCM_RX_STATUS: int
|
|
||||||
CAN_BCM_RX_TIMEOUT: int
|
|
||||||
CAN_BCM_RX_CHANGED: int
|
|
||||||
CAN_BCM_SETTIMER: int
|
|
||||||
CAN_BCM_STARTTIMER: int
|
|
||||||
CAN_BCM_TX_COUNTEVT: int
|
|
||||||
CAN_BCM_TX_ANNOUNCE: int
|
|
||||||
CAN_BCM_TX_CP_CAN_ID: int
|
|
||||||
CAN_BCM_RX_FILTER_ID: int
|
|
||||||
CAN_BCM_RX_CHECK_DLC: int
|
|
||||||
CAN_BCM_RX_NO_AUTOTIMER: int
|
|
||||||
CAN_BCM_RX_ANNOUNCE_RESUME: int
|
|
||||||
CAN_BCM_TX_RESET_MULTI_IDX: int
|
|
||||||
CAN_BCM_RX_RTR_FRAME: int
|
|
||||||
CAN_BCM_CAN_FD_FRAME: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 3.6
|
|
||||||
CAN_RAW_FD_FRAMES: int
|
|
||||||
|
|
||||||
if sys.platform == "linux" and sys.version_info >= (3, 9):
|
|
||||||
# Availability: Linux >= 4.1
|
|
||||||
CAN_RAW_JOIN_FILTERS: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.25
|
|
||||||
CAN_ISOTP: int
|
|
||||||
|
|
||||||
if sys.platform == "linux" and sys.version_info >= (3, 9):
|
|
||||||
# Availability: Linux >= 5.4
|
|
||||||
CAN_J1939: int
|
|
||||||
|
|
||||||
J1939_MAX_UNICAST_ADDR: int
|
|
||||||
J1939_IDLE_ADDR: int
|
|
||||||
J1939_NO_ADDR: int
|
|
||||||
J1939_NO_NAME: int
|
|
||||||
J1939_PGN_REQUEST: int
|
|
||||||
J1939_PGN_ADDRESS_CLAIMED: int
|
|
||||||
J1939_PGN_ADDRESS_COMMANDED: int
|
|
||||||
J1939_PGN_PDU1_MAX: int
|
|
||||||
J1939_PGN_MAX: int
|
|
||||||
J1939_NO_PGN: int
|
|
||||||
|
|
||||||
SO_J1939_FILTER: int
|
|
||||||
SO_J1939_PROMISC: int
|
|
||||||
SO_J1939_SEND_PRIO: int
|
|
||||||
SO_J1939_ERRQUEUE: int
|
|
||||||
|
|
||||||
SCM_J1939_DEST_ADDR: int
|
|
||||||
SCM_J1939_DEST_NAME: int
|
|
||||||
SCM_J1939_PRIO: int
|
|
||||||
SCM_J1939_ERRQUEUE: int
|
|
||||||
|
|
||||||
J1939_NLA_PAD: int
|
|
||||||
J1939_NLA_BYTES_ACKED: int
|
|
||||||
J1939_EE_INFO_NONE: int
|
|
||||||
J1939_EE_INFO_TX_ABORT: int
|
|
||||||
J1939_FILTER_MAX: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12) and sys.platform != "linux" and sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
# Availability: FreeBSD >= 14.0
|
|
||||||
AF_DIVERT: int
|
|
||||||
PF_DIVERT: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.2
|
|
||||||
AF_PACKET: int
|
|
||||||
PF_PACKET: int
|
|
||||||
PACKET_BROADCAST: int
|
|
||||||
PACKET_FASTROUTE: int
|
|
||||||
PACKET_HOST: int
|
|
||||||
PACKET_LOOPBACK: int
|
|
||||||
PACKET_MULTICAST: int
|
|
||||||
PACKET_OTHERHOST: int
|
|
||||||
PACKET_OUTGOING: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12) and sys.platform == "linux":
|
|
||||||
ETH_P_ALL: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.30
|
|
||||||
AF_RDS: int
|
|
||||||
PF_RDS: int
|
|
||||||
SOL_RDS: int
|
|
||||||
RDS_CANCEL_SENT_TO: int
|
|
||||||
RDS_CMSG_RDMA_ARGS: int
|
|
||||||
RDS_CMSG_RDMA_DEST: int
|
|
||||||
RDS_CMSG_RDMA_MAP: int
|
|
||||||
RDS_CMSG_RDMA_STATUS: int
|
|
||||||
RDS_CMSG_RDMA_UPDATE: int
|
|
||||||
RDS_CONG_MONITOR: int
|
|
||||||
RDS_FREE_MR: int
|
|
||||||
RDS_GET_MR: int
|
|
||||||
RDS_GET_MR_FOR_DEST: int
|
|
||||||
RDS_RDMA_DONTWAIT: int
|
|
||||||
RDS_RDMA_FENCE: int
|
|
||||||
RDS_RDMA_INVALIDATE: int
|
|
||||||
RDS_RDMA_NOTIFY_ME: int
|
|
||||||
RDS_RDMA_READWRITE: int
|
|
||||||
RDS_RDMA_SILENT: int
|
|
||||||
RDS_RDMA_USE_ONCE: int
|
|
||||||
RDS_RECVERR: int
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
|
||||||
SIO_RCVALL: int
|
|
||||||
SIO_KEEPALIVE_VALS: int
|
|
||||||
SIO_LOOPBACK_FAST_PATH: int
|
|
||||||
RCVALL_MAX: int
|
|
||||||
RCVALL_OFF: int
|
|
||||||
RCVALL_ON: int
|
|
||||||
RCVALL_SOCKETLEVELONLY: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
AF_TIPC: int
|
|
||||||
SOL_TIPC: int
|
|
||||||
TIPC_ADDR_ID: int
|
|
||||||
TIPC_ADDR_NAME: int
|
|
||||||
TIPC_ADDR_NAMESEQ: int
|
|
||||||
TIPC_CFG_SRV: int
|
|
||||||
TIPC_CLUSTER_SCOPE: int
|
|
||||||
TIPC_CONN_TIMEOUT: int
|
|
||||||
TIPC_CRITICAL_IMPORTANCE: int
|
|
||||||
TIPC_DEST_DROPPABLE: int
|
|
||||||
TIPC_HIGH_IMPORTANCE: int
|
|
||||||
TIPC_IMPORTANCE: int
|
|
||||||
TIPC_LOW_IMPORTANCE: int
|
|
||||||
TIPC_MEDIUM_IMPORTANCE: int
|
|
||||||
TIPC_NODE_SCOPE: int
|
|
||||||
TIPC_PUBLISHED: int
|
|
||||||
TIPC_SRC_DROPPABLE: int
|
|
||||||
TIPC_SUBSCR_TIMEOUT: int
|
|
||||||
TIPC_SUB_CANCEL: int
|
|
||||||
TIPC_SUB_PORTS: int
|
|
||||||
TIPC_SUB_SERVICE: int
|
|
||||||
TIPC_TOP_SRV: int
|
|
||||||
TIPC_WAIT_FOREVER: int
|
|
||||||
TIPC_WITHDRAWN: int
|
|
||||||
TIPC_ZONE_SCOPE: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 2.6.38
|
|
||||||
AF_ALG: int
|
|
||||||
SOL_ALG: int
|
|
||||||
ALG_OP_DECRYPT: int
|
|
||||||
ALG_OP_ENCRYPT: int
|
|
||||||
ALG_OP_SIGN: int
|
|
||||||
ALG_OP_VERIFY: int
|
|
||||||
ALG_SET_AEAD_ASSOCLEN: int
|
|
||||||
ALG_SET_AEAD_AUTHSIZE: int
|
|
||||||
ALG_SET_IV: int
|
|
||||||
ALG_SET_KEY: int
|
|
||||||
ALG_SET_OP: int
|
|
||||||
ALG_SET_PUBKEY: int
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Availability: Linux >= 4.8 (or maybe 3.9, CPython docs are confusing)
|
|
||||||
AF_VSOCK: int
|
|
||||||
IOCTL_VM_SOCKETS_GET_LOCAL_CID: int
|
|
||||||
VMADDR_CID_ANY: int
|
|
||||||
VMADDR_CID_HOST: int
|
|
||||||
VMADDR_PORT_ANY: int
|
|
||||||
SO_VM_SOCKETS_BUFFER_MAX_SIZE: int
|
|
||||||
SO_VM_SOCKETS_BUFFER_SIZE: int
|
|
||||||
SO_VM_SOCKETS_BUFFER_MIN_SIZE: int
|
|
||||||
VM_SOCKETS_INVALID_VERSION: int # undocumented
|
|
||||||
|
|
||||||
if sys.platform != "win32" or sys.version_info >= (3, 9):
|
|
||||||
# Documented as only available on BSD, macOS, but empirically sometimes
|
|
||||||
# available on Windows
|
|
||||||
AF_LINK: int
|
|
||||||
|
|
||||||
has_ipv6: bool
|
|
||||||
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
if sys.platform != "win32" or sys.version_info >= (3, 9):
|
|
||||||
BDADDR_ANY: str
|
|
||||||
BDADDR_LOCAL: str
|
|
||||||
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
HCI_FILTER: int # not in NetBSD or DragonFlyBSD
|
|
||||||
HCI_TIME_STAMP: int # not in FreeBSD, NetBSD, or DragonFlyBSD
|
|
||||||
HCI_DATA_DIR: int # not in FreeBSD, NetBSD, or DragonFlyBSD
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
AF_QIPCRTR: int # Availability: Linux >= 4.7
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11) and sys.platform != "linux" and sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
# FreeBSD
|
|
||||||
SCM_CREDS2: int
|
|
||||||
LOCAL_CREDS: int
|
|
||||||
LOCAL_CREDS_PERSISTENT: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 11) and sys.platform == "linux":
|
|
||||||
SO_INCOMING_CPU: int # Availability: Linux >= 3.9
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12) and sys.platform == "win32":
|
|
||||||
# Availability: Windows
|
|
||||||
AF_HYPERV: int
|
|
||||||
HV_PROTOCOL_RAW: int
|
|
||||||
HVSOCKET_CONNECT_TIMEOUT: int
|
|
||||||
HVSOCKET_CONNECT_TIMEOUT_MAX: int
|
|
||||||
HVSOCKET_CONNECTED_SUSPEND: int
|
|
||||||
HVSOCKET_ADDRESS_FLAG_PASSTHRU: int
|
|
||||||
HV_GUID_ZERO: str
|
|
||||||
HV_GUID_WILDCARD: str
|
|
||||||
HV_GUID_BROADCAST: str
|
|
||||||
HV_GUID_CHILDREN: str
|
|
||||||
HV_GUID_LOOPBACK: str
|
|
||||||
HV_GUID_PARENT: str
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 12):
|
|
||||||
if sys.platform != "win32":
|
|
||||||
# Availability: Linux, FreeBSD, macOS
|
|
||||||
ETHERTYPE_ARP: int
|
|
||||||
ETHERTYPE_IP: int
|
|
||||||
ETHERTYPE_IPV6: int
|
|
||||||
ETHERTYPE_VLAN: int
|
|
||||||
|
|
||||||
# --------------------
|
|
||||||
# Semi-documented constants
|
|
||||||
# These are alluded to under the "Socket families" section in the docs
|
|
||||||
# https://docs.python.org/3/library/socket.html#socket-families
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
if sys.platform == "linux":
|
|
||||||
# Netlink is defined by Linux
|
|
||||||
AF_NETLINK: int
|
|
||||||
NETLINK_ARPD: int
|
|
||||||
NETLINK_CRYPTO: int
|
|
||||||
NETLINK_DNRTMSG: int
|
|
||||||
NETLINK_FIREWALL: int
|
|
||||||
NETLINK_IP6_FW: int
|
|
||||||
NETLINK_NFLOG: int
|
|
||||||
NETLINK_ROUTE6: int
|
|
||||||
NETLINK_ROUTE: int
|
|
||||||
NETLINK_SKIP: int
|
|
||||||
NETLINK_TAPBASE: int
|
|
||||||
NETLINK_TCPDIAG: int
|
|
||||||
NETLINK_USERSOCK: int
|
|
||||||
NETLINK_W1: int
|
|
||||||
NETLINK_XFRM: int
|
|
||||||
|
|
||||||
if sys.platform == "darwin":
|
|
||||||
PF_SYSTEM: int
|
|
||||||
SYSPROTO_CONTROL: int
|
|
||||||
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
if sys.version_info >= (3, 9) or sys.platform != "win32":
|
|
||||||
AF_BLUETOOTH: int
|
|
||||||
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
# Linux and some BSD support is explicit in the docs
|
|
||||||
# Windows and macOS do not support in practice
|
|
||||||
BTPROTO_HCI: int
|
|
||||||
BTPROTO_L2CAP: int
|
|
||||||
BTPROTO_SCO: int # not in FreeBSD
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
if sys.version_info >= (3, 9) or sys.platform != "win32":
|
|
||||||
BTPROTO_RFCOMM: int
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 9) and sys.platform == "linux":
|
|
||||||
UDPLITE_RECV_CSCOV: int
|
|
||||||
UDPLITE_SEND_CSCOV: int
|
|
||||||
|
|
||||||
# --------------------
|
|
||||||
# Documented under socket.shutdown
|
|
||||||
# --------------------
|
|
||||||
SHUT_RD: int
|
|
||||||
SHUT_RDWR: int
|
|
||||||
SHUT_WR: int
|
|
||||||
|
|
||||||
# --------------------
|
|
||||||
# Undocumented constants
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
# Undocumented address families
|
|
||||||
AF_APPLETALK: int
|
|
||||||
AF_DECnet: int
|
|
||||||
AF_IPX: int
|
|
||||||
AF_SNA: int
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
AF_ROUTE: int
|
|
||||||
AF_SYSTEM: int
|
|
||||||
|
|
||||||
if sys.platform != "darwin":
|
|
||||||
AF_IRDA: int
|
|
||||||
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
AF_AAL5: int
|
|
||||||
AF_ASH: int
|
|
||||||
AF_ATMPVC: int
|
|
||||||
AF_ATMSVC: int
|
|
||||||
AF_AX25: int
|
|
||||||
AF_BRIDGE: int
|
|
||||||
AF_ECONET: int
|
|
||||||
AF_KEY: int
|
|
||||||
AF_LLC: int
|
|
||||||
AF_NETBEUI: int
|
|
||||||
AF_NETROM: int
|
|
||||||
AF_PPPOX: int
|
|
||||||
AF_ROSE: int
|
|
||||||
AF_SECURITY: int
|
|
||||||
AF_WANPIPE: int
|
|
||||||
AF_X25: int
|
|
||||||
|
|
||||||
# Miscellaneous undocumented
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
LOCAL_PEERCRED: int
|
|
||||||
|
|
||||||
if sys.platform != "win32" and sys.platform != "darwin":
|
|
||||||
IPX_TYPE: int
|
|
||||||
|
|
||||||
# ===== Exceptions =====
|
|
||||||
|
|
||||||
error = OSError
|
|
||||||
|
|
||||||
class herror(error): ...
|
|
||||||
class gaierror(error): ...
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
timeout = TimeoutError
|
|
||||||
else:
|
|
||||||
class timeout(error): ...
|
|
||||||
|
|
||||||
# ===== Classes =====
|
|
||||||
|
|
||||||
class socket:
|
|
||||||
@property
|
|
||||||
def family(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def type(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def proto(self) -> int: ...
|
|
||||||
@property
|
|
||||||
def timeout(self) -> float | None: ...
|
|
||||||
if sys.platform == "win32":
|
|
||||||
def __init__(
|
|
||||||
self, family: int = ..., type: int = ..., proto: int = ..., fileno: SupportsIndex | bytes | None = ...
|
|
||||||
) -> None: ...
|
|
||||||
else:
|
|
||||||
def __init__(self, family: int = ..., type: int = ..., proto: int = ..., fileno: SupportsIndex | None = ...) -> None: ...
|
|
||||||
|
|
||||||
def bind(self, address: _Address, /) -> None: ...
|
|
||||||
def close(self) -> None: ...
|
|
||||||
def connect(self, address: _Address, /) -> None: ...
|
|
||||||
def connect_ex(self, address: _Address, /) -> int: ...
|
|
||||||
def detach(self) -> int: ...
|
|
||||||
def fileno(self) -> int: ...
|
|
||||||
def getpeername(self) -> _RetAddress: ...
|
|
||||||
def getsockname(self) -> _RetAddress: ...
|
|
||||||
@overload
|
|
||||||
def getsockopt(self, level: int, optname: int, /) -> int: ...
|
|
||||||
@overload
|
|
||||||
def getsockopt(self, level: int, optname: int, buflen: int, /) -> bytes: ...
|
|
||||||
def getblocking(self) -> bool: ...
|
|
||||||
def gettimeout(self) -> float | None: ...
|
|
||||||
if sys.platform == "win32":
|
|
||||||
def ioctl(self, control: int, option: int | tuple[int, int, int] | bool, /) -> None: ...
|
|
||||||
|
|
||||||
def listen(self, backlog: int = ..., /) -> None: ...
|
|
||||||
def recv(self, bufsize: int, flags: int = ..., /) -> bytes: ...
|
|
||||||
def recvfrom(self, bufsize: int, flags: int = ..., /) -> tuple[bytes, _RetAddress]: ...
|
|
||||||
if sys.platform != "win32":
|
|
||||||
def recvmsg(self, bufsize: int, ancbufsize: int = ..., flags: int = ..., /) -> tuple[bytes, list[_CMSG], int, Any]: ...
|
|
||||||
def recvmsg_into(
|
|
||||||
self, buffers: Iterable[WriteableBuffer], ancbufsize: int = ..., flags: int = ..., /
|
|
||||||
) -> tuple[int, list[_CMSG], int, Any]: ...
|
|
||||||
|
|
||||||
def recvfrom_into(self, buffer: WriteableBuffer, nbytes: int = ..., flags: int = ...) -> tuple[int, _RetAddress]: ...
|
|
||||||
def recv_into(self, buffer: WriteableBuffer, nbytes: int = ..., flags: int = ...) -> int: ...
|
|
||||||
def send(self, data: ReadableBuffer, flags: int = ..., /) -> int: ...
|
|
||||||
def sendall(self, data: ReadableBuffer, flags: int = ..., /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def sendto(self, data: ReadableBuffer, address: _Address, /) -> int: ...
|
|
||||||
@overload
|
|
||||||
def sendto(self, data: ReadableBuffer, flags: int, address: _Address, /) -> int: ...
|
|
||||||
if sys.platform != "win32":
|
|
||||||
def sendmsg(
|
|
||||||
self,
|
|
||||||
buffers: Iterable[ReadableBuffer],
|
|
||||||
ancdata: Iterable[_CMSGArg] = ...,
|
|
||||||
flags: int = ...,
|
|
||||||
address: _Address | None = ...,
|
|
||||||
/,
|
|
||||||
) -> int: ...
|
|
||||||
if sys.platform == "linux":
|
|
||||||
def sendmsg_afalg(
|
|
||||||
self, msg: Iterable[ReadableBuffer] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
|
|
||||||
) -> int: ...
|
|
||||||
|
|
||||||
def setblocking(self, flag: bool, /) -> None: ...
|
|
||||||
def settimeout(self, value: float | None, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def setsockopt(self, level: int, optname: int, value: int | ReadableBuffer, /) -> None: ...
|
|
||||||
@overload
|
|
||||||
def setsockopt(self, level: int, optname: int, value: None, optlen: int, /) -> None: ...
|
|
||||||
if sys.platform == "win32":
|
|
||||||
def share(self, process_id: int, /) -> bytes: ...
|
|
||||||
|
|
||||||
def shutdown(self, how: int, /) -> None: ...
|
|
||||||
|
|
||||||
SocketType = socket
|
|
||||||
|
|
||||||
# ===== Functions =====
|
|
||||||
|
|
||||||
def close(fd: SupportsIndex, /) -> None: ...
|
|
||||||
def dup(fd: SupportsIndex, /) -> int: ...
|
|
||||||
|
|
||||||
# the 5th tuple item is an address
|
|
||||||
def getaddrinfo(
|
|
||||||
host: bytes | str | None,
|
|
||||||
port: bytes | str | int | None,
|
|
||||||
family: int = ...,
|
|
||||||
type: int = ...,
|
|
||||||
proto: int = ...,
|
|
||||||
flags: int = ...,
|
|
||||||
) -> list[tuple[int, int, int, str, tuple[str, int] | tuple[str, int, int, int]]]: ...
|
|
||||||
def gethostbyname(hostname: str, /) -> str: ...
|
|
||||||
def gethostbyname_ex(hostname: str, /) -> tuple[str, list[str], list[str]]: ...
|
|
||||||
def gethostname() -> str: ...
|
|
||||||
def gethostbyaddr(ip_address: str, /) -> tuple[str, list[str], list[str]]: ...
|
|
||||||
def getnameinfo(sockaddr: tuple[str, int] | tuple[str, int, int, int], flags: int, /) -> tuple[str, str]: ...
|
|
||||||
def getprotobyname(protocolname: str, /) -> int: ...
|
|
||||||
def getservbyname(servicename: str, protocolname: str = ..., /) -> int: ...
|
|
||||||
def getservbyport(port: int, protocolname: str = ..., /) -> str: ...
|
|
||||||
def ntohl(x: int, /) -> int: ... # param & ret val are 32-bit ints
|
|
||||||
def ntohs(x: int, /) -> int: ... # param & ret val are 16-bit ints
|
|
||||||
def htonl(x: int, /) -> int: ... # param & ret val are 32-bit ints
|
|
||||||
def htons(x: int, /) -> int: ... # param & ret val are 16-bit ints
|
|
||||||
def inet_aton(ip_addr: str, /) -> bytes: ... # ret val 4 bytes in length
|
|
||||||
def inet_ntoa(packed_ip: ReadableBuffer, /) -> str: ...
|
|
||||||
def inet_pton(address_family: int, ip_string: str, /) -> bytes: ...
|
|
||||||
def inet_ntop(address_family: int, packed_ip: ReadableBuffer, /) -> str: ...
|
|
||||||
def getdefaulttimeout() -> float | None: ...
|
|
||||||
def setdefaulttimeout(timeout: float | None, /) -> None: ...
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
|
||||||
def sethostname(name: str, /) -> None: ...
|
|
||||||
def CMSG_LEN(length: int, /) -> int: ...
|
|
||||||
def CMSG_SPACE(length: int, /) -> int: ...
|
|
||||||
def socketpair(family: int = ..., type: int = ..., proto: int = ..., /) -> tuple[socket, socket]: ...
|
|
||||||
|
|
||||||
def if_nameindex() -> list[tuple[int, str]]: ...
|
|
||||||
def if_nametoindex(oname: str, /) -> int: ...
|
|
||||||
def if_indextoname(index: int, /) -> str: ...
|
|
||||||
|
|
||||||
CAPI: object
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user