apple / app-store-server-library-python

MIT License
168 stars 31 forks source link

[Suggestion] use dataclass or pydantic instead of attr for models. #10

Open ahnsv opened 1 year ago

ahnsv commented 1 year ago

Hi, just bumped into this repo while I was browsing apple's changed receipt validation policy. Though it seems like this repo is its early stage, I think it would be more straightforward for potential users to use dataclass or pydantic.

ahnsv commented 1 year ago

PS: I just found this thread, but still the question is unanswered. If it is okay, want to change the models into dataclass and stuffs.

alexanderjordanbaker commented 1 year ago

Could you please elaborate why it would be more straightforward for potential users?

ahnsv commented 1 year ago

Sorry for the late reply. The reason why I thought it would be better to use dataclass or pydantic instead of attrs is :

1) simplicity - attrs needs users to install additional dependency, while dataclass is in native python since 3.7 2) readability - IMHO attrs method name is not explicit enough for users (e.g., attrs.ib, attrs.s, and stuff...), while dataclasses fields are more explicit and readable. 3) popularity - dataclasses module has been widely adopted in many projects (attrs would be as well), and pydantic has gained more popularity with the rise of FastAPI. Going with dataclass or pydantic will give more chances to contribute to modern python users. 4) functionality (maybe overkill in this usecase?) - as far as I know, attrs would be a good choice when you need validation in data serialization with validation but it seems like no validation applied in the repo (just default = None). I don't know if it will be added in the future, but for now I think dataclass would be enough to do the work.

And here are some references I looked up: https://www.revsys.com/tidbits/dataclasses-and-attrs-when-and-why/ https://site-deploy--pydantic-docs.netlify.app/benchmarks/ https://threeofwands.com/why-i-use-attrs-instead-of-pydantic/ - This article says attrs over pydantic but pydantic's latest release (v2) is significantly better than the previous, so you can check it out too.

elonzh commented 1 year ago

In addition to those reasons listed above, pydantic is used in many web framework like fastapi. We can combines models with other server parts if using pydantic.

For example:

elonzh commented 1 year ago

More and more projects are beginning to use pydantic. For example OpenAI just released their new SDK(v1.0) with httpx and pydantic.

woosal1337 commented 6 months ago

More and more projects are beginning to use pydantic. For example OpenAI just released their new SDK(v1.0) with httpx and pydantic.

@bahadiraraz successfully implemented this feature, and our current app, which also utilizes the OpenAI service and app-store-server-library-python-async library, provides a very smooth developer experience. You can check it out in Issue #91: Asynchronous Implementation and Pydantic Support if you wish.