-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 InstanceOf type/annotation #5778
Conversation
Should we just call it |
The only reason I didn't already do that was to avoid conflict with dirty_equals. But it's probably worth it given use of both together should be pretty rare. I've changed it to that now. |
Deploying with Cloudflare Pages
|
|
I think it probably makes sense to just keep it as |
please review |
@adriangb I'm interested in your opinion on this note:
In particular, we could add arguments to the dataclass and just use "default values" when applied via (I'm potentially happy to add none and just call this done, but if you have any concerns or good ideas I think we can still augment this a bit.) |
It would be backwards compatible to add knobs right? If so I'd say lets not add them for now. |
I could rename to One thing that might be convenient is to reserve one of these names for use as the annotation type (which may one day accept configuration arguments), and the other for the thing that is treated by typechecker as That way, in the future (not necessary today) it might look like: @_internal_dataclass.slots_dataclass
class InstanceOf:
config_flag: bool = False
@classmethod
def __class_getitem__(cls, item: AnyType) -> AnyType:
return Annotated[item, cls()]
@classmethod
def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
# use the generic _origin_ as the second argument to isinstance when appropriate
python_schema = core_schema.is_instance_schema(get_origin(source) or source)
json_schema = handler(source)
return core_schema.json_or_python_schema(python_schema=python_schema, json_schema=json_schema)
if TYPE_CHECKING:
# If we add configurable attributes to IsInstance, we'd probably need to stop hiding it from type checkers like this
IsInstance = Annotated[AnyType, ...] # `IsInstance[Sequence]` will be recognized by type checkers as `Sequence`
else:
IsInstance = InstanceOf |
please update (need to call it InstanceOf instead of IsInstance and then we'll merge) |
please review |
Closes #5773
Looking for feedback on the implementation here before doing documentation/etc.
I've set it up so it has the standard behavior for JSON and JSON schema by default, just uses
isinstance
for python. We could make this configurable but I wanted to hash out the desired configuration choices first.Unfortunately, due to the way JSON gets parsed on the Rust side, it seems that we don't get perfect preservation of big-ints, which was one of the things I was hoping to address with this change:
Not sure if it makes sense to worry about this, but I at least wanted to point it out.
Selected Reviewer: @adriangb