You are not logged in.

#1 2019-12-01 20:08:46

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 16,861

[Solved]Python, flake8 and the Walrus Operator

For whatever indefensible reason, I happen to like inline assignment operators and therefore welcome my new pinniped overlord.

But, I thought flake8 version 3.7.9 had support for it, but it seems not.  See my example below.  Should this work? or is that error message in the flake8 output trying to tell me something?  Note that the example is a deliberately spartan example and is by no means robust.  The line that is being flagged is

if not (len(message := s.recv(100)) == EXPECT):
With the assignment operator (:=) just before the midpoint.

ewaller@odin/home/ewaller % cat example.py
#! /bin/env python

import socket
message = None
EXPECT = 58

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.201', 5025))
s.settimeout(0.2)

s.send(b'*IDN?\n')
if not (len(message := s.recv(100)) == EXPECT):
    print('message was incorrect length')
else:
    print('message was correct length')

print(f'{message=}')
print(len(message))
ewaller@odin/home/ewaller % flake8 example.py 
"pyflakes" failed during execution due to "'FlakesChecker' object has no attribute 'NAMEDEXPR'"
Run flake8 with greater verbosity to see more details
example.py:12:20: E203 whitespace before ':'
example.py:12:21: E231 missing whitespace after ':'
ewaller@odin/home/ewaller[1] % ./example.py
message was correct length
message=b'Siglent Technologies,SDS1204X-E,SDSMMDBX2R1864,7.1.6.1.33\n'
58
ewaller@odin/home/ewaller % flake8 --version
3.7.9 (mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.8.0 on Linux
ewaller@odin/home/ewaller % 

Did I miss something?

Last edited by ewaller (2019-12-01 23:27:15)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#2 2019-12-01 22:39:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,395
Website

Re: [Solved]Python, flake8 and the Walrus Operator

I have no experience with flake8, but it seems their pypi page lists only up to python 3.7 in the supported languages (left bar, near the bottom) and the walrus is a python 3.8 feature.  I confirmed that this list is updated as flake8 developes by going back to earlier versions of flake8 that did not list/support python 3.6 or 3.7 either.

(tagential fyi)

Last edited by Trilby (2019-12-01 22:40:25)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#3 2019-12-01 22:51:50

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 16,861

Re: [Solved]Python, flake8 and the Walrus Operator

It is a 3.8 feature.  Are you asserting that the pyflake release numbers are tied to the python version number?  That would make sense; and their documentation seems to indicate that.

But, then there are project such as this: https://pypi.org/project/flake8-walrus/ that is for those who have tastes that differ from mine.  That package appears to flag the walrus operator as disallowed.

As to the fyi, I had seen that and it provided me a nice chuckle.

Edit:   It does appear I may have been getting ahead of myself.  It looks like 3.8 features are in the code tree, but have not yet been in any release.   I think the flake8-walrus project is written against the working git repository in anticipation of upcoming releases.

Last edited by ewaller (2019-12-01 23:21:59)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2019-12-01 23:19:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,395
Website

Re: [Solved]Python, flake8 and the Walrus Operator

ewaller wrote:

It is a 3.8 feature.  Are you asserting that the pyflake release numbers are tied to the python version number?

No.  The version numbers might be tied, and I'd agree that that'd be handy, but my point would still hold even if this wasn't the case.  I believe the documentation page I pointed to lists the versions of the python language that it applies to, and that list only goes up to python 3.7 for the current version of flake8.

The flake8-walrus package would indeed cast doubt on this, however, that package may exist pre-emptively (the author assuming that flake8 may shortly include python 3.8 features including the walrus).  And the rationale for that package being just "lol", I'm not sure how much weight I'd give to it.

Another data point: there is a commit message in the flake8 git repo "Cleanup CI and add python 3.8 support" from 2019-11-03 which is more recent than the last tag for flake8 3.7.9 on 2019-10-28.

So while a bit circumstantial, I believe this all suggests the stable flake8 release does not yet support 3.8 (including the walrus) but the next release likely will.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#5 2019-12-01 23:23:14

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 16,861

Re: [Solved]Python, flake8 and the Walrus Operator

You and I were posting at the same time (see my edits) and it looks like we are in agreement.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB