Python3.10: Improved error message
この属性じゃない?と教えてくれる
code:python
>> import datetime
>> datetime.datetim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'datetime' has no attribute 'datetim'. Did you mean: 'datetime'?
=は間違いではないかと教えてくれる
code:python
>> if x = 2: ...
File "<stdin>", line 1
if x = 2: ...
^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
コロンがないんじゃないと教えてくれる
code: python
>> if x < 10
File "<stdin>", line 1
if x < 10
^
SyntaxError: expected ':'
Pythonファイル(モジュール)で文法エラー。辞書の{}が閉じていない
code: python
names_to_id = {
"hoge": 1,
"fuga": 2,
"piyo": 3,
"mofu": 4
def main():
pass
code: shell
# python demo310.py
File "/docs/demo310.py", line 1
names_to_id = {
^
SyntaxError: '{' was never closed
never closed と教えてくれる
code: shell
$ python3.9 demo310.py # わかりやすくなったことの比較のため
File "/.../demo310.py", line 8
def main():
^
SyntaxError: invalid syntax
3.9以前はカッコが閉じていないことに気づきにくい
修正
code: python
names_to_id = {
"hoge": 1,
"fuga": 2,
"piyo": 3,
"mofu": 4
"hogera": 5
}
def main():
pass
code: shell
# python demo310.py
File "/docs/demo310.py", line 5
"mofu": 4
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
辞書のカンマ忘れも教えてくれる
code: shell
$ python3.9 demo310.py # わかりやすくなったことの比較のため
File "/.../demo310.py", line 6
"hogera": 5
^
SyntaxError: invalid syntax
詳しくは https://docs.python.org/3.10/whatsnew/3.10.html#better-error-messages