larksuite / larksuite/oapi-sdk-python

DeprecationWarning on Python 3.12+: bundled well_known_types.py uses deprecated datetime.datetime.utcfromtimestamp

Aperta Adatta ai principianti
#145 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
559
Fork
102
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Description

lark_oapi bundles a vendored copy of protobuf's well_known_types.py under lark_oapi/ws/pb/google/protobuf/internal/. That vendored file still calls datetime.datetime.utcfromtimestamp(0), which is deprecated in Python 3.12+ and emits a DeprecationWarning on Python 3.13/3.14:

.../lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py:91: DeprecationWarning:
  datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version.
  Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
  _EPOCH_DATETIME_NAIVE = datetime.datetime.utcfromtimestamp(0)

Upstream google.protobuf already replaced this call (it now uses datetime.fromtimestamp(0, datetime.timezone.utc)), but the copy pinned inside lark_oapi has not been regenerated, so users on Python 3.12+ still hit the warning whenever the WebSocket (lark_oapi.ws) protobuf code is imported.

Reproduction

Environment:

  • Python: 3.14.6
  • lark-oapi: 1.7.1 (installed from PyPI; also reproducible against v2_main at the path below)
  • OS: macOS

Steps:

python -W error::DeprecationWarning -c "import lark_oapi.ws.pb.google.protobuf.internal.well_known_types"

Expected: import succeeds with no warning.

Actual:

.../lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py:91: DeprecationWarning:
  datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version.
  Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).

Location

lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py:91 (still present on v2_main):

_EPOCH_DATETIME_NAIVE = datetime.datetime.utcfromtimestamp(0)
_EPOCH_DATETIME_AWARE = datetime.datetime.fromtimestamp(
    0, tz=datetime.timezone.utc)

The _AWARE variant right below already uses the timezone-aware form; only the _NAIVE line needs to change.

Suggested fix

utcfromtimestamp cannot be made timezone-aware directly; replace it with the equivalent naive UTC datetime constructed from the already-correct aware value, e.g.:

_EPOCH_DATETIME_NAIVE = datetime.datetime.fromtimestamp(
    0, tz=datetime.timezone.utc).replace(tzinfo=None)

This preserves the original "naive UTC" semantics that callers of _EPOCH_DATETIME_NAIVE rely on (used at line 251: return _EPOCH_DATETIME_NAIVE + delta) while removing the deprecated API call.

Alternatively, regenerate the vendored protobuf stubs from a current googleapis/python-protobuf release so the whole bundled copy tracks upstream fixes.

Impact

  • Functional impact: none (warning only; behavior is unchanged).
  • DX impact: noisy logs, fails test runs that elevate DeprecationWarning to errors, and will turn into a hard AttributeError once Python removes the API (currently scheduled for a future 3.x release).

Workaround

Users can suppress the warning locally, e.g.:

import warnings
warnings.filterwarnings(
    "ignore",
    message="datetime.datetime.utcfromtimestamp.*",
    category=DeprecationWarning,
)

—but the bundled protobuf code should be updated at the source.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py:91 e confronta il valore dell’epoca naive con il valore timezone-aware immediatamente sotto. Mantieni la semantica UTC naive rimuovendo al contempo la chiamata deprecata. Esegui python -W error::DeprecationWarning -c "import lark_oapi.ws.pb.google.protobuf.internal.well_known_types"; il lavoro è completato quando l’importazione riesce senza avvisi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
1/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
88/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.