@@ -9,6 +9,7 @@ pub mod types;
99/// Builds a type hint from a module name and a member name in the module
1010///
1111/// ```
12+ /// use pyo3::type_hint_identifier;
1213/// use pyo3::inspect::PyStaticExpr;
1314///
1415/// const T: PyStaticExpr = type_hint_identifier!("datetime", "date");
@@ -40,9 +41,10 @@ pub(crate) use type_hint_identifier;
4041/// Builds the union of multiple type hints
4142///
4243/// ```
44+ /// use pyo3::{type_hint_identifier, type_hint_union};
4345/// use pyo3::inspect::PyStaticExpr;
4446///
45- /// const T: PyStaticExpr = type_hint_union!(type_hint_identifier!("datetime.datetime" ), type_hint_identifier!("datetime ", "date "));
47+ /// const T: PyStaticExpr = type_hint_union!(type_hint_identifier!("builtins", "int" ), type_hint_identifier!("builtins ", "float "));
4648/// assert_eq!(T.to_string(), "int | float");
4749/// ```
4850#[ macro_export]
@@ -59,13 +61,14 @@ pub(crate) use type_hint_union;
5961/// Builds a subscribed type hint
6062///
6163/// ```
64+ /// use pyo3::{type_hint_identifier, type_hint_subscript};
6265/// use pyo3::inspect::PyStaticExpr;
6366///
6467/// const T: PyStaticExpr = type_hint_subscript!(type_hint_identifier!("collections.abc", "Sequence"), type_hint_identifier!("builtins", "float"));
6568/// assert_eq!(T.to_string(), "collections.abc.Sequence[float]");
6669///
67- /// const 2 : PyStaticExpr = type_hint_subscript!(type_hint_identifier!("builtins", "dict"), type_hint_identifier!("builtins", "str"), type_hint_identifier!("builtins", "float"));
68- /// assert_eq!(T .to_string(), "dict[str, float]");
70+ /// const T2 : PyStaticExpr = type_hint_subscript!(type_hint_identifier!("builtins", "dict"), type_hint_identifier!("builtins", "str"), type_hint_identifier!("builtins", "float"));
71+ /// assert_eq!(T2 .to_string(), "dict[str, float]");
6972/// ```
7073#[ macro_export]
7174macro_rules! type_hint_subscript {
@@ -193,7 +196,7 @@ pub const fn serialize_for_introspection(expr: &PyStaticExpr, mut output: &mut [
193196pub const fn serialized_len_for_introspection ( expr : & PyStaticExpr ) -> usize {
194197 match expr {
195198 PyStaticExpr :: Constant { value } => match value {
196- PyStaticConstant :: None => 34 ,
199+ PyStaticConstant :: None => 33 ,
197200 } ,
198201 PyStaticExpr :: Name { id, kind } => {
199202 ( match kind {
@@ -214,7 +217,7 @@ pub const fn serialized_len_for_introspection(expr: &PyStaticExpr) -> usize {
214217 PyStaticExpr :: Tuple { elts } => 5 + serialized_container_len_for_introspection ( elts) ,
215218 PyStaticExpr :: List { elts } => 4 + serialized_container_len_for_introspection ( elts) ,
216219 PyStaticExpr :: Subscript { value, slice } => {
217- 39 + serialized_len_for_introspection ( value) + serialized_len_for_introspection ( slice)
220+ 38 + serialized_len_for_introspection ( value) + serialized_len_for_introspection ( slice)
218221 }
219222 }
220223}
@@ -335,7 +338,7 @@ const fn write_container_and_move_forward<'a>(
335338}
336339
337340const fn serialized_container_len_for_introspection ( elts : & [ PyStaticExpr ] ) -> usize {
338- let mut len = 20 ;
341+ let mut len = 21 ;
339342 let mut i = 0 ;
340343 while i < elts. len ( ) {
341344 if i > 0 {
@@ -385,36 +388,58 @@ mod tests {
385388
386389 #[ test]
387390 fn test_serialize_for_introspection ( ) {
388- const T : PyStaticExpr = type_hint_subscript ! (
389- type_hint_identifier!( "typing" , "Callable" ) ,
391+ fn check_serialization ( expr : PyStaticExpr , expected : & str ) {
392+ let mut out = vec ! [ 0 ; serialized_len_for_introspection( & expr) ] ;
393+ serialize_for_introspection ( & expr, & mut out) ;
394+ assert_eq ! ( std:: str :: from_utf8( & out) . unwrap( ) , expected)
395+ }
396+
397+ check_serialization (
398+ PyStaticExpr :: Constant {
399+ value : PyStaticConstant :: None ,
400+ } ,
401+ r#"{"type":"constant","kind":"none"}"# ,
402+ ) ;
403+ check_serialization (
404+ type_hint_identifier ! ( "builtins" , "int" ) ,
405+ r#"{"type":"name","kind":"global","id":"int"}"# ,
406+ ) ;
407+ check_serialization (
408+ PyStaticExpr :: Name {
409+ id : "int" ,
410+ kind : PyStaticNameKind :: Local ,
411+ } ,
412+ r#"{"type":"name","kind":"local","id":"int"}"# ,
413+ ) ;
414+ check_serialization (
415+ type_hint_identifier ! ( "datetime" , "date" ) ,
416+ r#"{"type":"attribute","value":{"type":"name","kind":"global","id":"datetime"},"attr":"date"}"# ,
417+ ) ;
418+ check_serialization (
419+ type_hint_union ! (
420+ type_hint_identifier!( "builtins" , "int" ) ,
421+ type_hint_identifier!( "builtins" , "float" )
422+ ) ,
423+ r#"{"type":"binop","left":{"type":"name","kind":"global","id":"int"},"op":"bitor","right":{"type":"name","kind":"global","id":"float"}}"# ,
424+ ) ;
425+ check_serialization (
426+ PyStaticExpr :: Tuple {
427+ elts : & [ type_hint_identifier ! ( "builtins" , "list" ) ] ,
428+ } ,
429+ r#"{"type":"tuple","elts":[{"type":"name","kind":"global","id":"list"}]}"# ,
430+ ) ;
431+ check_serialization (
390432 PyStaticExpr :: List {
391- elts: & [
392- type_hint_union!(
393- PyStaticExpr :: Name {
394- id: "int" ,
395- kind: PyStaticNameKind :: Global
396- } ,
397- PyStaticExpr :: Name {
398- id: "weird" ,
399- kind: PyStaticNameKind :: Local
400- }
401- ) ,
402- type_hint_identifier!( "datetime" , "time" ) ,
403- ]
433+ elts : & [ type_hint_identifier ! ( "builtins" , "list" ) ] ,
404434 } ,
405- PyStaticExpr :: Constant {
406- value: PyStaticConstant :: None
407- }
435+ r#"{"type":"list","elts":[{"type":"name","kind":"global","id":"list"}]}"# ,
436+ ) ;
437+ check_serialization (
438+ type_hint_subscript ! (
439+ type_hint_identifier!( "builtins" , "list" ) ,
440+ type_hint_identifier!( "builtins" , "int" )
441+ ) ,
442+ r#"{"type":"subscript","value":{"type":"name","kind":"global","id":"list"},"slice":{"type":"name","kind":"global","id":"int"}}"# ,
408443 ) ;
409- const SER_LEN : usize = serialized_len_for_introspection ( & T ) ;
410- const SER : [ u8 ; SER_LEN ] = {
411- let mut out: [ u8 ; SER_LEN ] = [ 0 ; SER_LEN ] ;
412- serialize_for_introspection ( & T , & mut out) ;
413- out
414- } ;
415- assert_eq ! (
416- std:: str :: from_utf8( & SER ) . unwrap( ) ,
417- r#"{"type":"subscript","value":{"type":"attribute","value":{"type":"name","kind":"global","id":"typing"},"attr":"Callable"},"slice":{"type":"tuple","elts":[{"type":"list","elts":[{"type":"binop","left":{"type":"name","kind":"global","id":"int"},"op":"bitor","right":{"type":"name","kind":"local","id":"weird"}},{"type":"attribute","value":{"type":"name","kind":"global","id":"datetime"},"attr":"time"}]},{"type":"constant","kind":"none"}]}}"#
418- )
419444 }
420445}
0 commit comments