mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[syntax-errors] Store to or delete __debug__
(#16984)
Summary -- Detect setting or deleting `__debug__`. Assigning to `__debug__` was a `SyntaxError` on the earliest version I tested (3.8). Deleting `__debug__` was made a `SyntaxError` in [BPO 45000], which said it was resolved in Python 3.10. However, `del __debug__` was also a runtime error (`NameError`) when I tested in Python 3.9.6, so I thought it was worth including 3.9 in this check. I don't think it was ever a *good* idea to try `del __debug__`, so I think there's also an argument for not making this version-dependent at all. That would only simplify the implementation very slightly, though. [BPO 45000]: https://github.com/python/cpython/issues/89163 Test Plan -- New inline tests. This also required adding a `PythonVersion` field to the `TestContext` that could be taken from the inline `ParseOptions` and making the version field on the options accessible.
This commit is contained in:
parent
8396d7cd63
commit
a0819f0c51
28 changed files with 1372 additions and 7 deletions
|
@ -42,7 +42,7 @@ fn test_valid_syntax(input_path: &Path) {
|
|||
let options = extract_options(&source).unwrap_or_else(|| {
|
||||
ParseOptions::from(Mode::Module).with_target_version(PythonVersion::latest())
|
||||
});
|
||||
let parsed = parse_unchecked(&source, options);
|
||||
let parsed = parse_unchecked(&source, options.clone());
|
||||
|
||||
if parsed.has_syntax_errors() {
|
||||
let line_index = LineIndex::from_source_text(&source);
|
||||
|
@ -88,7 +88,9 @@ fn test_valid_syntax(input_path: &Path) {
|
|||
|
||||
let parsed = parsed.try_into_module().expect("Parsed with Mode::Module");
|
||||
|
||||
let mut visitor = SemanticSyntaxCheckerVisitor::new(TestContext::default());
|
||||
let mut visitor = SemanticSyntaxCheckerVisitor::new(
|
||||
TestContext::default().with_python_version(options.target_version()),
|
||||
);
|
||||
|
||||
for stmt in parsed.suite() {
|
||||
visitor.visit_stmt(stmt);
|
||||
|
@ -134,7 +136,7 @@ fn test_invalid_syntax(input_path: &Path) {
|
|||
let options = extract_options(&source).unwrap_or_else(|| {
|
||||
ParseOptions::from(Mode::Module).with_target_version(PythonVersion::latest())
|
||||
});
|
||||
let parsed = parse_unchecked(&source, options);
|
||||
let parsed = parse_unchecked(&source, options.clone());
|
||||
|
||||
validate_tokens(parsed.tokens(), source.text_len(), input_path);
|
||||
validate_ast(parsed.syntax(), source.text_len(), input_path);
|
||||
|
@ -182,7 +184,9 @@ fn test_invalid_syntax(input_path: &Path) {
|
|||
|
||||
let parsed = parsed.try_into_module().expect("Parsed with Mode::Module");
|
||||
|
||||
let mut visitor = SemanticSyntaxCheckerVisitor::new(TestContext::default());
|
||||
let mut visitor = SemanticSyntaxCheckerVisitor::new(
|
||||
TestContext::default().with_python_version(options.target_version()),
|
||||
);
|
||||
|
||||
for stmt in parsed.suite() {
|
||||
visitor.visit_stmt(stmt);
|
||||
|
@ -461,6 +465,15 @@ impl<'ast> SourceOrderVisitor<'ast> for ValidateAstVisitor<'ast> {
|
|||
#[derive(Debug, Default)]
|
||||
struct TestContext {
|
||||
diagnostics: RefCell<Vec<SemanticSyntaxError>>,
|
||||
python_version: PythonVersion,
|
||||
}
|
||||
|
||||
impl TestContext {
|
||||
#[must_use]
|
||||
fn with_python_version(mut self, python_version: PythonVersion) -> Self {
|
||||
self.python_version = python_version;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl SemanticSyntaxContext for TestContext {
|
||||
|
@ -469,7 +482,7 @@ impl SemanticSyntaxContext for TestContext {
|
|||
}
|
||||
|
||||
fn python_version(&self) -> PythonVersion {
|
||||
PythonVersion::default()
|
||||
self.python_version
|
||||
}
|
||||
|
||||
fn report_semantic_error(&self, error: SemanticSyntaxError) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue