Skip to content
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

Add data structure, parsing and serialisation for direct_url.json #701

Open
pradyunsg opened this issue Jul 9, 2023 · 9 comments
Open

Comments

@pradyunsg
Copy link
Member

x-ref https://discuss.python.org/t/place-for-a-direct-url-json-parser/26266
Corresponding code in pip: https://github.com/pypa/pip/blob/23.1.2/src/pip/_internal/models/direct_url.py

@brettcannon
Copy link
Member

https://packaging.python.org/en/latest/specifications/direct-url-data-structure/

Do we just want a TypedDict for this? Or are you after something fancier?

@ofek
Copy link
Contributor

ofek commented Jul 11, 2023

Would that way support conditional logic? Just asking because I'm unfamiliar with the limitations of typing

@brettcannon
Copy link
Member

Would that way support conditional logic? Just asking because I'm unfamiliar with the limitations of typing

What condition are you explicitly trying to model in the data?

@ofek
Copy link
Contributor

ofek commented Jul 13, 2023

I mean that the data is contingent upon what the URL field is

@brettcannon
Copy link
Member

I mean that the data is contingent upon what the URL field is

If it's all self-contained and can be determined by a literal, then yes. E.g. {vsc: "git", ...} versus {vcs:"hg", ...} can have different keys. Then type for any key that could be either would be the union and the type checkers would determine what you're working with by seeing if you checked if the literal value was used.

@ofek
Copy link
Contributor

ofek commented Jul 13, 2023

I think it's more dynamic and based on the top level url key as seen here https://packaging.python.org/en/latest/specifications/direct-url-data-structure/#examples

@brettcannon
Copy link
Member

I think it's more dynamic and based on the top level url key as seen here https://packaging.python.org/en/latest/specifications/direct-url-data-structure/#examples

Ah, there might be some way to use Annotated, but I'm not sure.

@pradyunsg
Copy link
Member Author

pradyunsg commented Jul 14, 2023

One concern I have with a TypedDict model is that it is non-trivial to validate untrusted data against it, without taking on additional dependencies or having a bunch of extra logic. We can definitely do that tho, if the type system is capable!

If this is something that we want to design for adoption in installers1, IMO we should have the ability to error out with a decent error message about malformed variants of this file (stuff the existing pip logic raises DirectUrlValidationError for).

Footnotes

  1. which is the tooling that generates and parses this information, but I guess someone generating an SBOM might want this too.

@edgarrmondragon
Copy link
Contributor

I'd be happy to bring https://github.com/edgarrmondragon/pep610/ into packaging if it fits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants