Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[FR] Improve Ability to detect if App has been Initialized

オープン
#701 コメント 2 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

まず firebase_admin/init.py を読み、特にリンク先の行付近にある initialize_app() と get_app() を確認します。提案されている is_initialized 関数と特定のエラーを使うアプローチを比較し、そのうえで maintainer が望んでいる公開動作を判断します。ユーザーが private な _apps 変数を調べたり、一般的な ValueError を解析したりせずに、要求した app が初期化済みかどうかを確実に検出できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

api: core type: feature request

Is your feature request related to a problem? Please describe.

Currently there is not a way offered by the library to check if firebase_admin.initialize_app(...)(source) has been called and an app has been initialized besides catching an exception. This can result in library users seeing the following exception:

ValueError: The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.
source

From the discussion in this Stackoverflow post, there are two main approaches library users have implemented:

1. use a try/except block on ValueError to get the app and initialize it if there is an exception

try:
    app = firebase_admin.get_app()
except ValueError as e:
    cred = credentials.Certificate(CREDENTIALS_FIREBASE_PATH)
    firebase_admin.initialize_app(cred)

source

Pros: simple

Cons: ValueError is a general error so theoretically the use does not know for sure the error is regarding initialization, so further inspection is needed to verify that the error is related to app initialization, e.g. a string check on "already exists".

2. check the "private" _apps variable

if not firebase_admin._apps:
    cred = credentials.Certificate('path/to/serviceAccountKey.json') 
    default_app = firebase_admin.initialize_app(cred)

source

Pros: not relying on try/except flow

Cons: accessing a "private" variable, as the library owners now if you change this variable it will cause breaking changes for many library users.

Describe the solution you'd like

Option 1: Implement a function like is_initialized(name=_DEFAULT_APP_NAME)

This could check _apps for the given name and return true / false for whether it is initialized.

Option 2: Raise a specific error

Implement a new error that extends ValueError for backwards compatibility and raise the error with this new type.

For example:

class AppInitializedError(ValueError):
    def __init__(self, message):            
        super().__init__(message)

This could then be used:

try:
    app = firebase_admin.get_app()
except AppInitializedError:
    cred = credentials.Certificate(CREDENTIALS_FIREBASE_PATH)
    firebase_admin.initialize_app(cred)

These implementations could be done in conjunction, however option 1 is safest as it only adds new behavior and changes no existing behavior. Furthermore there will still be those against using try / except as the "expected" way to check if the app is initialized as offered in option 2.

Describe alternatives you've considered

The alternatives are described in the stackoverflow post above (link) as well as in this issue.

主要言語
Python
スター
1.2k
フォーク
359
平均マージ
5日 6分
マージ済み PR(30日)
2

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

firebase/firebase-admin-python のほかの issue

firebase/firebase-admin-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。