Skip to content

Commit 9de0c68

Browse files
committed
[requests] Add missing attributes and __init__ to JSONDecodeError stub
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 #15167
1 parent 81a6d24 commit 9de0c68

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

stubs/requests/requests/exceptions.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ class RequestException(OSError):
1414
) -> None: ...
1515

1616
class InvalidJSONError(RequestException): ...
17-
class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError): ...
17+
18+
class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):
19+
# requests.JSONDecodeError.__init__ explicitly calls CompatJSONDecodeError.__init__(msg, doc, pos),
20+
# so these attributes from json.JSONDecodeError are always present on instances.
21+
# Re-declared here to help type checkers resolve them despite the complex MRO.
22+
msg: str
23+
doc: str
24+
pos: int
25+
lineno: int
26+
colno: int
27+
def __init__(self, msg: str, doc: str, pos: int) -> None: ...
1828

1929
class HTTPError(RequestException):
2030
request: Request | PreparedRequest | Any

0 commit comments

Comments
 (0)