-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Infer subscript expression types for bytes literals (#13901)
## Summary Infer subscript expression types for bytes literals: ```py b = b"\x00abc\xff" reveal_type(b[0]) # revealed: Literal[b"\x00"] reveal_type(b[1]) # revealed: Literal[b"a"] reveal_type(b[-1]) # revealed: Literal[b"\xff"] reveal_type(b[-2]) # revealed: Literal[b"c"] reveal_type(b[False]) # revealed: Literal[b"\x00"] reveal_type(b[True]) # revealed: Literal[b"a"] ``` part of #13689 (#13689 (comment)) ## Test Plan - New Markdown-based tests (see `mdtest/subscript/bytes.md`) - Added missing test for `string_literal[bool_literal]`
- Loading branch information
Showing
7 changed files
with
156 additions
and
68 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/red_knot_python_semantic/resources/mdtest/literal/bytes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Bytes literals | ||
|
||
## Simple | ||
|
||
```py | ||
reveal_type(b"red" b"knot") # revealed: Literal[b"redknot"] | ||
reveal_type(b"hello") # revealed: Literal[b"hello"] | ||
reveal_type(b"world" + b"!") # revealed: Literal[b"world!"] | ||
reveal_type(b"\xff\x00") # revealed: Literal[b"\xff\x00"] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod subscript; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
pub(crate) trait PythonSubscript { | ||
type Item; | ||
|
||
fn python_subscript(&mut self, index: i64) -> Option<Self::Item>; | ||
} | ||
|
||
impl<I, T: DoubleEndedIterator<Item = I>> PythonSubscript for T { | ||
type Item = I; | ||
|
||
fn python_subscript(&mut self, index: i64) -> Option<I> { | ||
if index >= 0 { | ||
self.nth(usize::try_from(index).ok()?) | ||
} else { | ||
let nth_rev = usize::try_from(index.checked_neg()?).ok()?.checked_sub(1)?; | ||
self.rev().nth(nth_rev) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::PythonSubscript; | ||
|
||
#[test] | ||
fn python_subscript_basic() { | ||
let iter = 'a'..='e'; | ||
|
||
assert_eq!(iter.clone().python_subscript(0), Some('a')); | ||
assert_eq!(iter.clone().python_subscript(1), Some('b')); | ||
assert_eq!(iter.clone().python_subscript(4), Some('e')); | ||
assert_eq!(iter.clone().python_subscript(5), None); | ||
|
||
assert_eq!(iter.clone().python_subscript(-1), Some('e')); | ||
assert_eq!(iter.clone().python_subscript(-2), Some('d')); | ||
assert_eq!(iter.clone().python_subscript(-5), Some('a')); | ||
assert_eq!(iter.clone().python_subscript(-6), None); | ||
} | ||
|
||
#[test] | ||
fn python_subscript_empty() { | ||
let iter = 'a'..'a'; | ||
|
||
assert_eq!(iter.clone().python_subscript(0), None); | ||
assert_eq!(iter.clone().python_subscript(1), None); | ||
assert_eq!(iter.clone().python_subscript(-1), None); | ||
} | ||
|
||
#[test] | ||
fn python_subscript_single_element() { | ||
let iter = 'a'..='a'; | ||
|
||
assert_eq!(iter.clone().python_subscript(0), Some('a')); | ||
assert_eq!(iter.clone().python_subscript(1), None); | ||
assert_eq!(iter.clone().python_subscript(-1), Some('a')); | ||
assert_eq!(iter.clone().python_subscript(-2), None); | ||
} | ||
|
||
#[test] | ||
fn python_subscript_uses_full_index_range() { | ||
let iter = 0..=u64::MAX; | ||
|
||
assert_eq!(iter.clone().python_subscript(0), Some(0)); | ||
assert_eq!(iter.clone().python_subscript(1), Some(1)); | ||
assert_eq!( | ||
iter.clone().python_subscript(i64::MAX), | ||
Some(i64::MAX as u64) | ||
); | ||
|
||
assert_eq!(iter.clone().python_subscript(-1), Some(u64::MAX)); | ||
assert_eq!(iter.clone().python_subscript(-2), Some(u64::MAX - 1)); | ||
|
||
// i64::MIN is not representable as a positive number, so it is not | ||
// a valid index: | ||
assert_eq!(iter.clone().python_subscript(i64::MIN), None); | ||
|
||
// but i64::MIN +1 is: | ||
assert_eq!( | ||
iter.clone().python_subscript(i64::MIN + 1), | ||
Some(2u64.pow(63) + 1) | ||
); | ||
} | ||
} |