@@ -811,7 +811,8 @@ pub struct DebugText {
811
811
pub trailing : String ,
812
812
}
813
813
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.
815
816
///
816
817
/// This type differs from the original Python AST ([JoinedStr]) in that it
817
818
/// doesn't join the implicitly concatenated parts into a single string. Instead,
@@ -842,7 +843,7 @@ pub struct FStringValue {
842
843
}
843
844
844
845
impl FStringValue {
845
- /// Creates a new f-string with the given value .
846
+ /// Creates a new f-string literal with a single [`FString`] part .
846
847
pub fn single ( value : FString ) -> Self {
847
848
Self {
848
849
inner : FStringValueInner :: Single ( FStringPart :: FString ( value) ) ,
@@ -854,9 +855,13 @@ impl FStringValue {
854
855
///
855
856
/// # Panics
856
857
///
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.
858
860
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
+ ) ;
860
865
Self {
861
866
inner : FStringValueInner :: Concatenated ( values) ,
862
867
}
@@ -896,7 +901,7 @@ impl FStringValue {
896
901
897
902
/// Returns an iterator over the [`StringLiteral`] parts contained in this value.
898
903
///
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,
900
905
///
901
906
/// ```python
902
907
/// "foo" f"bar {x}" "baz" f"qux"
@@ -909,7 +914,7 @@ impl FStringValue {
909
914
910
915
/// Returns an iterator over the [`FString`] parts contained in this value.
911
916
///
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,
913
918
///
914
919
/// ```python
915
920
/// "foo" f"bar {x}" "baz" f"qux"
@@ -1281,8 +1286,8 @@ impl fmt::Debug for FStringElements {
1281
1286
}
1282
1287
}
1283
1288
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 .
1286
1291
#[ derive( Clone , Debug , PartialEq ) ]
1287
1292
pub struct ExprStringLiteral {
1288
1293
pub range : TextRange ,
@@ -1307,7 +1312,7 @@ pub struct StringLiteralValue {
1307
1312
}
1308
1313
1309
1314
impl StringLiteralValue {
1310
- /// Creates a new single string literal with the given value .
1315
+ /// Creates a new string literal with a single [`StringLiteral`] part .
1311
1316
pub fn single ( string : StringLiteral ) -> Self {
1312
1317
Self {
1313
1318
inner : StringLiteralValueInner :: Single ( string) ,
@@ -1331,10 +1336,13 @@ impl StringLiteralValue {
1331
1336
///
1332
1337
/// # Panics
1333
1338
///
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.
1336
1341
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
+ ) ;
1338
1346
Self {
1339
1347
inner : StringLiteralValueInner :: Concatenated ( ConcatenatedStringLiteral {
1340
1348
strings,
@@ -1348,10 +1356,14 @@ impl StringLiteralValue {
1348
1356
matches ! ( self . inner, StringLiteralValueInner :: Concatenated ( _) )
1349
1357
}
1350
1358
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".
1352
1364
///
1353
1365
/// 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 .
1355
1367
pub fn is_unicode ( & self ) -> bool {
1356
1368
self . iter ( )
1357
1369
. next ( )
@@ -1385,7 +1397,11 @@ impl StringLiteralValue {
1385
1397
self . as_mut_slice ( ) . iter_mut ( )
1386
1398
}
1387
1399
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.
1389
1405
pub fn is_empty ( & self ) -> bool {
1390
1406
self . len ( ) == 0
1391
1407
}
@@ -1684,7 +1700,7 @@ impl From<StringLiteral> for Expr {
1684
1700
/// implicitly concatenated string.
1685
1701
#[ derive( Clone ) ]
1686
1702
struct ConcatenatedStringLiteral {
1687
- /// Each string literal that makes up the concatenated string.
1703
+ /// The individual [`StringLiteral`] parts that make up the concatenated string.
1688
1704
strings : Vec < StringLiteral > ,
1689
1705
/// The concatenated string value.
1690
1706
value : OnceLock < Box < str > > ,
@@ -1722,8 +1738,8 @@ impl Debug for ConcatenatedStringLiteral {
1722
1738
}
1723
1739
}
1724
1740
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 .
1727
1743
#[ derive( Clone , Debug , PartialEq ) ]
1728
1744
pub struct ExprBytesLiteral {
1729
1745
pub range : TextRange ,
@@ -1748,28 +1764,31 @@ pub struct BytesLiteralValue {
1748
1764
}
1749
1765
1750
1766
impl BytesLiteralValue {
1751
- /// Creates a new single bytes literal with the given value .
1767
+ /// Create a new bytestring literal with a single [`BytesLiteral`] part .
1752
1768
pub fn single ( value : BytesLiteral ) -> Self {
1753
1769
Self {
1754
1770
inner : BytesLiteralValueInner :: Single ( value) ,
1755
1771
}
1756
1772
}
1757
1773
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 .
1760
1776
///
1761
1777
/// # Panics
1762
1778
///
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.
1765
1781
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
+ ) ;
1767
1786
Self {
1768
1787
inner : BytesLiteralValueInner :: Concatenated ( values) ,
1769
1788
}
1770
1789
}
1771
1790
1772
- /// Returns `true` if the bytes literal is implicitly concatenated.
1791
+ /// Returns `true` if the bytestring is implicitly concatenated.
1773
1792
pub const fn is_implicit_concatenated ( & self ) -> bool {
1774
1793
matches ! ( self . inner, BytesLiteralValueInner :: Concatenated ( _) )
1775
1794
}
@@ -1801,17 +1820,21 @@ impl BytesLiteralValue {
1801
1820
self . as_mut_slice ( ) . iter_mut ( )
1802
1821
}
1803
1822
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.
1805
1828
pub fn is_empty ( & self ) -> bool {
1806
1829
self . iter ( ) . all ( |part| part. is_empty ( ) )
1807
1830
}
1808
1831
1809
- /// Returns the length of the concatenated bytes .
1832
+ /// Returns the length of the concatenated bytestring .
1810
1833
pub fn len ( & self ) -> usize {
1811
1834
self . iter ( ) . map ( |part| part. len ( ) ) . sum ( )
1812
1835
}
1813
1836
1814
- /// Returns an iterator over the bytes of the concatenated bytes .
1837
+ /// Returns an iterator over the bytes of the concatenated bytestring .
1815
1838
pub fn bytes ( & self ) -> impl Iterator < Item = u8 > + ' _ {
1816
1839
self . iter ( ) . flat_map ( |part| part. as_slice ( ) . iter ( ) . copied ( ) )
1817
1840
}
@@ -1865,10 +1888,10 @@ impl<'a> From<&'a BytesLiteralValue> for Cow<'a, [u8]> {
1865
1888
/// An internal representation of [`BytesLiteralValue`].
1866
1889
#[ derive( Clone , Debug , PartialEq ) ]
1867
1890
enum BytesLiteralValueInner {
1868
- /// A single bytes literal i.e., `b"foo"`.
1891
+ /// A single-part bytestring literal i.e., `b"foo"`.
1869
1892
Single ( BytesLiteral ) ,
1870
1893
1871
- /// An implicitly concatenated bytes literals i.e., `b"foo" b"bar"`.
1894
+ /// An implicitly concatenated bytestring literal i.e., `b"foo" b"bar"`.
1872
1895
Concatenated ( Vec < BytesLiteral > ) ,
1873
1896
}
1874
1897
0 commit comments