Skip to content

locale.getdefaultlocale() 在 Python 3.11+ 已废弃,将在 Python 3.15 中被移除 #18

Description

@archerhu

问题描述

在导入 yop_python_sdk 时,会触发 DeprecationWarning,原因是使用了 locale.getdefaultlocale()。该函数自 Python 3.11 起已被标记为废弃,并将在 Python 3.15 中彻底移除。

文件位置: yop_python_sdk/client/yopclient.py,第 21 行

locale_info = locale.getdefaultlocale()   # ← 已废弃
locale_lang = locale_info[0]
if locale_lang is None:
    locale_lang = 'zh-CN'

触发的警告:

DeprecationWarning: 'locale.getdefaultlocale' is deprecated and slated for removal
in Python 3.15. Use setlocale(), getencoding() and getlocale() instead.

影响

该警告在 import 阶段就会触发,早于任何 SDK 调用。
在测试环境中使用 -W error::DeprecationWarning 运行时,会导致 ImportError,直接中断整个测试套件。

环境信息

  • yop-python-sdk 版本:4.3.0
  • Python 版本:3.11.x

建议修复方案

locale.getdefaultlocale() 替换为 locale.getlocale()

try:
    locale_lang = locale.getlocale()[0] or 'zh-CN'
except (ValueError, locale.Error):
    locale_lang = 'zh-CN'

locale.getlocale() 返回相同的 (language_code, encoding) 元组,自 Python 2 起就已支持。加上 try/except 可兼容 locale 未设置或返回无效值的边缘情况。

参考链接

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions