已棄用項目

Python 3.15 中待移除的項目

  • 引入系統 (import system):

    • 在模組上設定 __cached__ 而沒有設定 __spec__.cached 的做法已被棄用。在 Python 3.15 中,引入系統或標準函式庫將不再設定或考慮 __cached__。(gh-97879)

    • 在模組上設定 __package__ 而沒有設定 __spec__.parent 的做法已被棄用。在 Python 3.15 中,引入系統或標準函式庫將不再設定或考慮 __package__。(gh-97879)

  • ctypes

    • 自 Python 3.13 起,未記錄的 ctypes.SetPointerType() 函式已被棄用。

  • http.server

    • 過時且很少使用的 CGIHTTPRequestHandler 自 Python 3.13 起已被棄用。不存在直接的替代。任何東西都比 CGI 更好地將 Web 伺服器與請求處理程序介接起來。

    • 自 Python 3.13 起,python -m http.server 命令列介面的 --cgi 旗標已被棄用。

  • importlib

    • load_module() method:請改用 exec_module()

  • locale

  • pathlib

  • platform

    • 自 Python 3.13 起,java_ver() 已被棄用。此函式僅對 Jython 支援有用,具有令人困惑的 API,基本上未經測試。

  • sysconfig

  • threading

    • RLock() 在 Python 3.15 中將不接受任何引數。自 Python 3.14 起,傳遞任何引數的用法已被棄用,因為 Python 版本不允許任何引數,但 C 版本允許任意數量的位置或關鍵字引數,並忽略每個引數。

  • types

  • typing

    • 用於建立 NamedTuple 類別的未以文件記錄之關鍵字引數語法 (Point = NamedTuple("Point", x=int, y=int)) 已自 Python 3.13 棄用。請改用基於類別的語法或函式語法 (functional syntax)。

    • 當使用 TypedDict 的函式語法時,未傳遞值給 fields 參數 (TD = TypedDict("TD")) 或傳遞 None (TD = TypedDict("TD", None)) 的做法自 Python 3.13 起已被棄用。請使用 class TD(TypedDict): passTD = TypedDict("TD", {}) 來建立具有零個欄位的 TypedDict。

    • 自 Python 3.13 起,typing.no_type_check_decorator() 裝飾器函式已被棄用。在 typing 模組中使用了八年之後,它尚未得到任何主要型別檢查器的支援。

  • wave

  • zipimport

Python 3.16 中待移除的項目

Python 3.17 中待移除的項目

  • collections.abc

    • collections.abc.ByteString is scheduled for removal in Python 3.17.

      Use isinstance(obj, collections.abc.Buffer) to test if obj implements the buffer protocol at runtime. For use in type annotations, either use Buffer or a union that explicitly specifies the types your code supports (e.g., bytes | bytearray | memoryview).

      ByteString was originally intended to be an abstract class that would serve as a supertype of both bytes and bytearray. However, since the ABC never had any methods, knowing that an object was an instance of ByteString never actually told you anything useful about the object. Other common buffer types such as memoryview were also never understood as subtypes of ByteString (either at runtime or by static type checkers).

      See PEP 688 for more details. (Contributed by Shantanu Jain in gh-91896.)

  • typing

    • 在 Python 3.14 之前,舊式聯集是使用私有類別 typing._UnionGenericAlias 實作的。這個類別不再被需要,但為了向後相容性而保留,並計劃將在 Python 3.17 中移除。使用者應該改用文件中記錄的內省輔助函式,例如 typing.get_origin()typing.get_args(),或者依賴私有實作細節。

    • typing.ByteString, deprecated since Python 3.9, is scheduled for removal in Python 3.17.

      Use isinstance(obj, collections.abc.Buffer) to test if obj implements the buffer protocol at runtime. For use in type annotations, either use Buffer or a union that explicitly specifies the types your code supports (e.g., bytes | bytearray | memoryview).

      ByteString was originally intended to be an abstract class that would serve as a supertype of both bytes and bytearray. However, since the ABC never had any methods, knowing that an object was an instance of ByteString never actually told you anything useful about the object. Other common buffer types such as memoryview were also never understood as subtypes of ByteString (either at runtime or by static type checkers).

      See PEP 688 for more details. (Contributed by Shantanu Jain in gh-91896.)

Python 3.19 中待移除的項目

  • ctypes

    • 在非 Windows 平台上,透過設定 _pack_ 而沒有設定 _layout_ 來隱式地切換到與 MSVC 相容的結構佈局。

未來版本中的待移除項目

