Walrus operator support
I wanted to try out the new assignment expressions aka walrus operator in Python 3.8.
It all worked fine in plain Python 3.8:
$ python
Python 3.8.0a1 (default, Feb 19 2019, 11:26:51)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sample_data = [
... {"userId": 1, "id": 1, "title": "delectus aut autem", "completed": False},
... {"userId": 1, "id": 2, "title": "quis ut nam facilis", "completed": False},
... {"userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": False},
... {"userId": 1, "id": 4, "title": "et porro tempora", "completed": True},
... {"userId": 1, "id": 4, "title": None, "completed": True},
... ]
>>>
>>> print("With Python 3.8 Walrus Operator:")
With Python 3.8 Walrus Operator:
>>> for entry in sample_data:
... if title := entry.get("title"):
... print(f'Found title: "{title}"')
...
Found title: "delectus aut autem"
Found title: "quis ut nam facilis"
Found title: "fugiat veniam minus"
Found title: "et porro tempora"
But failed miserably in IPython:
$ ipython
Python 3.8.0a1 (default, Feb 19 2019, 11:26:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0.dev -- An enhanced Interactive Python. Type '?' for help.
In [1]: sample_data = [
...: {"userId": 1, "id": 1, "title": "delectus aut autem", "completed": False},
...: {"userId": 1, "id": 2, "title": "quis ut nam facilis", "completed": False},
...: {"userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": False},
...: {"userId": 1, "id": 4, "title": "et porro tempora", "completed": True},
...: {"userId": 1, "id": 4, "title": None, "completed": True},
...: ]
...:
...: print("With Python 3.8 Walrus Operator:")
...: for entry in sample_data:
...: if title := entry.get("title"):
...: print(f'Found title: "{title}"')
...:
With Python 3.8 Walrus Operator:
---------------------------------------------------------------------------
SystemError Traceback (most recent call last)
/usr/local/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
131
132 def __call__(self, source, filename, symbol):
--> 133 codeob = compile(source, filename, symbol, self.flags, 1)
134 for feature in _features:
135 if codeob.co_flags & feature.compiler_flag:
SystemError: unexpected expression
I was surpised to see that happen, as IPython 7.3.0+ should have Python 3.8 support.
$ python -c "import IPython; print(IPython.sys_info())"
{'commit_hash': '19b47fa',
'commit_source': 'repository',
'default_encoding': 'utf-8',
'ipython_path': '/home/piotr/Personal/ipython/IPython',
'ipython_version': '7.4.0.dev',
'os_name': 'posix',
'platform': 'Linux-4.15.0-45-generic-x86_64-with-glibc2.17',
'sys_executable': '/home/piotr/.virtualenvs/ipython-KlOhEFsK/bin/python',
'sys_platform': 'linux',
'sys_version': '3.8.0a1 (default, Feb 19 2019, 11:26:51) \n'
'[GCC 5.4.0 20160609]'}