Typecheck Json inner type#4332
Conversation
f995915 to
5f226d2
Compare
|
please review :) |
There was a problem hiding this comment.
Thanks for this.
Two questions:
- is this better than the status quo - I think "yes"
- is this suitable for a patch release since some legitimate usage will "break" (at least for type checkers) - I think probably not
I would suggest we defer this to V2.
We need to think a lot about serialisation and in particular round-trip validation which affects Json, but perhaps not this.
| my_json: Json[Dict[str, str]] = '{"hello": "world"}' # type: ignore | ||
| my_json_list: Json[List[str]] = '["hello", "world"]' # type: ignore |
There was a problem hiding this comment.
Humm, problem is this makes the output type compatibility better, but makes the input type compatibility worse.
Hence the "type ignore" here.
There was a problem hiding this comment.
Yes, indeed. I thought that it's alright since it's kind of consistent throughout pydantic: you can even see same type: ignore's above for other types.
There was a problem hiding this comment.
Yes, agreed, also presumably it allows bytes.
Hence why I said this is better than the status quo on balance.
|
I think it certainly not suitable for a patch release. I also don't have enough context about V2 release date. As for changing inner logic, I'd like to see something entirely different in terms of inner types support in V2. Currently I have a case where I need to decode and validate JWS, would be nice to be able to do something like: signed_payload: Annotated[PayloadModel, Field(decode=decode_jws_payload)This would allow for usages like T = TypeVar("T")
JWS = Annotated[T, Field(decode=decode_jws_payload)
signed_payload: JWS[PayloadModel]Now I'm accomplishing same with custom generic type. Doable, but definitely much more verbose. |
|
Ye, I see your point on runtime behaviour not changing. Regarding |
|
thanks so much. |
Change Summary
Make Json a proper generic via
Annotated. Typecheckers will now infer the type of field asinner_typeofJson.These changes are soft breaking, one should use
Json[Any]instead of plainJson, as the later will result in error from mypy.In runtime, all should remain the same, except
Json[Any]is now allowed.Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)