[BUG] `TypeError: 'ContentFile' object is not subscriptable` in mod_upload when creating GitHub issue
メンテナーはふだん 4 日以内に返信
評価
調査の方向性
Read mod_upload/controllers.py around lines 259–267 and the existing test in tests/test_upload/test_controllers.py around lines 80–83. Reproduce the failure with PyGithub’s ContentFile return type, then update the handling and test it for both supported encodings. Done when reporting a GitHub issue completes without a 500 error; pull request #1262 is already open.
索引モデルが issue の本文から書いたものです。
説明
In mod_upload/controllers.py, when a user completes an upload with the option to report a GitHub issue (form.report.data == 'y'), the platform attempts to fetch .github/ISSUE_TEMPLATE.md via PyGithub:
# mod_upload/controllers.py:259-264
response = repository.get_contents(".github/ISSUE_TEMPLATE.md")
encoded_data = response['content']
if response['encoding'] == 'base64':
data = base64.b64decode(encoded_data).decode()
Since the migration to PyGithub in commit 74ef5e2 (#774), repository.get_contents(...) returns an instance of github.ContentFile.ContentFile rather than a dictionary.
Because ContentFile does not implement __getitem__, indexing response['content'] raises an unhandled TypeError: 'ContentFile' object is not subscriptable, causing the HTTP request to abort with a 500 Internal Server Error.
In addition, if response.encoding != 'base64', data is left unassigned, resulting in an UnboundLocalError on line 267 (data.replace(...)).
Why Existing Unit Tests Did Not Catch This
In tests/test_upload/test_controllers.py (lines 80–83), the test mocks get_contents to return a plain Python dictionary:
mock_github.return_value.get_repo.return_value.get_contents = mock.MagicMock(return_value={
"content": base64.b64encode("test".encode("ascii")).decode("ascii"),
"encoding": 'base64'
})
Because the mock returned a dictionary instead of a ContentFile object, the unit test passes even though real execution against PyGithub fails.
Minimal Reproduction Script
Run the following against PyGithub (as installed from requirements.txt):
from unittest.mock import MagicMock
from github.ContentFile import ContentFile
# PyGithub's actual return type for get_contents()
cf = ContentFile(
requester=MagicMock(),
headers={},
attributes={"content": "dGVzdA==", "encoding": "base64"},
completed=True
)
# Line 260 of mod_upload/controllers.py:
encoded_data = cf['content']
Output:
Traceback (most recent call last):
File "repro.py", line 12, in <module>
encoded_data = cf['content']
~~^^^^^^^^^^^
TypeError: 'ContentFile' object is not subscriptable
Proposed Fix
PyGithub's ContentFile provides a decoded_content property that automatically handles decoding:
response = repository.get_contents(".github/ISSUE_TEMPLATE.md")
data = response.decoded_content.decode('utf-8')
I would be happy to submit a pull request with the fix and an updated unit test using a proper ContentFile mock if this looks good to maintainers.
- 主要言語
- Python
- スター
- 31
- フォーク
- 92
- 平均マージ
- 4日 9時間
- マージ済み PR(30日)
- 29
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
CCExtractor/sample-platform のほかの issue
-
[BUG] Unhandled AttributeError on empty database queries for GeneralData and CCExtractorVersion対応中かも @Aspirant200715 が 9 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
CCExtractor/sample-platform#1275 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
CCExtractor/sample-platform#1273 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
[BUG] Missing timeouts on 5 outbound HTTP calls outside mod_ci (follow-up to #943)対応中かも @devnishantt が 54 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 84/100
CCExtractor/sample-platform#1192 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
-
[Feature Request] Improve comparison algorithm to handle frame offsets intelligently (GSoC Qualification)再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
CCExtractor/sample-platform#1079 ·
メンテナーはふだん 4 日以内に返信
-
[BUG] never-worked baseline handling is not fully integrated into trusted CI reporting再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
CCExtractor/sample-platform#1057 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
CCExtractor/sample-platform の issue をすべて見る
似ている issue
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finite対応中かも @leninworld が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
DoubleML/doubleml-for-py#471 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
python-pillow/Pillow#10590 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
BUG: ZarrTrace fails in init_trace for models with datetime coordinates (object dtype coord array)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 2 日以内に返信