“升级 Python 3.15 后,报错信息会不会变、日志监控和测试里的正则会不会失效” 是升级前最容易被忽略的问题。这篇在本机用 3.14.9 和 3.15.0 对 110 个常见的出错小程序和 10 个常见警告逐字比较,结果是绝大部分没有变,变了的集中在两处。
按类型(变化数 / 总数):NameError 0/4,AttributeError 3/8,UnboundLocalError 0/1,TypeError 3/25,KeyError 0/2,IndexError 0/2,ValueError 0/12,ZeroDivisionError 0/1,OverflowError 0/1,RecursionError 0/1,StopIteration 0/1,RuntimeError 0/1,AssertionError 0/1,ModuleNotFoundError 0/1,ImportError 0/2,循环导入 0/1,SyntaxError 0/31,UnicodeDecodeError 0/1,UnicodeEncodeError 0/1,FileNotFoundError 0/1,json.JSONDecodeError 0/1,OSError 0/1,IsADirectoryError 0/1,PermissionError 0/1,subprocess 0/1,ExceptionGroup 0/1,异常带 0/1,raise 0/1,生成器 0/1,match 0/1,asyncio 0/1,TimeoutError 0/1 没有变化的有 104 个,其中 SyntaxError 31 个、TypeError 22 个、ValueError 12 个 逐字比较(不做任何归一化)不同的有 14 个——多出来的 8 个只是标准库里的路径或行号
110 个出错小程序逐字比较:6 个文字变了,104 个没变,变化集中在 AttributeError 和 % 格式化的 TypeError。
AttributeError 拼错方法:
3.14: AttributeError: 'list' object has no attribute 'appnd'. Did you mean: 'append'?
3.15: AttributeError: 'list' object has no attribute 'appnd'. Did you mean '.append' instead of '.appnd'?
AttributeError 模块与本地文件同名:
3.14: AttributeError: module 'random' has no attribute 'randit'. Did you mean: 'randint'?
3.15: AttributeError: module 'random' has no attribute 'randit'. Did you mean '.randint' instead of '.randit'?
TypeError %d 格式化字符串:
3.14: TypeError: %d format: a real number is required, not str
3.15: TypeError: format argument: %d requires a real number, not str
TypeError %s 参数不够:
3.14: TypeError: not enough arguments for format string
3.15: TypeError: not enough arguments for format string (got 1)
TypeError %x 格式化字符串:
3.14: TypeError: %x format: an integer is required, not str
3.15: TypeError: format argument: %x requires an integer, not str
AttributeError 字符串没有 decode:
3.14: AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?
3.15: AttributeError: 'str' object has no attribute 'decode'. Did you mean '.encode' instead of '.decode'?AttributeError 的拼写建议换格式;% 格式化的 TypeError 文字重写。
NameError 拼错变量: NameError: name 'mth' is not defined. Did you mean: 'math'? SyntaxError print 语句: SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? NameError 局部变量拼错: NameError: name 'cuont' is not defined. Did you mean: 'count'? NameError self 忘了写: NameError: name 'value' is not defined. Did you mean: 'False'? 共 4 个
NameError 的变量名建议和 print 语句的提示逐字相同。
SyntaxWarning 无效转义序列:两个版本相同:SyntaxWarning: "\d" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\d"? A raw string is also an option.
SyntaxWarning is 比较字面量:两个版本相同:SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
SyntaxWarning 断言元组:两个版本相同:SyntaxWarning: assertion is always true, perhaps remove parentheses?
SyntaxWarning finally 里 return:两个版本相同:SyntaxWarning: 'return' in a 'finally' block
SyntaxWarning 调用整数:两个版本相同:SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
DeprecationWarning datetime.utcnow:两个版本相同:DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
RuntimeWarning 协程没有 await:两个版本相同:RuntimeWarning: coroutine 'f' was never awaited
ResourceWarning 没关的文件:两个版本相同:ResourceWarning: unclosed file <_io.TextIOWrapper name='a.py' mode='r' encoding='utf-8'>
FutureWarning 正则嵌套集合:两个版本相同:FutureWarning: Possible nested set at position 1
DeprecationWarning 元组/ByteString:
3.14: (没有警告)
3.15: DeprecationWarning: 'typing.ByteString' and 'collections.abc.ByteString' are deprecated and slated for removal in Python 3.1710 个常见警告只有 typing.ByteString 新增了 DeprecationWarning。
Python 3.14(退出码 0):
str(e) = 'list' object has no attribute 'appnd'
traceback.format_exception_only -> AttributeError: 'list' object has no attribute 'appnd'. Did you mean: 'append'?
正则 Did you mean: '(\w+)'\? 匹配 format_exception_only 的结果 -> True
正则 has no attribute '(\w+)' 匹配 -> True
e.name = appnd | e.obj 的类型 = list
Python 3.15(退出码 0):
str(e) = 'list' object has no attribute 'appnd'
traceback.format_exception_only -> AttributeError: 'list' object has no attribute 'appnd'. Did you mean '.append' instead of '.appnd'?
正则 Did you mean: '(\w+)'\? 匹配 format_exception_only 的结果 -> False
正则 has no attribute '(\w+)' 匹配 -> True
e.name = appnd | e.obj 的类型 = list用正则抓 Did you mean: '…'? 的日志监控和测试在 3.15 上抓不到;用 e.name 比解析文字稳。