-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[red-knot] Infer subscript expression types for bytes literals #13901
Changes from all commits
d0c406d
4455cc9
e529344
543d0ad
cc9356a
93db3dd
1c2bb9f
d29004e
24f778d
9f3b5c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ reveal_type(s[1]) # revealed: Literal["b"] | |
reveal_type(s[-1]) # revealed: Literal["e"] | ||
reveal_type(s[-2]) # revealed: Literal["d"] | ||
|
||
reveal_type(s[False]) # revealed: Literal["a"] | ||
reveal_type(s[True]) # revealed: Literal["b"] | ||
|
||
a = s[8] # error: [index-out-of-bounds] "Index 8 is out of bounds for string `Literal["abcde"]` with length 5" | ||
reveal_type(a) # revealed: Unknown | ||
|
||
|
@@ -20,11 +23,10 @@ reveal_type(b) # revealed: Unknown | |
## Function return | ||
|
||
```py | ||
def add(x: int, y: int) -> int: | ||
return x + y | ||
Comment on lines
-23
to
-24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am assuming |
||
def int_instance() -> int: ... | ||
|
||
|
||
a = "abcde"[add(0, 1)] | ||
a = "abcde"[int_instance()] | ||
# TODO: Support overloads... Should be `str` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that we want to infer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We attempt to infer the type here by looking up In fact, we just infer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
reveal_type(a) # revealed: @Todo | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod subscript; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I probably would have just had this module as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indexing/slicing to me feels like something that is not really related to types, but rather to "const evaluation" of some expressions at type-check time. I'll leave it where it is for now, we can decide again in the next PR which is going to add slices to this module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM |
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) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were placed in the wrong file => moved to
mdtest/literal/bytes.md
.