Conversation
034175e to
bdec986
Compare
Co-authored-by: Daniel W. Park <daniel.park@vercel.com> Co-authored-by: Yury Selivanov <yury@vercel.com>
bdec986 to
6f73b61
Compare
| we don't want to tangle with yet, and because many use cases can be | ||
| simulated in other ways. | ||
|
|
||
| .. * Should we support building new nominal types?? |
There was a problem hiding this comment.
It's up to you! Delete if not adding.
| .. * Should we support building new nominal types?? |
hugovk
left a comment
There was a problem hiding this comment.
GitHub insists "You need to leave a comment indicating the requested changes." 🤷
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
|
Thanks for the edits! |
| That is, it is a "consistent subtype". This is subtyping extended | ||
| to gradual types. | ||
|
|
||
| * ``IsEquivalent[T, S]``: |
There was a problem hiding this comment.
That's inconsistent with how the spec defines the term "equivalent". This is describing consistency, not equivalence.
There was a problem hiding this comment.
Oh, argh, you're right. IsConsistent seems quite a worse name, though. I've added a note indicating that the real relation is consistency, and we can discuss what the name ought to be in detail as part of the main pep discussion?
| In order to allow an evaluator library to trigger type evaluation in | ||
| those cases, we add a new hook to ``typing``: | ||
|
|
||
| * ``special_form_evaluator``: This is a ``ContextVar`` that holds a |
There was a problem hiding this comment.
I don't really understand how you'd use this.
Let's look at a concrete example. If I define a Pydantic-like class like this:
class MyModel[T](BaseModel):
x: T
y: int if IsAssignable[T, int] else str
MyModel(1, 1) # OK
MyModel(1.0, "x") # OK
MyModel(1.0, 1) # error, second arg must be strWhat would Pydantic need to do so it can typecheck this correctly at runtime?
There was a problem hiding this comment.
Those particular examples I think probably wouldn't work at runtime (... though I think if you really tried you could make it) since they'd have to do inference based on the easily inferrable arguments, which pydantic doesn't do. It does support it if you supply the type with [] though, so it should be able to support:
MyModel[int](x=1, y=1) # OK
MyModel[float](x=1.0, y="x") # OK
with pytest.raises(TypeError):
MyModel[float](x=1.0, y=1) # error, second arg must be str
I made an example of how it can be implemented on top of the current prototype type evaluator library we have (supporting my modified example above): https://github.com/vercel/python-typemap/blob/e0301f889037b86c3e2b83467d9e2ee0560ba879/tests/test_model_like.py#L23-L34
The approach is that we need to "evaluate" the type MyModel[float], which means to substitute in the type arguments and evaluate the members as needed, which will sometimes involve evaluating type operators.
To do this we'll call the __annotate__ function on MyModel with the type argument from the _GenericAlias injected in place of the TypeVar for T. While doing this whole process, we'll have special_from_evaluator set to point at a callback that calls back into the type evaluator, which will then evaluate IsAssignable, etc.
It's a little involved but can be well-encapsulated--the Pydantic-like constructor in the test basically just calls eval_typing.
Basic requirements (all PEP Types)
pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) andPEPheaderAuthororSponsor, and formally confirmed their approvalAuthor,Status(Draft),TypeandCreatedheaders filled out correctlyPEP-Delegate,Topic,RequiresandReplacesheaders completed if appropriate.github/CODEOWNERSfor the PEPStandards Track requirements
Python-Versionset to valid (pre-beta) future Python version, if relevantDiscussions-ToandPost-History📚 Documentation preview 📚: https://pep-previews--4834.org.readthedocs.build/pep-0827/