python-lsp / pylsp-mypy

Mypy plugin for the Python LSP Server.
MIT License
118 stars 35 forks source link

Bug: Failed to load hook pylsp_lint: 'utf-8' codec can't decode byte 0xb5 in position 3 #55

Closed KagaJiankui closed 1 year ago

KagaJiankui commented 1 year ago

Raised following error logs when using python-lsp with jupyter lab 3.5.2:

2022-12-27 19:33:28,495 中国标准时间 - WARNING - pylsp.plugins._resolvers - Something went wrong when resolving label for <Completion: man>: 'NoneType' object has no attribute 'is_module'
2022-12-27 19:33:28,577 中国标准时间 - WARNING - pylsp.plugins._resolvers - Something went wrong when resolving label for <Completion: Menu>: 'NoneType' object has no attribute 'is_module'
2022-12-27 19:33:28,951 中国标准时间 - WARNING - pylsp.plugins._resolvers - Something went wrong when resolving label for <Completion: mpl_toolkits>: 'NoneType' object has no attribute 'is_module'
2022-12-27 19:33:28,988 中国标准时间 - WARNING - pylsp.plugins._resolvers - Something went wrong when resolving label for <Completion: man>: 'NoneType' object has no attribute 'is_module'
2022-12-27 19:33:47,805 中国标准时间 - WARNING - pylsp.config.config - Failed to load hook pylsp_lint: 'utf-8' codec can't decode byte 0xb5 in position 3: invalid start byte
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\pylsp\config\config.py", line 33, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_manager.py", line 327, in traced_hookexec
    return outcome.get_result()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_result.py", line 33, in from_call
    result = func()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_manager.py", line 324, in <lambda>
    lambda: oldcall(hook_name, hook_impls, kwargs, firstresult)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\ProgramData\Anaconda3\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pylsp_mypy\plugin.py", line 223, in pylsp_lint
    report = completed_process.stdout.decode()

Installed python-lsp-server extensions:

python-lsp-server[rope,yapf]
pyls-isort                       0.2.2
pyls-memestra                    0.0.16
pyls-spyder                      0.4.0
pylsp-mypy                       0.6.4
pylsp-rope                       0.1.10

Platform: Jupyter 3.5.2 ,python 3.10 on Windows 11

Shabbyshambles commented 1 year ago

Somewhere in your setup things got encoded in ascii looks like. You can try to use something like sys.setdefaultencoding('utf-8'), but I can't vouch for the solution I just saw it on a few Stack Overflow posts and it got upvoted.

ccordoba12 commented 1 year ago

Hey @KagaJiankui, this is a bug in our Mypy plugin, so I'm going to move this issue to its repo.

ccordoba12 commented 1 year ago

@Richardk2n, could you take a look at this one? Thanks!

Richardk2n commented 1 year ago

Decode probably defaults to utf-8 regardless of the encoding the system is set to. Just to confirm: Do you know which encoding your os is set to?

ccordoba12 commented 1 year ago

@Richardk2n, it could be this one, according to a quick googling:

https://www.wikiwand.com/en/Code_page_936_(Microsoft_Windows)

KagaJiankui commented 1 year ago

the pylsp installation on my computer throws this exception both under system-wide CP936(GBK encoding) and UTF-8 encoding(I used system-wide UTF-8 encoding previously)

Richardk2n commented 1 year ago

Can you check if current master works for you?

zyxir commented 1 year ago

My OS uses cp936 encoding (simplified Chinese) by default too. I am using pylsp with Emacs (Eglot).

Every time the buffer (an open file) is saved, the "'utf-8' codec can't decode byte 0xb5 in position 3" error disappears, and mypy works as expected. But when I try to edit the buffer, the error appear again and mypy stopped functioning. I have to save the buffer to make mypy do type checking for me.

It seems that pylsp-mypy only work on saved files, when the OS encoding is not UTF-8.

This problem does not appear on my Linux PC, which uses UTF-8 by default.

Switching to the latest commit does not fix this problem for me.

Richardk2n commented 1 year ago

@zyxir This is an interesting fact. Can you give me the full error (including the line number) for the latest version? If it only happens in live mode it might be related to how the tmp file is saved (and can be avoided for the time being by disabling live mode).

zyxir commented 1 year ago

@Richardk2n Currently I don't have access to the Windows PC at home but I will do it when I can. However, it is not easy to produce the full error, as the error is only shown temporarily when the buffer is not saved, and disappears as long as the buffer is saved, so I cannot reproduce it with a saved file. However I can create a short test file to reproduce it, and copy the file as well as the error message here.