Skip to content

Commit 16d0625

Browse files
authored
Improve internal docs for various string-node APIs (#16256)
1 parent 25920fe commit 16d0625

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

crates/ruff_python_ast/src/nodes.rs

+53-30
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,8 @@ pub struct DebugText {
811811
pub trailing: String,
812812
}
813813

814-
/// An AST node used to represent an f-string.
814+
/// An AST node that represents either a single-part f-string literal
815+
/// or an implicitly concatenated f-string literal.
815816
///
816817
/// This type differs from the original Python AST ([JoinedStr]) in that it
817818
/// doesn't join the implicitly concatenated parts into a single string. Instead,
@@ -842,7 +843,7 @@ pub struct FStringValue {
842843
}
843844

844845
impl FStringValue {
845-
/// Creates a new f-string with the given value.
846+
/// Creates a new f-string literal with a single [`FString`] part.
846847
pub fn single(value: FString) -> Self {
847848
Self {
848849
inner: FStringValueInner::Single(FStringPart::FString(value)),
@@ -854,9 +855,13 @@ impl FStringValue {
854855
///
855856
/// # Panics
856857
///
857-
/// Panics if `values` is less than 2. Use [`FStringValue::single`] instead.
858+
/// Panics if `values` has less than 2 elements.
859+
/// Use [`FStringValue::single`] instead.
858860
pub fn concatenated(values: Vec<FStringPart>) -> Self {
859-
assert!(values.len() > 1);
861+
assert!(
862+
values.len() > 1,
863+
"Use `FStringValue::single` to create single-part f-strings"
864+
);
860865
Self {
861866
inner: FStringValueInner::Concatenated(values),
862867
}
@@ -896,7 +901,7 @@ impl FStringValue {
896901

897902
/// Returns an iterator over the [`StringLiteral`] parts contained in this value.
898903
///
899-
/// Note that this doesn't nest into the f-string parts. For example,
904+
/// Note that this doesn't recurse into the f-string parts. For example,
900905
///
901906
/// ```python
902907
/// "foo" f"bar {x}" "baz" f"qux"
@@ -909,7 +914,7 @@ impl FStringValue {
909914

910915
/// Returns an iterator over the [`FString`] parts contained in this value.
911916
///
912-
/// Note that this doesn't nest into the f-string parts. For example,
917+
/// Note that this doesn't recurse into the f-string parts. For example,
913918
///
914919
/// ```python
915920
/// "foo" f"bar {x}" "baz" f"qux"
@@ -1281,8 +1286,8 @@ impl fmt::Debug for FStringElements {
12811286
}
12821287
}
12831288

1284-
/// An AST node that represents either a single string literal or an implicitly
1285-
/// concatenated string literals.
1289+
/// An AST node that represents either a single-part string literal
1290+
/// or an implicitly concatenated string literal.
12861291
#[derive(Clone, Debug, PartialEq)]
12871292
pub struct ExprStringLiteral {
12881293
pub range: TextRange,
@@ -1307,7 +1312,7 @@ pub struct StringLiteralValue {
13071312
}
13081313

13091314
impl StringLiteralValue {
1310-
/// Creates a new single string literal with the given value.
1315+
/// Creates a new string literal with a single [`StringLiteral`] part.
13111316
pub fn single(string: StringLiteral) -> Self {
13121317
Self {
13131318
inner: StringLiteralValueInner::Single(string),
@@ -1331,10 +1336,13 @@ impl StringLiteralValue {
13311336
///
13321337
/// # Panics
13331338
///
1334-
/// Panics if `strings` is less than 2. Use [`StringLiteralValue::single`]
1335-
/// instead.
1339+
/// Panics if `strings` has less than 2 elements.
1340+
/// Use [`StringLiteralValue::single`] instead.
13361341
pub fn concatenated(strings: Vec<StringLiteral>) -> Self {
1337-
assert!(strings.len() > 1);
1342+
assert!(
1343+
strings.len() > 1,
1344+
"Use `StringLiteralValue::single` to create single-part strings"
1345+
);
13381346
Self {
13391347
inner: StringLiteralValueInner::Concatenated(ConcatenatedStringLiteral {
13401348
strings,
@@ -1348,10 +1356,14 @@ impl StringLiteralValue {
13481356
matches!(self.inner, StringLiteralValueInner::Concatenated(_))
13491357
}
13501358

1351-
/// Returns `true` if the string literal is a unicode string.
1359+
/// Returns `true` if the string literal has a `u` prefix, e.g. `u"foo"`.
1360+
///
1361+
/// Although all strings in Python 3 are valid unicode (and the `u` prefix
1362+
/// is only retained for backwards compatibility), these strings are known as
1363+
/// "unicode strings".
13521364
///
13531365
/// For an implicitly concatenated string, it returns `true` only if the first
1354-
/// string literal is a unicode string.
1366+
/// [`StringLiteral`] has the `u` prefix.
13551367
pub fn is_unicode(&self) -> bool {
13561368
self.iter()
13571369
.next()
@@ -1385,7 +1397,11 @@ impl StringLiteralValue {
13851397
self.as_mut_slice().iter_mut()
13861398
}
13871399

1388-
/// Returns `true` if the string literal value is empty.
1400+
/// Returns `true` if the node represents an empty string.
1401+
///
1402+
/// Note that a [`StringLiteralValue`] node will always have >=1 [`StringLiteral`] parts
1403+
/// inside it. This method checks whether the value of the concatenated parts is equal
1404+
/// to the empty string, not whether the string has 0 parts inside it.
13891405
pub fn is_empty(&self) -> bool {
13901406
self.len() == 0
13911407
}
@@ -1684,7 +1700,7 @@ impl From<StringLiteral> for Expr {
16841700
/// implicitly concatenated string.
16851701
#[derive(Clone)]
16861702
struct ConcatenatedStringLiteral {
1687-
/// Each string literal that makes up the concatenated string.
1703+
/// The individual [`StringLiteral`] parts that make up the concatenated string.
16881704
strings: Vec<StringLiteral>,
16891705
/// The concatenated string value.
16901706
value: OnceLock<Box<str>>,
@@ -1722,8 +1738,8 @@ impl Debug for ConcatenatedStringLiteral {
17221738
}
17231739
}
17241740

1725-
/// An AST node that represents either a single bytes literal or an implicitly
1726-
/// concatenated bytes literals.
1741+
/// An AST node that represents either a single-part bytestring literal
1742+
/// or an implicitly concatenated bytestring literal.
17271743
#[derive(Clone, Debug, PartialEq)]
17281744
pub struct ExprBytesLiteral {
17291745
pub range: TextRange,
@@ -1748,28 +1764,31 @@ pub struct BytesLiteralValue {
17481764
}
17491765

17501766
impl BytesLiteralValue {
1751-
/// Creates a new single bytes literal with the given value.
1767+
/// Create a new bytestring literal with a single [`BytesLiteral`] part.
17521768
pub fn single(value: BytesLiteral) -> Self {
17531769
Self {
17541770
inner: BytesLiteralValueInner::Single(value),
17551771
}
17561772
}
17571773

1758-
/// Creates a new bytes literal with the given values that represents an
1759-
/// implicitly concatenated bytes.
1774+
/// Creates a new bytestring literal with the given values that represents an
1775+
/// implicitly concatenated bytestring.
17601776
///
17611777
/// # Panics
17621778
///
1763-
/// Panics if `values` is less than 2. Use [`BytesLiteralValue::single`]
1764-
/// instead.
1779+
/// Panics if `values` has less than 2 elements.
1780+
/// Use [`BytesLiteralValue::single`] instead.
17651781
pub fn concatenated(values: Vec<BytesLiteral>) -> Self {
1766-
assert!(values.len() > 1);
1782+
assert!(
1783+
values.len() > 1,
1784+
"Use `BytesLiteralValue::single` to create single-part bytestrings"
1785+
);
17671786
Self {
17681787
inner: BytesLiteralValueInner::Concatenated(values),
17691788
}
17701789
}
17711790

1772-
/// Returns `true` if the bytes literal is implicitly concatenated.
1791+
/// Returns `true` if the bytestring is implicitly concatenated.
17731792
pub const fn is_implicit_concatenated(&self) -> bool {
17741793
matches!(self.inner, BytesLiteralValueInner::Concatenated(_))
17751794
}
@@ -1801,17 +1820,21 @@ impl BytesLiteralValue {
18011820
self.as_mut_slice().iter_mut()
18021821
}
18031822

1804-
/// Returns `true` if the concatenated bytes has a length of zero.
1823+
/// Return `true` if the node represents an empty bytestring.
1824+
///
1825+
/// Note that a [`BytesLiteralValue`] node will always have >=1 [`BytesLiteral`] parts
1826+
/// inside it. This method checks whether the value of the concatenated parts is equal
1827+
/// to the empty bytestring, not whether the bytestring has 0 parts inside it.
18051828
pub fn is_empty(&self) -> bool {
18061829
self.iter().all(|part| part.is_empty())
18071830
}
18081831

1809-
/// Returns the length of the concatenated bytes.
1832+
/// Returns the length of the concatenated bytestring.
18101833
pub fn len(&self) -> usize {
18111834
self.iter().map(|part| part.len()).sum()
18121835
}
18131836

1814-
/// Returns an iterator over the bytes of the concatenated bytes.
1837+
/// Returns an iterator over the bytes of the concatenated bytestring.
18151838
pub fn bytes(&self) -> impl Iterator<Item = u8> + '_ {
18161839
self.iter().flat_map(|part| part.as_slice().iter().copied())
18171840
}
@@ -1865,10 +1888,10 @@ impl<'a> From<&'a BytesLiteralValue> for Cow<'a, [u8]> {
18651888
/// An internal representation of [`BytesLiteralValue`].
18661889
#[derive(Clone, Debug, PartialEq)]
18671890
enum BytesLiteralValueInner {
1868-
/// A single bytes literal i.e., `b"foo"`.
1891+
/// A single-part bytestring literal i.e., `b"foo"`.
18691892
Single(BytesLiteral),
18701893

1871-
/// An implicitly concatenated bytes literals i.e., `b"foo" b"bar"`.
1894+
/// An implicitly concatenated bytestring literal i.e., `b"foo" b"bar"`.
18721895
Concatenated(Vec<BytesLiteral>),
18731896
}
18741897

0 commit comments

Comments
 (0)