以下 API 將在未來被移除,雖然目前尚未安排移除日期。

  • argparse

    • 巢狀引數群組和巢狀互斥群組已被棄用。

    • 將未以文件記錄的關鍵字引數 prefix_chars 傳遞給 add_argument_group() 的做法現在已被棄用。

    • argparse.FileType 型別轉換器已被棄用。

  • builtins

    • bool(NotImplemented)

    • 產生器:throw(type, exc, tb)athrow(type, exc, tb) 簽名已被棄用:請改用 throw(exc)athrow(exc),為單引數簽名。

    • 目前 Python 接受數值字面值後面立即接關鍵字,例如 0in x1or x0if 1else 2。它讓表達式模糊且容易混淆,如 [0x1for x in y](可以解釋為 [0x1 for x in y][0x1f or x in y])。如果數值字面值後立即接 andelseforifinisor 之一的關鍵字,則會引發語法警告。在未來版本中,它將被更改為語法錯誤。(gh-87999)

    • __index__()__int__() 方法回傳非 int 型別的支援:這些方法將需要回傳 int 的嚴格子類別實例。

    • 回傳 float 嚴格子類別 __float__() 方法的支援:這些方法將需要回傳 float 的實例。

    • 回傳 complex 嚴格子類別 __complex__() 方法的支援:這些方法將需要回傳 complex 的實例。

    • int() 委派給 __trunc__() 方法。

    • complex() 建構子中將複數作為 realimag 引數傳遞現在已被棄用;它應該只作為單個位置引數傳遞。 (由 Serhiy Storchaka 於 gh-109218 貢獻。)

  • calendarcalendar.Januarycalendar.February 常數已被棄用並被 calendar.JANUARYcalendar.FEBRUARY 取代。 (由 Prince Roshan 於 gh-103636 貢獻。)

  • codecs:請改用 open() 而非 codecs.open()。(gh-133038)

  • codeobject.co_lnotab:請改用 codeobject.co_lines() 方法。

  • datetime

    • utcnow():請改用 datetime.datetime.now(tz=datetime.UTC)

    • utcfromtimestamp():請改用 datetime.datetime.fromtimestamp(timestamp, tz=datetime.UTC)

  • gettext:複數值必須是整數。

  • importlib

  • importlib.metadata

    • EntryPoints 元組介面。

    • 回傳值上的隱式 None

  • logging:自 Python 3.3 起,warn() 方法已被棄用,請改用 warning()

  • mailbox:已棄用 StringIO 輸入和文本模式,請改用 BytesIO 和二進位模式。

  • os:在多執行緒行程中呼叫 os.register_at_fork()

  • pydoc.ErrorDuringImportexc_info 參數的元組值已被棄用,請用例外實例。

  • re:現在對正規表示式中的數值群組參照和群組名稱用了更嚴格的規則。現在只有 ASCII 數碼序列被接受作為數值參照。位元組模式和替換字串中的群組名稱現在只能包含 ASCII 字母、數碼和底線。(由 Serhiy Storchaka 於 gh-91760 貢獻。)

  • sre_compilesre_constantssre_parse 模組。

  • shutilrmtree()onerror 參數在 Python 3.12 中已被棄用;請改用 onexc 參數。

  • ssl 選項和協定:

    • 不帶協定引數的 ssl.SSLContext 已被棄用。

    • ssl.SSLContextset_npn_protocols()selected_npn_protocol() 已被棄用:請改用 ALPN。

    • ssl.OP_NO_SSL* 選項

    • ssl.OP_NO_TLS* 選項

    • ssl.PROTOCOL_SSLv3

    • ssl.PROTOCOL_TLS

    • ssl.PROTOCOL_TLSv1

    • ssl.PROTOCOL_TLSv1_1

    • ssl.PROTOCOL_TLSv1_2

    • ssl.TLSVersion.SSLv3

    • ssl.TLSVersion.TLSv1

    • ssl.TLSVersion.TLSv1_1

  • threading 方法:

  • typing.Text (gh-92332)。

  • 內部類別 typing._UnionGenericAlias 不再用於實作 typing.Union。為了保持與此私有類別使用者的相容性,直到至少 Python 3.17 都將提供一個相容性 shim。(由 Jelle Zijlstra 於 gh-105499 貢獻。)

  • unittest.IsolatedAsyncioTestCase:從測試案例中回傳非 None 的值已被棄用。

  • urllib.parse 已棄用函式:請改用 urlparse()

    • splitattr()

    • splithost()

    • splitnport()

    • splitpasswd()

    • splitport()

    • splitquery()

    • splittag()

    • splittype()

    • splituser()

    • splitvalue()

    • to_bytes()

  • wsgirefSimpleHandler.stdout.write() 不應該進行部分寫入。

  • xml.etree.ElementTree:已棄用對 Element 的真值測試。在未來版本中,它將始終回傳 True。請改用明確的 len(elem)elem is not None 測試。

  • sys._clear_type_cache() 已被棄用:請改用 sys._clear_internal_caches()

C API 的棄用項目

Python 3.15 中待移除的項目

Python 3.18 中待移除的項目

未來版本中的待移除項目

下列 API 已被棄用並將會被移除,不過目前尚未訂定移除日期。