Skip to content

stubdoc: Fix crash on non-str docstring#15623

Merged
hauntsaninja merged 2 commits into
python:masterfrom
hamdanal:stubdoc-docstr
Jul 8, 2023
Merged

stubdoc: Fix crash on non-str docstring#15623
hauntsaninja merged 2 commits into
python:masterfrom
hamdanal:stubdoc-docstr

Conversation

@hamdanal

@hamdanal hamdanal commented Jul 8, 2023

Copy link
Copy Markdown
Collaborator

Encourted this while testing stubgen on pandas:

$ stubgen -p pandas
Traceback (most recent call last):
  File "/tmp/.venv/bin/stubgen", line 8, in <module>
    sys.exit(main())
  File "mypy/stubgen.py", line 1945, in main
  File "mypy/stubgen.py", line 1799, in generate_stubs
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 212, in generate_stub_for_c_module
    generate_c_type_stub(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 528, in generate_c_type_stub
    generate_c_function_stub(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 337, in generate_c_function_stub
    inferred = sig_gen.get_method_sig(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 140, in get_method_sig
    inferred = self.get_function_sig(cls, module_name, class_name)
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 120, in get_function_sig
    inferred = infer_sig_from_docstring(docstr, name)
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubdoc.py", line 264, in infer_sig_from_docstring
    tokens = tokenize.tokenize(io.BytesIO(docstr.encode("utf-8")).readline)
AttributeError: 'getset_descriptor' object has no attribute 'encode'

This is because of the definition of docstring as a descriptor here https://github.com/pandas-dev/pandas/blob/main/pandas/_libs/properties.pyx

(Explain how this PR changes mypy.)

hamdanal added 2 commits July 8, 2023 09:46
Encourted this while testing stubgen on pandas:

```console
$ stubgen -p pandas
Traceback (most recent call last):
  File "/tmp/.venv/bin/stubgen", line 8, in <module>
    sys.exit(main())
  File "mypy/stubgen.py", line 1945, in main
  File "mypy/stubgen.py", line 1799, in generate_stubs
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 212, in generate_stub_for_c_module
    generate_c_type_stub(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 528, in generate_c_type_stub
    generate_c_function_stub(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 337, in generate_c_function_stub
    inferred = sig_gen.get_method_sig(
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 140, in get_method_sig
    inferred = self.get_function_sig(cls, module_name, class_name)
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubgenc.py", line 120, in get_function_sig
    inferred = infer_sig_from_docstring(docstr, name)
  File "/tmp/.venv/lib/python3.10/site-packages/mypy/stubdoc.py", line 264, in infer_sig_from_docstring
    tokens = tokenize.tokenize(io.BytesIO(docstr.encode("utf-8")).readline)
AttributeError: 'getset_descriptor' object has no attribute 'encode'
```

This is because of the definition of docstring as a descriptor here
https://github.com/pandas-dev/pandas/blob/main/pandas/_libs/properties.pyx
@github-actions

github-actions Bot commented Jul 8, 2023

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@hauntsaninja
hauntsaninja merged commit ebfea94 into python:master Jul 8, 2023
@hamdanal
hamdanal deleted the stubdoc-docstr branch July 9, 2023 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants