neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.47k stars 118 forks source link

Python 3.8: TypeError: 'type' object is not subscriptable #560

Closed gptlang closed 5 months ago

gptlang commented 5 months ago
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/pynvim/plugin/host.py", line 193, in _load
    module = _handle_import(directory, name)
  File "/usr/local/lib/python3.8/dist-packages/pynvim/plugin/host.py", line 40, in _handle_import
    return importlib.import_module(name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
TypeError: 'type' object is not subscriptable

Reference: https://github.com/gptlang/CopilotChat.nvim/issues/18

justinmk commented 5 months ago

https://github.com/neovim/pynvim/blob/5f989dfc47d98bba9f98e5ea17bfbe4c995cb0b0/pynvim/plugin/host.py#L40

That seems like a bug in importlib? Does it work on later python versions?

justinmk commented 5 months ago

Traceback (most recent call last): File "/usr/local/lib/python3.8/dist-packages/pynvim/plugin/host.py", line 193, in _load module = _handle_import(directory, name)

That seems like an out of date package. Maybe there are changes on master that weren't release yet. Here's line 193:

https://github.com/neovim/pynvim/blob/5f989dfc47d98bba9f98e5ea17bfbe4c995cb0b0/pynvim/plugin/host.py#L193

gptlang commented 5 months ago

That seems like a bug in importlib? Does it work on later python versions?

Yes. Works on later versions. I'm pretty sure importlib is standard library though; it shouldn't be out of date

justinmk commented 5 months ago

I mean that your pynvim source is out of date compared to pynvim master (can you try that)?

acheong08 commented 5 months ago

pip install git+https://github.com/neovim/pynvim?

CC @Cassius0924

Edit: oops, wrong account

Cassius0924 commented 5 months ago

git install git+https://github.com/neovim/pynvim?

CC @Cassius0924

Edit: oops, wrong account

It seems like there's a small mistake in your command. It's pip install not git install.

I used pip install git+https://github.com/neovim/pynvim to upgrade the pynvim version to 0.5.0.dev0, but there are still errors, but the error line has changed from line 193 to line 187

Encountered TypeError loading plugin at /root/.local/share/lunarvim/site/pack/lazy/opt/CopilotChat.nvim/rplugin/python3/utilities.py: 'type' object is not subscriptable
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/pynvim/plugin/host.py", line 187, in _load
    module = _handle_import(directory, name)
  File "/usr/local/lib/python3.8/dist-packages/pynvim/plugin/host.py", line 40, in _handle_import
    return importlib.import_module(name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
TypeError: 'type' object is not subscriptable

remote/host: python3 host registered plugins []
remote/host: generated rplugin manifest: /root/.local/share/lvim/rplugin.vim
acheong08 commented 5 months ago

It seems like there's a small mistake in your command. It's pip install not git install.

Yup. Typo

Could you try to reproduce this with a minimal Python file by importing importlib and calling it?

Cassius0924 commented 5 months ago

Like below?

I wrote a Python file:

import importlib
import sys

sys.path.append(
    "/root/.local/share/lunarvim/site/pack/lazy/opt/CopilotChat.nvim/rplugin/python3"
)

def test_import(name):
    importlib.import_module(name)

test_import("copilot")

Still getting error after running:

Traceback (most recent call last):
  File "t_1.py", line 17, in <module>
    test_import("copilot")
  File "t_1.py", line 11, in test_import
    importlib.import_module(name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/root/.local/share/lunarvim/site/pack/lazy/opt/CopilotChat.nvim/rplugin/python3/copilot.py", line 10, in <module>
    import utilities
  File "/root/.local/share/lunarvim/site/pack/lazy/opt/CopilotChat.nvim/rplugin/python3/utilities.py", line 13, in <module>
    chat_history: list[typings.Message],
TypeError: 'type' object is not subscriptable
Cassius0924 commented 5 months ago

This error seems not to be from pynvim, but from CopilotChat. I tried replacing list with List and it worked normally.

gptlang commented 5 months ago

Ah I see. I suppose I got confused by the error output

wookayin commented 5 months ago

Yes, the error was coming from the rplugin code, but the error message printed in Nvim can be confusing. We can improve error messages to clearly indicate that the SyntaxError/RuntimeError originates from the remote plugin.