mkdocstrings / griffe

Signatures for entire Python programs. Extract the structure, the frame, the skeleton of your project, to generate API documentation or find breaking changes in your API.
https://mkdocstrings.github.io/griffe
ISC License
281 stars 41 forks source link

feature: stable `as_json` #310

Closed patrick91 closed 3 weeks ago

patrick91 commented 1 month ago

Hi there! I have not investigated this much, but looks like using as_json doesn't always return the same data (or at least not in the same order) see this diff: https://github.com/strawberry-graphql/strawberry.rocks/commit/fc70f2a1f42c8fd69ce213d8366a71e683be5827

For my use case I'll do some sorting, but I was wondering if this could be a feature of Griffe 😊

pawamoy commented 1 month ago

113,475 additions, 113,475 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

Ouch :sweat_smile:

Yes that's a good idea, we can probably use json.dumps(..., sort_keys=True) :slightly_smiling_face:

patrick91 commented 1 month ago

@pawamoy I was using sort_keys, but then the order of the members would change too, not sure why :D

pawamoy commented 1 month ago

Ah 🤔 I'll investigate. Members order should definitely stay the same. Unless you're using dynamic analysis and inspect.getmembers doesn't return members in the same order every time 🤔

pawamoy commented 1 month ago

Can you tell me (again? sorry if I forgot) how you dump and reload the data? Or just point me at the code you're using to dump/load?

pawamoy commented 1 month ago

I can't replicate the members dis-ordering issue across multiple invocations of Griffe :thinking:

pawamoy commented 1 month ago

Note that members are dumped in the order they are found in the sources. If you move objects around, the order in the JSON dump will also change :slightly_smiling_face: Maybe we shouldn't do that and sort alphabetically instead, since we either:

By the way this source ordering is completely implicit and relies on default ordering of Python dicts since Python 3.7 (I think it's 3.7).

And if we don't need the initial, implicit order of members, maybe we can even stop using a list and use a dict when dumping them instead :shrug: That would by the way make the JSON more readable (for example, no need to expand an item in Firefox to see the member's name, all names would be displayed right there in the tree without having to expand anything).