Skip to content

[requests] Add missing attributes and __init__ to JSONDecodeError stub#15544

Closed
sedat4ras wants to merge 1 commit intopython:mainfrom
sedat4ras:fix/requests-jsondecode-error-attrs
Closed

[requests] Add missing attributes and __init__ to JSONDecodeError stub#15544
sedat4ras wants to merge 1 commit intopython:mainfrom
sedat4ras:fix/requests-jsondecode-error-attrs

Conversation

@sedat4ras
Copy link

Fixes #15167

Problem

requests.exceptions.JSONDecodeError inherits from both InvalidJSONError (which inherits from RequestException) and json.JSONDecodeError. When catching it in user code, accessing attributes like exc.doc causes a mypy error:

error: "JSONDecodeError" has no attribute "doc"  [attr-defined]

Root Cause

The runtime requests.JSONDecodeError.__init__ explicitly calls CompatJSONDecodeError.__init__(self, *args) first, which sets the json.JSONDecodeError instance attributes (doc, pos, lineno, colno, msg). However, due to the complex MRO (where RequestException.__init__ appears before json.JSONDecodeError in the chain), type checkers may not correctly resolve these inherited attributes.

Fix

Explicitly re-declare the json.JSONDecodeError attributes on requests.JSONDecodeError and add the correct __init__ signature matching the runtime behavior (msg: str, doc: str, pos: int), consistent with how requests calls it:

raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

After this fix:

import requests
from requests.exceptions import JSONDecodeError

try:
    requests.get("https://www.google.com").json()
except JSONDecodeError as exc:
    d = exc.doc  # no longer a mypy error
    print("Failed to decode json. Got:", d[:10])

requests.exceptions.JSONDecodeError inherits from both InvalidJSONError
(via RequestException) and json.JSONDecodeError. Its custom __init__
explicitly calls CompatJSONDecodeError.__init__(msg, doc, pos) first,
which sets the json.JSONDecodeError instance attributes (doc, pos,
lineno, colno, msg).

However, due to the complex MRO, type checkers may not resolve these
inherited attributes correctly on the requests.JSONDecodeError class.
Explicitly re-declaring them ensures tools like mypy correctly recognize
attributes such as exc.doc when catching requests.JSONDecodeError.

Also adds the proper __init__ signature matching the runtime behavior.

Fixes python#15167
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
Copy link
Collaborator

srittau commented Mar 23, 2026

Thanks, but this was already fixed in #15167 by deriving requests.JSONDecodeError from json.JSONDecodeError. We just forgot to close the corresponding issue.

@srittau srittau closed this Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type for requests.exceptions.JSONDecodeError is missing attributes

2 participants