Search code examples
pythonipythonpython-decorators

ipython doesn't allow creating of classmethods


I tried using ipython to create a class method with the class method decorator. When I press enter I get the following error: enter image description here

I tried using the same decorator in a normal python script and it worked. Why can't I do the same in Ipython?


Solution

  • Upgrade your ipython package to the latest version, e.g.

    $ python3 -m pip install -U ipython
    

    It works fine for ipython==8.1.0 (released Feb 25, 2022) or later:

    $ ipython
    Python 3.11.4 (main, Jun 20 2023, 16:52:35) [Clang 13.0.0 (clang-1300.0.29.30)]
    Type 'copyright', 'credits' or 'license' for more information
    IPython 8.1.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: class Test:
       ...:     @classmethod
       ...:     def test():
       ...:         pass
       ...:
    
    In [2]:
    

    I could reproduce the issue for the previous version of ipython, 8.0.1:

    $ ipython
    Python 3.11.4 (main, Jun 20 2023, 16:52:35) [Clang 13.0.0 (clang-1300.0.29.30)]
    Type 'copyright', 'credits' or 'license' for more information
    IPython 8.0.1 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: class Test:
       ...:     @classmethod
      Input In [1]
        @classmethod
                    ^
    SyntaxError: incomplete input