question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Walrus operator support

See original GitHub issue

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]'}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
tomyuncommented, Mar 17, 2019
1reaction
tirkarthicommented, Apr 6, 2019

The upstream issue was fixed and the fix is available with Python 3.8 alpha 3. Verified the same on master. I think this can be closed.

(foo-venv) ⋊> ~/cpython on master ⨯ ipython                                                  18:59:03
Python 3.8.0a3+ (heads/master:2ea8099523, Apr  6 2019, 18:56:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: items = [{'name': 'foo'}, {}]

In [2]: for item in items:
   ...:     if name := item.get('name'):
   ...:         print(name)
   ...:
foo
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Walrus Operator: Python 3.8 Assignment Expressions
In this tutorial, you'll learn about assignment expressions and the walrus operator. The biggest change in Python 3.8 was the inclusion of the...
Read more >
What's New In Python 3.8 — Python 3.11.1 documentation
It is affectionately known as “the walrus operator” due to its ... To support both 3.8 and older, try pkg-config python-X.Y-embed --libs first...
Read more >
The Walrus Operator in Python
The walrus operator, introduced in Python 3.8, offers a way to accomplish two tasks at once: assigning a value to a variable, and...
Read more >
Python's walrus operator
Python's "walrus operator" is used for assignment expressions. Assignment expressions are a way of embedding an assignment statement inside another line of ...
Read more >
You Should Be Using Python's Walrus Operator - Here's Why
The assignment operator - or walrus operator as we all know it - is a ... then it can help you make your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found