pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

Add a constant holding `core_schema.any_schema()` value

Open lig opened this issue 2 years ago • 4 comments

I would like the following code more but it will always call core_schema.any_schema()

        content_core_schema = schema.get('schema', core_schema.any_schema())

It would be nice to have a constant like core_schema.DEFAULT_SCHEMA that will hold this value and allow the above to be as efficient as if..in.

Originally posted by @lig in https://github.com/pydantic/pydantic/pull/6715#discussion_r1265881851

Selected Assignee: @lig

lig avatar Jul 17 '23 20:07 lig

I can do it myself

lig avatar Jul 17 '23 20:07 lig

I don’t think it’s worth it. Schema building should not be happening in a hot loop, and diverging from all other schema functions is not worth saving the 2 parentheses characters.

adriangb avatar Jul 17 '23 21:07 adriangb

Well, this is about saving a call to core_schema.any_schema() in case you need it as a default value. See the original case https://github.com/pydantic/pydantic/pull/6715#discussion_r1265881851

Having such a constant allows using it in constructs like schema.get('schema', core_schema.DEFAULT_SCHEMA) instead of schema['schema'] if 'schema' in schema else core_schema.any_schema(). Using core_schema.any_schema() in a former results in an extra function call.

lig avatar Jul 17 '23 21:07 lig

Why not schema.get('schema', core_schema.any_schema())?

adriangb avatar Jul 17 '23 21:07 adriangb

Fixed by https://github.com/pydantic/pydantic-core/pull/1394

sydney-runkle avatar Aug 16 '24 17:08 sydney-runkle