bigywater.tech
新版本特性 · 7

Python 3.15 的报错信息变了吗:110 个出错小程序逐字比较,只有 AttributeError 的 Did you mean 和 % 格式化的 TypeError 变了

“升级 Python 3.15 后,报错信息会不会变、日志监控和测试里的正则会不会失效” 是升级前最容易被忽略的问题。这篇在本机用 3.14.9 和 3.15.0 对 110 个常见的出错小程序和 10 个常见警告逐字比较,结果是绝大部分没有变,变了的集中在两处。

6 / 110
文字有变化的出错小程序数量;SyntaxError 31 个、ValueError 12 个全部相同
Did you mean '.append'
AttributeError 建议的新格式(3.14 是 Did you mean: 'append'?)
1 / 10
10 个常见警告里变化的只有 typing.ByteString 的 DeprecationWarning(新增)

总览

实测 · 本机执行输出
  按类型(变化数 / 总数):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.17

10 个常见警告只有 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 比解析文字稳。

分步演示

报错信息变化
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / Homebrew 的 Python 3.14.9 与 3.15.0(同一台机器上两个解释器并排跑同一份代码;两者都不是自由线程构建,也都没有启用 JIT);每个例子是临时目录里的独立小脚本,用子进程运行并取真实的 stdout / stderr(路径换成 );计时用 time.perf_counter,数字每次运行会有小幅抖动。每个出错小程序都是一个独立的小脚本,用子进程运行;NO_COLOR=1 保证没有颜色控制符;比较前把标准库内部的路径、行号和 帧的行号归一化(它们每个版本都不同,不算报错文字的变化)
    • 没有验证:这 110 个小程序之外的错误(C 扩展、第三方库、asyncio 内部的报错);终端颜色(NO_COLOR 关掉了,没有比较彩色 traceback);异常组、__notes__ 的显示在更复杂场景下的差别;交互式解释器(REPL)里的报错显示;Python 其他小版本的措辞;报错信息变化的原因和计划(只比较了结果)
    京ICP备2026067502号-1京公网安备11010502063487号