For bugs/questions:
import sys; print(sys.version): Python 3.6.8 :: Anaconda, Inc.import pydantic; print(pydantic.VERSION): 0.30I would like to make a custom data type like done here: https://pydantic-docs.helpmanual.io/#custom-data-types but use numpy.ndarray. I have made validators so that the object can construct but I can't seem to figure out how to properly define the schema for this. Is this something I should be able to do and how?
import pydantic
import numpy
class NumpyNDArray(numpy.ndarray):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v: Any) -> str:
# validate data...
return v
class RemoteExample(pydantic.BaseModel):
numpy_sample: NumpyNDArray = pydantic.Schema(None, alias='numpy_sample')
a = numpy.ndarray((1,2,3))
re = RemoteExample(numpy_sample=a)
print(re.schema())
Traceback (most recent call last):
File "test_resources.py", line 8, in <module>
print(re.schema())
File ".../python3.6/site-packages/pydantic/main.py", line 463, in schema
s = model_schema(cls, by_alias=by_alias)
File ".../python3.6/site-packages/pydantic/schema.py", line 235, in model_schema
model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File ".../python3.6/site-packages/pydantic/schema.py", line 553, in model_process_schema
model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
File ".../python3.6/site-packages/pydantic/schema.py", line 581, in model_type_schema
f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
File ".../python3.6/site-packages/pydantic/schema.py", line 294, in field_schema
known_models=known_models or set(),
File ".../python3.6/site-packages/pydantic/schema.py", line 524, in field_type_schema
known_models=known_models,
File ".../python3.6/site-packages/pydantic/schema.py", line 794, in field_singleton_schema
raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
ValueError: Value not declarable with JSON Schema, field: numpy_sample type=NumpyNDArray default=None
I tried looking through field_singleton_schema and couldn't find anything obvious to do.
see #380 for a similar discussion, apart from schema.
Regarding schema generation, I'm afraid it's not possible at the moment afaik.
I guess we should add support for a __get_schema__ or similar method on custom types. I thought we already had a dicussion about it, but can't find an issue.
@tiangolo thoughts?
+1 for __get_schema__, would you take a PR for this?
:+1:
Agreed. I've been planning on implementing/proposing __get_schema__ for custom types, but haven't found the time 馃槄
I haven't touched this yet and will be a few weeks on my end if you get to it first :)
This should be fixed by #1065. Please comment there if you have any feedback or suggestions.
Most helpful comment
This should be fixed by #1065. Please comment there if you have any feedback or suggestions.