pydantic icon indicating copy to clipboard operation
pydantic copied to clipboard

New API for date-like types unit/format configuration

Open sydney-runkle opened this issue 10 months ago • 14 comments

Fixes https://github.com/pydantic/pydantic/issues/10454 Fixes https://github.com/pydantic/pydantic/issues/7940

Previously outlined this API in https://github.com/pydantic/pydantic/pull/10949, but continuing here with a clean history.

sydney-runkle avatar Feb 27 '25 16:02 sydney-runkle

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: cd7d95c
Status:🚫  Build failed.

View logs

@ollz272, would love your feedback on this API proposal! Want to solidify things before I implement logic in pydantic-core.

sydney-runkle avatar Feb 27 '25 16:02 sydney-runkle

CodSpeed Performance Report

Merging #11504 will not alter performance

Comparing datetime-config-settings (cd7d95c) with main (bff7477)

Summary

✅ 46 untouched benchmarks

codspeed-hq[bot] avatar Feb 27 '25 16:02 codspeed-hq[bot]

@ollz272, would love your feedback on this API proposal! Want to solidify things before I implement logic in pydantic-core.

Hi!

Take with a pinch of salt as I only have access to my phone at the moment! However looking at this, all seems sensible and will achieve what we’re looking for!

really excited to see this in 2.11!

ollz272 avatar Feb 27 '25 19:02 ollz272

I'm about to go on leave for a couple of weeks so if any other input from me is wanted i won't be around, @benjsec may be able to give some input my absence. Really looking forward to seeing this!

ollz272 avatar Feb 28 '25 11:02 ollz272

After much discussion with the team, we've settled on the above, which closely matches some of the work that @ollz272 did previously, so I'll port that over in pydnatic-core shortly.

sydney-runkle avatar Mar 03 '25 16:03 sydney-runkle

@ollz272,

Unfortunate update here - I'll be scaling back my work on pydantic starting next week, so this might not get finished for the v2.11 release. That being said, I confirmed with our team internally that this is the API we want to move forward with for these requested features. If anyone wants to pick this up, we'd happily accept PRs advancing this feature.

Sorry for the churn on this feature request - it definitely fell to the bottom of our todo list a few times as features like improving the alias configuration API took priority due to popular demand. I'll leave this PR open for now (maybe someone will have the capacity to pick this up in the next month or so). @Viicos or @davidhewitt, feel free to close for cleanup purposes as you see fit (it's linked to the relevant issues, so should be easy enough to dig up later).

sydney-runkle avatar Mar 06 '25 16:03 sydney-runkle

Hi @sydney-runkle,

slowly coming back now. Unfortunate that this didn’t make it into 2.11, but understandable. Will this be prioritised internally for 2.12? Or will you be relying on community PR’s to push this along?

ollz272 avatar Mar 27 '25 21:03 ollz272

@davidhewitt @Viicos Hi! I saw on linkedin that sydney has moved on from pydantic so maybe she wasn't the best person to ask the above question to, sorry if you guys aren't either!

Same question: Will this be prioritised internally for 2.12? Or will you be relying on community PR’s to push this along?

ollz272 avatar Apr 01 '25 08:04 ollz272

Hi @ollz272 sorry for the slow response. We'll tackle this in 2.12. It might be that we recommend using validators instead (unless not possible or incurs performance issues, I'll need to take a deeper look), as we want to avoid introducing many configuration values that affect validation behavior as this can get quickly out of hand.

Viicos avatar Apr 01 '25 08:04 Viicos

Hi @ollz272 sorry for the slow response. We'll tackle this in 2.12. It might be that we recommend using validators instead (unless not possible or incurs performance issues, I'll need to take a deeper look), as we want to avoid introducing many configuration values that affect validation behavior as this can get quickly out of hand.

Thanks! I think the main thing i was concerned with in the original issue was serialisation, so im not sure a validator would fix that. Open to other suggestions on how this could work, ofc, but really we want to avoid things like this:

T_SerializedDatetime = Annotated[
    dt.datetime,
    PlainSerializer(
        lambda x: x.timestamp() * 1000,
        return_type=int,
        when_used="json",
    ),
]

class Value(BaseModel):
    series: dict[T_SerializedDatetime, float]

as the cost of computing the x.timestamp() * 1000 many times (i guess some overhead from going back and forth from python to rust as well?) eats into a high amount of time for us.

ollz272 avatar Apr 01 '25 08:04 ollz272

Hi @DouweM, i think from looking around you’re now managing pydantic! Congrats!

I was just hoping to get some insight into progress on this issue/pr and if we can expect it in 2.12?

ollz272 avatar May 29 '25 18:05 ollz272

@ollz272 Hi Oliver, I'm primarily on PydanticAI and @Viicos is the primary developer on Pydantic, so I'd need to check with him to see if his comment from April 1 about this going into 2.12 is still accurate. He's out right now and will be back next Wednesday, I'll make a note to ask him then!

DouweM avatar May 29 '25 19:05 DouweM

Hi @ollz272 , yes this will definitely be tackled for 2.12. I'm not fully working on Pydantic these days but I'll get back around it before the release for sure.

Viicos avatar May 29 '25 19:05 Viicos

@ollz272 I believe we can close this one right?

Viicos avatar Sep 05 '25 13:09 Viicos

I think so, yes. Will close.

davidhewitt avatar Sep 15 '25 08:09 davidhewitt