Skip to content

Commit

Permalink
Add tests for invalid syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Sep 9, 2024
1 parent 7160338 commit e28d9d9
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4414,15 +4414,10 @@ mod tests {
}

#[test]
fn comprehension_with_not_iterable_iter() -> anyhow::Result<()> {
fn comprehension_with_unbound_iter() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"src/a.py",
"
[z for z in x]
",
)?;
db.write_dedented("src/a.py", "[z for z in x]")?;

assert_scope_ty(&db, "src/a.py", &["<listcomp>"], "x", "Unbound");

Expand Down Expand Up @@ -4518,6 +4513,57 @@ mod tests {
Ok(())
}

#[test]
fn comprehension_with_missing_in_keyword() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"src/a.py",
"
def foo():
[z for z IntIterable()]
class IntIterator:
def __next__(self) -> int:
return 42
class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()
",
)?;

// We'll emit a diagnostic separately for invalid syntax,
// but it's reasonably clear here what they *meant* to write,
// so we'll still infer the correct type:
assert_scope_ty(&db, "src/a.py", &["foo", "<listcomp>"], "z", "int");
Ok(())
}

#[test]
fn comprehension_with_missing_in_keyword_and_missing_iter() -> anyhow::Result<()> {
let mut db = setup_db();

db.write_dedented(
"src/a.py",
"
def foo():
[z for z]
class IntIterator:
def __next__(self) -> int:
return 42
class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()
",
)?;

assert_scope_ty(&db, "src/a.py", &["foo", "<listcomp>"], "z", "Unknown");
Ok(())
}

/// This tests that we understand that `async` comprehensions
/// do not work according to the synchronous iteration protocol
#[test]
Expand Down

0 comments on commit e28d9d9

Please sign in to comment.