Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Crash: TypeError: a bytes-like object is required, not 'str' #569

Open
tushar-deepsource opened this issue Jan 10, 2022 · 7 comments · May be fixed by #570
Open

Crash: TypeError: a bytes-like object is required, not 'str' #569

tushar-deepsource opened this issue Jan 10, 2022 · 7 comments · May be fixed by #570

Comments

@tushar-deepsource
Copy link

Code snippet:

def f():
    b"""abc"""

Output:

$ pydocstyle mytest.py 
Traceback (most recent call last):
  File "/Users/tusharsadhwani/code/marvin-python/venv3/bin/pydocstyle", line 8, in <module>
    sys.exit(main())
  File "/Users/tusharsadhwani/code/marvin-python/venv3/lib/python3.10/site-packages/pydocstyle/cli.py", line 68, in main
    sys.exit(run_pydocstyle())
  File "/Users/tusharsadhwani/code/marvin-python/venv3/lib/python3.10/site-packages/pydocstyle/cli.py", line 44, in run_pydocstyle
    errors.extend(check((filename,), select=checked_codes,
  File "/Users/tusharsadhwani/code/marvin-python/venv3/lib/python3.10/site-packages/pydocstyle/checker.py", line 993, in check
    for error in ConventionChecker().check_source(source, filename,
  File "/Users/tusharsadhwani/code/marvin-python/venv3/lib/python3.10/site-packages/pydocstyle/checker.py", line 143, in check_source
    error = this_check(self, definition,
  File "/Users/tusharsadhwani/code/marvin-python/venv3/lib/python3.10/site-packages/pydocstyle/checker.py", line 204, in check_one_liners
    lines = safe_literal_eval(docstring).split('\n')
TypeError: a bytes-like object is required, not 'str'
@tushar-deepsource tushar-deepsource linked a pull request Jan 10, 2022 that will close this issue
2 tasks
@cclauss
Copy link
Contributor

cclauss commented Jan 3, 2023

What use case necessitates creating a docstring that is bytes?

I can see a reason for creating a docstring that is a r"raw string" but not a reason for creating a docstring that is a b"bytes".

@tusharsadhwani
Copy link

@cclauss there's no specific reason per se, but if I were to do that, I'd hope that pydocstyle won't crash while running on my code.

@cclauss
Copy link
Contributor

cclauss commented Jan 3, 2023

My sense is that it is a good thing that bad input results in an exception being raised.

@sambhav
Copy link
Member

sambhav commented Jan 3, 2023

+1 I agree with @cclauss. If it helps improve things, we might even want to catch that type error and add a helpful error message that points to the above link. Although I'm ambivalent on the latter.

@tusharsadhwani
Copy link

Having the program crash with a stack trace is bad for two reasons:

  • it stops pydocstyle completely, so other files don't get checked.
  • it gives mixed signals: "Is their code buggy? Is this intentional?" There's no real way to figure out. Better to catch it and display an actual user facing message.

@cclauss
Copy link
Contributor

cclauss commented Jan 3, 2023

Their code is buggy.
Python 2: bytes == str
Python 3: bytes != str
The PEP is clear that docstrings should be a string literal.

@tushar-deepsource
Copy link
Author

What I meant here was: When pydocstyle crashes, the user doesn't know if it's a bug in their code, or is it a bug in pydocstyle's code.

Certainly, when I run a python program and it crashes I assume there's a bug in the program, and not the input I gave.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants