hanjinliu / magic-class

Generate multifunctional and macro recordable GUIs from Python classes
https://hanjinliu.github.io/magic-class/
BSD 3-Clause "New" or "Revised" License
40 stars 5 forks source link

`TypeError: <class 'magicclass._gui._base.MagicTemplate'> is not a generic class` in Python 3.8 #104

Closed multimeric closed 1 year ago

multimeric commented 1 year ago

There seems to be a bug that only appears in Python 3.8. You can reproduce it as follows:

# Get Python 3.8
conda create -n py38 'python<3.9'
conda activate py38

# Install deps
pip install magicgui[pyqt5] magicclass

# Trigger bug
python3.8 -c 'import magicclass'

Result:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/magicclass/__init__.py", line 3, in <module>
    from .core import (
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/magicclass/core.py", line 9, in <module>
    from magicclass._gui.class_gui import (
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/magicclass/_gui/__init__.py", line 1, in <module>
    from ._base import BaseGui, MagicTemplate
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/magicclass/_gui/_base.py", line 814, in <module>
    class BaseGui(MagicTemplate[_W]):
  File "/opt/anaconda3/envs/py38/lib/python3.8/typing.py", line 261, in inner
    return func(*args, **kwds)
  File "/opt/anaconda3/envs/py38/lib/python3.8/typing.py", line 897, in __class_getitem__
    _check_generic(cls, params)
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/typing_extensions.py", line 155, in _check_generic
    raise TypeError(f"{cls} is not a generic class")
TypeError: <class 'magicclass._gui._base.MagicTemplate'> is not a generic class

I understand the error. It thinks that class BaseGui(MagicTemplate[_W]) is treating MagicTemplate like a generic class when instead it just defines __getitem__() for another reason. However, I have no idea why the Python version affects this.

hanjinliu commented 1 year ago

Hi @multimeric , thank you for sharing the bug. It is definitely my mistake adding a type variable to MagicTemplate, but I don't know why python>=3.9 did not treat it as an error... FYI, python 3.8 is no longer supported (following NEP29). I recommend you to use python>=3.9 to get the newer updates.

multimeric commented 1 year ago

I think people tend to follow the Python EOL timelines, and 3.8 is not yet end of life.

multimeric commented 1 year ago

Thanks for the fix. Any chance you could publish a new patch version so I could get Python 3.8 working again?

hanjinliu commented 1 year ago

Sure. I dropped 3.8 just recently so I think it'll not take much time.