File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 189
189
"__iter__" ,
190
190
}
191
191
192
+ # These magic methods always return the same type.
193
+ KNOWN_MAGIC_METHODS_RETURN_TYPES : Final = {
194
+ "__len__" : "int" ,
195
+ "__length_hint__" : "int" ,
196
+ "__init__" : "None" ,
197
+ "__del__" : "None" ,
198
+ "__bool__" : "bool" ,
199
+ "__bytes__" : "bytes" ,
200
+ "__format__" : "str" ,
201
+ "__contains__" : "bool" ,
202
+ "__complex__" : "complex" ,
203
+ "__int__" : "int" ,
204
+ "__float__" : "float" ,
205
+ "__index__" : "int" ,
206
+ }
207
+
192
208
193
209
class Options :
194
210
"""Represents stubgen options.
@@ -735,6 +751,8 @@ def visit_func_def(
735
751
# Always assume abstract methods return Any unless explicitly annotated. Also
736
752
# some dunder methods should not have a None return type.
737
753
retname = None # implicit Any
754
+ elif o .name in KNOWN_MAGIC_METHODS_RETURN_TYPES :
755
+ retname = KNOWN_MAGIC_METHODS_RETURN_TYPES [o .name ]
738
756
elif has_yield_expression (o ):
739
757
self .add_abc_import ("Generator" )
740
758
yield_name = "None"
Original file line number Diff line number Diff line change @@ -2705,3 +2705,32 @@ def f():
2705
2705
return 0
2706
2706
[out]
2707
2707
def f(): ...
2708
+
2709
+ [case testKnownMagicMethodsReturnTypes]
2710
+ class Some:
2711
+ def __len__(self): ...
2712
+ def __length_hint__(self): ...
2713
+ def __init__(self): ...
2714
+ def __del__(self): ...
2715
+ def __bool__(self): ...
2716
+ def __bytes__(self): ...
2717
+ def __format__(self, spec): ...
2718
+ def __contains__(self, obj): ...
2719
+ def __complex__(self): ...
2720
+ def __int__(self): ...
2721
+ def __float__(self): ...
2722
+ def __index__(self): ...
2723
+ [out]
2724
+ class Some:
2725
+ def __len__(self) -> int: ...
2726
+ def __length_hint__(self) -> int: ...
2727
+ def __init__(self) -> None: ...
2728
+ def __del__(self) -> None: ...
2729
+ def __bool__(self) -> bool: ...
2730
+ def __bytes__(self) -> bytes: ...
2731
+ def __format__(self, spec) -> str: ...
2732
+ def __contains__(self, obj) -> bool: ...
2733
+ def __complex__(self) -> complex: ...
2734
+ def __int__(self) -> int: ...
2735
+ def __float__(self) -> float: ...
2736
+ def __index__(self) -> int: ...
You can’t perform that action at this time.
0 commit comments