Fix: json5/lib.py's load() function declared fp: IO, the bare... - #115
Open
M001N wants to merge 1 commit into
Open
Fix: json5/lib.py's load() function declared fp: IO, the bare...#115M001N wants to merge 1 commit into
fp: IO, the bare...#115M001N wants to merge 1 commit into
Conversation
- load()'s fp parameter is now typed Union[IO[str], IO[bytes]] instead of the bare, unparameterized IO, since load() supports both text-mode and binary-mode file objects (confirmed by tests/lib_test.py's test_encoding, which passes an io.BytesIO to json5.load()). - dump()'s fp parameter is typed IO[str] since it only ever writes str. - loads()/parse()'s s parameter is now typed Union[str, bytes] to match their existing runtime support for byte strings (they already decode bytes via an isinstance check), fixing a latent inconsistency that the load() fix would otherwise expose as a new mypy error. Fixes reportUnknownMemberType/partially-unknown-type complaints from pyright/mypy when calling json5.load(f). Type-annotation-only change; verified zero behavioral impact via full test suite and `python -m mypy json5`.
Owner
|
Seems reasonable, thanks for the fix! Question, though: I notice that the typeshed's hints for the library are actually a bit different. Do you think we should consider something closer to those? |
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.
Summary
Changed json5/lib.py: load()'s
fp: IO->fp: Union[IO[str], IO[bytes]](load() genuinely supports both text- and binary-mode file objects, confirmed by the BytesIO test). dump()'sfp: IO->fp: IO[str](dump() only ever writes str, via fp.write(dumps(...))). loads()'s and parse()'ss: str->s: Union[str, bytes]to make their signatures consistent with their existing documented/tested bytes support, and to keeppython -m mypyclean given load()'s now-typed fp.read() result flows into parse().Problem
dpranke/pyjson5 issue reference: #92
Root Cause
json5/lib.py's load() function declared
fp: IO, the bare unparameterized typing.IO generic, which strict type checkers (pyright, and mypy in stricter modes) flag as an incomplete/unknown type. The bareIO(notIO[str]) triggers reportUnknownMemberType-style diagnostics in downstream code calling json5.load(f). dump() had the identical bareIOpattern. Additionally, once fp is properly parameterized, fp.read() becomes a concretely-typed str/bytes value instead of Any, which surfaced a pre-existing latent inconsistency: loads()/parse() are typed as accepting onlys: streven though they already handle bytes at runtime (via anisinstance(s, bytes)decode step) — confirmed by tests/lib_test.py's test_encoding, which calls bothjson5.loads(b'...')andjson5.load(io.BytesIO(...)).Testing
PASS -
python -m mypy json5reports no issues (was previously not exercising this path meaningfully since bare IO/str were effectively Any-typed); full test suite (75 tests across tests/) passes unchanged, confirming zero behavioral impact.Related Issue
#92