[requests] Add missing attributes and __init__ to JSONDecodeError stub#15544
Closed
sedat4ras wants to merge 1 commit intopython:mainfrom
Closed
[requests] Add missing attributes and __init__ to JSONDecodeError stub#15544sedat4ras wants to merge 1 commit intopython:mainfrom
sedat4ras wants to merge 1 commit intopython:mainfrom
Conversation
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
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Collaborator
|
Thanks, but this was already fixed in #15167 by deriving |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15167
Problem
requests.exceptions.JSONDecodeErrorinherits from bothInvalidJSONError(which inherits fromRequestException) andjson.JSONDecodeError. When catching it in user code, accessing attributes likeexc.doccauses a mypy error:Root Cause
The runtime
requests.JSONDecodeError.__init__explicitly callsCompatJSONDecodeError.__init__(self, *args)first, which sets thejson.JSONDecodeErrorinstance attributes (doc,pos,lineno,colno,msg). However, due to the complex MRO (whereRequestException.__init__appears beforejson.JSONDecodeErrorin the chain), type checkers may not correctly resolve these inherited attributes.Fix
Explicitly re-declare the
json.JSONDecodeErrorattributes onrequests.JSONDecodeErrorand add the correct__init__signature matching the runtime behavior (msg: str, doc: str, pos: int), consistent with how requests calls it:After this fix: