1- use crate :: model:: { Argument , Class , Function , Module , VariableLengthArgument } ;
2- use std:: collections:: HashMap ;
1+ use crate :: model:: { Argument , Class , Const , Function , Module , VariableLengthArgument } ;
2+ use std:: collections:: { BTreeSet , HashMap } ;
33use std:: path:: { Path , PathBuf } ;
44
55/// Generates the [type stubs](https://typing.readthedocs.io/en/latest/source/stubs.html) of a given module.
@@ -32,16 +32,29 @@ fn add_module_stub_files(
3232
3333/// Generates the module stubs to a String, not including submodules
3434fn module_stubs ( module : & Module ) -> String {
35+ let mut modules_to_import = BTreeSet :: new ( ) ;
3536 let mut elements = Vec :: new ( ) ;
3637 for class in & module. classes {
3738 elements. push ( class_stubs ( class) ) ;
3839 }
3940 for function in & module. functions {
4041 elements. push ( function_stubs ( function) ) ;
4142 }
43+ for konst in & module. consts {
44+ elements. push ( const_stubs ( konst, & mut modules_to_import) ) ;
45+ }
4246
43- // We insert two line jumps (i.e. empty strings) only above and below multiple line elements (classes with methods, functions with decorators)
4447 let mut output = String :: new ( ) ;
48+
49+ for module_to_import in & modules_to_import {
50+ output. push_str ( & format ! ( "import {module_to_import}\n " ) ) ;
51+ }
52+
53+ if !modules_to_import. is_empty ( ) {
54+ output. push ( '\n' )
55+ }
56+
57+ // We insert two line jumps (i.e. empty strings) only above and below multiple line elements (classes with methods, functions with decorators)
4558 for element in elements {
4659 let is_multiline = element. contains ( '\n' ) ;
4760 if is_multiline && !output. is_empty ( ) && !output. ends_with ( "\n \n " ) {
@@ -53,6 +66,7 @@ fn module_stubs(module: &Module) -> String {
5366 output. push ( '\n' ) ;
5467 }
5568 }
69+
5670 // We remove a line jump at the end if they are two
5771 if output. ends_with ( "\n \n " ) {
5872 output. pop ( ) ;
@@ -111,6 +125,12 @@ fn function_stubs(function: &Function) -> String {
111125 buffer
112126}
113127
128+ fn const_stubs ( konst : & Const , modules_to_import : & mut BTreeSet < String > ) -> String {
129+ modules_to_import. insert ( "typing" . to_string ( ) ) ;
130+ let Const { name, value } = konst;
131+ format ! ( "{name}: typing.Final = {value}" )
132+ }
133+
114134fn argument_stub ( argument : & Argument ) -> String {
115135 let mut output = argument. name . clone ( ) ;
116136 if let Some ( default_value) = & argument. default_value {
0 commit comments