Is anyone using Walrus operator? Match statement?
Python 3.8 introduced Walrus operator.
Python 3.10 introduced Match statement.
I don’t build applications with Python. But I spend quite some time with Pandas and Python notebooks. I never found myself a need to use these new language features.
So I decided to figure out if the open source world has adopted these features at all. I looked at top 10 most downloaded packages on Pip https://pypistats.org/top
The plan was to get the source code of each package, parse each of the Python code file, and look for Match and NamedExpr (https://docs.python.org/3/library/ast.html#ast.NamedExpr represents Walrus operator)
I realized I had to find the Github page for each of them. How can I automate this? Perhaps I can simply download the source from Pip.
pip download boto3
you will get boto3-1.26.32-py3-none-any.whl
and a few other .whl
files. They look like boto3’s dependencies. I could inspect them as well. .whl
files are source code in a zip format.
Let’s get them all.
pip download boto3
pip download urllib3
pip download botocore
pip download requests
pip download idna
pip download charset-normalizer
pip download setuptools
pip download certifi
pip download typing-extensions
pip download google-api-core
We can parse the source code into AST (Abstract syntax tree)
from zipfile import ZipFile
def parseWhl(whlFile):
with ZipFile(whlFile) as z:
for name in z.namelist():
if name.endswith('.py'):
src = z.read(name)
res = parse(src)
yield res | {"name": name}
def parse(src):
parsed = ast.parse(src)
has_walrus = False
has_match = False
for node in ast.walk(parsed):
if isinstance(node, ast.Match):
has_match = True
elif isinstance(node, ast.NamedExpr):
has_walrus = True
if has_match and has_walrus:
break
return {"has_walrus": has_walrus, "has_match": has_match}
I prefer Pandas for better reporting and sometimes parallelization if the dataset is getting larger.
The conclusion is that none of these packages have used Walrus operator or Match statement at all.
On a site note, I was inspired by Python’s principle "There should be one-- and preferably only one --obvious way to do it.” https://peps.python.org/pep-0020/ However the recent versions seem to have broken it.