我不知道是什麼樣的小號wece分析工具,所以我只能提出一個建議。然而,評論太長,所以我寫了一個概念驗證腳本。
這個想法是用Python的tokenize
模塊來看源代碼,該模塊從Python表達式生成令牌。如果格式良好的Python代碼包含隱式連續的字符串文字,則它將顯示爲STRING
標記,然後是NL
。
例如,我們使用以下源文件source.py
作爲測試用例。
x = ("a"
"b" # some trailing spaces
# Coment line
"c"
""
# The following is an explicit continuation
"d" \
"e")
上的文件運行命令python check.py < source.py
產生:
1:8: implicit continuation:
x = ("a"
~~~^
2:35: implicit continuation:
"b" # some trailing spaces
~~~^
4:3: implicit continuation:
"c"
~~~^
5:2: implicit continuation:
""
^
程序,check.py
,僅僅是一個驗證的概念,它不檢查語法錯誤或其它邊緣情況:
import sys
import tokenize
LOOKNEXT = False
tok_gen = tokenize.generate_tokens(sys.stdin.readline)
for tok, tok_str, start, end, line_text in tok_gen:
if tok == tokenize.STRING:
LOOKNEXT = True
continue
if LOOKNEXT and (tok == tokenize.NL):
warn_header = "%d:%d: implicit continuation: " % start
print >> sys.stderr, warn_header
print >> sys.stderr, line_text
indents = start[1] - 3
if indents >= 0:
print >> sys.stderr, "%s~~~^" % (" " * indents)
else:
print >> sys.stderr, "%s^" % (" " * start[1])
LOOKNEXT = False
希望這個想法可能會幫助您擴展您的lint工具或IDE以達到您的目的。
''foo「」bar「'foobar''也是如此。這是一個錯誤嗎?爲什麼? – 2016-11-09 09:10:17
嗯是的,我想我也想爲這種情況下的皮棉錯誤。它創造了比它值得的更多的麻煩。另請參閱http://legacy.python.org/dev/peps/pep-3126/#concerns – Moberg
我不知道這是否有任何工具?即使這樣做,爲什麼你需要這個?這是Python的行爲,你應該牢記這一點。總會有*什麼「IF」*?如果我寫了2代替3,該怎麼辦?你需要工具來告訴你這個嗎?你在問題中提到的關切是一樣的。 –