-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Stubgen bug] Fix behavior for Instance Variables in C extensions #12150
Labels
Comments
Hi, did anyone get a chance to look at the reproducer? |
Gentle ping for the maintainers to have a look at this. |
Mypy is a volunteer-run project with limited resources. Any fix for this issue will likely have to come from an external contributor. |
shubz1998
pushed a commit
to shubz1998/mypy
that referenced
this issue
Apr 5, 2022
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 python#12150.
shubz1998
pushed a commit
to shubz1998/mypy
that referenced
this issue
Apr 5, 2022
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 python#12150.
shubz1998
pushed a commit
to shubz1998/mypy
that referenced
this issue
Apr 5, 2022
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 python#12150.
JelleZijlstra
pushed a commit
that referenced
this issue
Apr 11, 2022
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. Fixes #12150.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stubgen generates wrong stubs for instance variables defined in C extensions.
In the attached
extension.C
, I've created a class named Custom which has 3 instance variables: var1,var2,var3. On running stubgen for this extension, the generated .pyi file contains:Detailed Reproducer:
g++ -fPIC -std=c++11 -I. -I/opt/python/python-3.9/include/python3.9 -Iextension -c extension/extension.C -o extension.o
in directory $DIR.g++ -shared -L/opt/python/python-3.9/lib64 -Wl,-R/opt/python/python-3.9/lib64 extension.o -o extension.so
in directory $DIR.env PYTHONPATH=$DIR stubgen -m extension
On digging into this, I found this is due to a wrong check in
stubgenc.py#is_c_property
.The check should contain "or" (instead of "and") since the instance variables in case of C extension doesn't have the "fget" attribute but
inspect.isdatadescription
return True.On making this change locally, stubgen runs fine.
If this looks good to you, I can raise a PR for the same.
extension.zip
The text was updated successfully, but these errors were encountered: