Skip to content

Commit db4be74

Browse files
author
sishubha
committed
Fix stubgen
It is not necessary for instance variables to have the fget attrbute (e.g. instance variable in a C class in an extension) but inspect.isdatadescriptor return True as expected, hence we update the 'is_c_property' check. Since special attributes (like __dict__ etc) also passes 'is_c_property' check, we ignore all such special attributes in 'generate_c_property_stub' while creating the contents of stub file. Also, 'is_c_property_readonly' assumed that the property would always have 'fset' attribute which again is not true for instance variables in C extension. Hence make the check defensive by first checking if 'fset' attribute even exists or not. This fixes #12150.
1 parent ee0638f commit db4be74

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

mypy/stubgenc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ def is_c_classmethod(obj: object) -> bool:
121121

122122

123123
def is_c_property(obj: object) -> bool:
124-
return inspect.isdatadescriptor(obj) and hasattr(obj, 'fget')
124+
return inspect.isdatadescriptor(obj) or hasattr(obj, 'fget')
125125

126126

127127
def is_c_property_readonly(prop: Any) -> bool:
128-
return prop.fset is None
128+
return hasattr(prop, 'fset') and prop.fset is None
129129

130130

131131
def is_c_type(obj: object) -> bool:
@@ -287,6 +287,10 @@ def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
287287
else:
288288
return None
289289

290+
# Ignore special properties/attributes.
291+
if name.startswith('__') and name.endswith('__'):
292+
return
293+
290294
inferred = infer_prop_type(getattr(obj, '__doc__', None))
291295
if not inferred:
292296
fget = getattr(obj, 'fget', None)

0 commit comments

Comments
 (0)