Skip to content

Question regarding usage of generics #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Viicos opened this issue Feb 6, 2024 · 5 comments
Closed

Question regarding usage of generics #124

Viicos opened this issue Feb 6, 2024 · 5 comments

Comments

@Viicos
Copy link

Viicos commented Feb 6, 2024

Hello,

thanks for this library. I'm looking for a way to parse JSON Schema into something that can be used in Python but couldn't find anything suitable. jsonschema can validate JSON "instances" against a schema, but does not seem to provide such parsing abilities.

As I'm probably going to parse the schemas "by hand", referencing looks interesting for my task as I will not have to resolve references manually.


That said, I just wanted to raise something that feels inconsistent regarding typing. The Registry is currently defined as a Mapping[URI, Resource[D]], simplified as Mapping[str, D]. Does this mean a Registry should hold only one type of resources?

This D type variable seems to be kept when iterating over subresources and anchors as well, e.g. subresources returns an Iterable[Resource[D]].

My understanding is for JSON schemas, this D type variable should always resolve to Union[bool, ObjectSchema], even when iterating over the subresources. But I don't know if this is the case if not dealing with JSON schemas?

@Julian
Copy link
Member

Julian commented Feb 6, 2024

Hi there!

jsonschema can validate JSON "instances" against a schema, but does not seem to provide such parsing abilities.

Depending on what you mean by "parsing" schemas, though I suppose that's besides the point of your question! (jsonschema uses this library for reference resolution of course, which was the main initial reason to write this!)

For the question itself -- firstly, there's certainly a possibility the types are wrong, but:

My understanding is for JSON schemas, this D type variable should always resolve to Union[bool, ObjectSchema], even when iterating over the subresources

It depends on what draft of JSON Schema. On earlier ones, there's no union, it's just ObjectSchema, and subresources also must be ObjectSchema. On newer ones, yeah, it's the union. And going beyond official JSON Schema dialects it indeed it may be something entirely different (e.g. in jsonschema it's potentially effectively Any, as you can use whatever kind of objects you want as schemas).

Presumably if you have multiple different kind of resources you get a larger union type there (e.g. if this project ever gets some OpenAPI support, which I still hopefully plan on).

But honestly as I started with, there's still a possibility something is wrong, I did some small level of thinking but not fully, mostly since I unfortunately usually find typing to be a net negative in terms of time investment, and generally consider it to be a documentation tool, so if you have a concrete issue in front of you which suggests something is wrong I'm happy to look at a PR or make a tweak.

@Viicos
Copy link
Author

Viicos commented Feb 6, 2024

Thanks for the quick answer!

Depending on what you mean by "parsing" schemas, though I suppose that's besides the point of your question! (jsonschema uses this library for reference resolution of course, which was the main initial reason to write this!)

Basically what I'm trying to do is provide the user a list of "json paths" (format TBD, can be JSON Pointer, lodash style, etc) from a JSON Schema, so having a structured Python representation of the schema could help. I think this can be pretty straightforward to do manually (i.e. iterating over the json.load dict) apart from ref resolution, where this library comes in really handy. So basically I'm trying to have https://github.com/gazpachoking/jsonref, but with referencing

Presumably if you have multiple different kind of resources you get a larger union type there (e.g. if this project ever gets some OpenAPI support, which I still hopefully plan on).

Ok good to know, I guess this will be discovered more once more resources types are being used!

I unfortunately usually find typing to be a net negative in terms of time investment, and generally consider it to be a documentation tool

I can agree with this in certain situations -- typing can be more thought as a library developer assistant (it catches some errors even before adding tests) and a library user assistant (documentation + IDE integration; I have to say I'm glad the library is typed, it really helped understanding the data structure and relation between registries, documents and specifications).

@Julian
Copy link
Member

Julian commented Feb 6, 2024

So basically I'm trying to have https://github.com/gazpachoking/jsonref, but with referencing

Cool -- the monkey wrench in modern JSON Schema is $dynamicRef, where there are cases which are completely impossible to do this kind of thing on. But definitely keen to see what you come up with! Tooling in Python is definitely still full of holes (really the kind of thing I want to grow in the next few years in this organization, and of course in part with this tool.) So yeah keep me posted!

Going to close this then but more feedback definitely welcome!

@Julian Julian closed this as completed Feb 6, 2024
@Julian
Copy link
Member

Julian commented Feb 6, 2024

I should say a general "structured schema walker" is indeed a specific tool I have in mind, but there are other things I want to get to first... To quote the great, "so much time, and so little to do...".

@Viicos
Copy link
Author

Viicos commented Feb 6, 2024

To quote the great, "so much time, and so little to do...".

I feel the same every day.

I should say a general "structured schema walker" is indeed a specific tool I have in mind

As my current task is work related, I might have some time working on this. Hopefully I'll be able to have it generic enough to be implemented as part of this library, or in a separated one, or as a reusable gist

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

No branches or pull requests

2 participants