dan-fritchman / Hdl21

Hardware Description Library
BSD 3-Clause "New" or "Revised" License
60 stars 13 forks source link

Pydantic v2 #157

Closed dan-fritchman closed 2 months ago

dan-fritchman commented 1 year ago

Came faster than expected: https://twitter.com/pydantic/status/1674820177005887508

daquintero commented 1 year ago

Worth mentioning they have a pydantic.v1 notation for an intermediate migration. They describe it here

daquintero commented 1 year ago

Also apparently there is an automatic migration tool https://github.com/pydantic/bump-pydantic#installation

growly commented 11 months ago

(FYI) Ran into this when I accidentally had pydantic 2.1.1

  File "/home/arya/src/Hdl21/hdl21/__init__.py", line 12, in <module>
    BaseModel.Config.arbitrary_types_allowed = True
    ^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py3.11/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 205, in __getattr__
    raise AttributeError(item)
AttributeError: Config
dan-fritchman commented 11 months ago

Yep, they're definitely not designing it to be API-for-API compatible.

dan-fritchman commented 4 months ago

WIP on PR #214.

Noting:

dan-fritchman commented 4 months ago

PR #214 tests have a few holes left.
Smallish (I think) stuff:

Prefixed is the sole remaining real thing.
Prefixed uses Pydantic's "custom data types" feature, to enable things like:

@dataclass 
class C: 
  p: h.Prefixed = 1.1 # <= this gets converted inline

# So do these:
C(p=1)
C(p=Decimal("3.1415")

The custom data types feature is pretty well overhauled in Pydantic v2. Definitely for the better, but different. The way to do this now is something like:

# Just add this `Annotated` thing
Prefixed = Annotated[Prefixed, BeforeValidator(to_prefixed)

Quite a bit nicer way to get the custom validation and checking.
Problem is, that Annotated... thing (it's a "special form", much like Union) does not play nice with isinstance. And we have isinstance(thing, Prefixed) all over. Particularly we have tons of values of type, like, Union[int, float, Prefixed], in which case something like isinstance(Prefixed) is, if not a necessity, certainly very helpful.

Couple options I can think of:

dan-fritchman commented 3 months ago

Prior comments spawned some other, smaller issues including:

And the resolution on Prefixed is "don't do custom validation for Prefixed".
And with that, PR #804 is just about there, hopefully finishing verification now.

daquintero commented 2 months ago

Hi @dan-fritchman, thanks so much for all the effort you have put into this, it's really exciting to explore the integrations this will enable. I was wondering, are #218 and #215 the blocking aspects for, say, a pre-release of a pydantic v2 version? Depending if 3.7 and 3.8 support is depreciated assuming they both are reaching end of life #217 image

dan-fritchman commented 2 months ago

Here is how I am thinking about what must (and might) be in the next release: #220

dan-fritchman commented 2 months ago

@daquintero where is that Python version/ release chart from BTW?
Looks pretty helpful.

daquintero commented 2 months ago

Ahh this is great for the next release, thanks!

The diagram is from here https://devguide.python.org/versions/ and it's really helps managing the target support for releases imo

dan-fritchman commented 2 months ago

Terrific that is quite helpful.
And I'm going to refer further commentary on the topic to #217.

dan-fritchman commented 2 months ago

Live in v6 at https://pypi.org/project/hdl21/6.0.0/