@@ -365,6 +365,7 @@ def generate_c_function_stub(
365
365
if is_overloaded :
366
366
imports .append ("from typing import overload" )
367
367
if inferred :
368
+ docstr = getattr (obj , "__doc__" , None )
368
369
for signature in inferred :
369
370
args : list [str ] = []
370
371
for arg in signature .args :
@@ -458,6 +459,7 @@ def generate_c_property_stub(
458
459
module : ModuleType | None = None ,
459
460
known_modules : list [str ] | None = None ,
460
461
imports : list [str ] | None = None ,
462
+ include_docstrings : bool = False ,
461
463
) -> None :
462
464
"""Generate property stub using introspection of 'obj'.
463
465
@@ -476,25 +478,39 @@ def infer_prop_type(docstr: str | None) -> str | None:
476
478
else :
477
479
return None
478
480
479
- inferred = infer_prop_type (getattr (obj , "__doc__" , None ))
481
+ docstr : str | None = getattr (obj , "__doc__" , None )
482
+ inferred = infer_prop_type (docstr )
480
483
if not inferred :
481
484
fget = getattr (obj , "fget" , None )
482
485
inferred = infer_prop_type (getattr (fget , "__doc__" , None ))
486
+ docstr = getattr (fget , "__doc__" , None ) or docstr
483
487
if not inferred :
484
488
inferred = "Any"
485
489
486
490
if module is not None and imports is not None and known_modules is not None :
487
491
inferred = strip_or_import (inferred , module , known_modules , imports )
488
492
493
+ if include_docstrings and docstr :
494
+ # Quoted docstr
495
+ docstr = mypy .util .quote_docstring (docstr .strip ())
496
+ # Indented docstr
497
+ docstr = "\n " .join (docstr .split ("\n " ))
498
+
489
499
if is_static_property (obj ):
490
500
trailing_comment = " # read-only" if readonly else ""
491
501
static_properties .append (f"{ name } : ClassVar[{ inferred } ] = ...{ trailing_comment } " )
502
+ if include_docstrings and docstr :
503
+ static_properties .extend (docstr .split ("\n " ))
492
504
else : # regular property
493
505
if readonly :
494
506
ro_properties .append ("@property" )
495
507
ro_properties .append (f"def { name } (self) -> { inferred } : ..." )
508
+ if include_docstrings and docstr :
509
+ ro_properties .extend (f" { docstr } " .split ("\n " ))
496
510
else :
497
511
rw_properties .append (f"{ name } : { inferred } " )
512
+ if include_docstrings and docstr :
513
+ rw_properties .extend (docstr .split ("\n " ))
498
514
499
515
500
516
def generate_c_type_stub (
@@ -561,6 +577,7 @@ def generate_c_type_stub(
561
577
module = module ,
562
578
known_modules = known_modules ,
563
579
imports = imports ,
580
+ include_docstrings = include_docstrings ,
564
581
)
565
582
elif is_c_type (value ):
566
583
generate_c_type_stub (
@@ -605,8 +622,19 @@ def generate_c_type_stub(
605
622
)
606
623
else :
607
624
bases_str = ""
625
+
626
+ docstr : str | None = getattr (obj , "__doc__" , None )
627
+ if include_docstrings and docstr :
628
+ # Quoted docstr
629
+ docstr = mypy .util .quote_docstring (docstr .strip ())
630
+ # Indented docstr
631
+ docstr = "\n " .join (docstr .split ("\n " ))
632
+ # Add the docstr class type
633
+
608
634
if types or static_properties or rw_properties or methods or ro_properties :
609
635
output .append (f"class { class_name } { bases_str } :" )
636
+ if include_docstrings and docstr :
637
+ output .extend (f" { docstr } " .split ("\n " ))
610
638
for line in types :
611
639
if (
612
640
output
@@ -626,6 +654,8 @@ def generate_c_type_stub(
626
654
output .append (f" { line } " )
627
655
else :
628
656
output .append (f"class { class_name } { bases_str } : ..." )
657
+ if include_docstrings and docstr :
658
+ output .extend (f" { docstr } " .split ("\n " ) + ["" ])
629
659
630
660
631
661
def get_type_fullname (typ : type ) -> str :
0 commit comments