Skip to content
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

feat(minor): Allow passing config to parse_obj_as #4805

Closed
wants to merge 1 commit into from

Conversation

gavindsouza
Copy link

The parse_obj_as API doesn't allow arbitrary objects which can be extended by passing Config. My use case is similar to this question @samuelcolvin has answered on SO.

# before - ohno runtime error I can probably do nothing about ༼ಢ_ಢ༽ 
In [1]: parse_obj_as(list[File], 'true')
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File <ipython-input-7-4e10774d8e55>:1, in <cell line: 1>()
----> 1 parse_obj_as(list[File], 'true')
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/tools.py:30, in pydantic.tools._get_parsing_type()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/main.py:1026, in pydantic.main.create_model()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/main.py:198, in pydantic.main.ModelMetaclass.__new__()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/fields.py:506, in pydantic.fields.ModelField.infer()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/fields.py:436, in pydantic.fields.ModelField.__init__()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/fields.py:557, in pydantic.fields.ModelField.prepare()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/fields.py:831, in pydantic.fields.ModelField.populate_validators()
File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/validators.py:765, in find_validators()
RuntimeError: no validator found for <class 'frappe.core.doctype.file.file.File'>, see `arbitrary_types_allowed` in Config

# after - works as expected ヽ(^ᴗ^ヽ)
In [2]: parse_obj_as(list[File], 'true', config=FrappePydanticConfig)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
File <ipython-input-12-268e8a88311f>:1, in <cell line: 1>()
----> 1 parse_obj_as(list[File], 'true', config=FrappePydanticConfig)

File ~/Desktop/projects-bench/apps/frappe/frappe/utils/typing_validations.py:76, in parse_obj_as(type_, obj, type_name, config)
     67 def parse_obj_as(
     68 	type_: type[T],
     69 	obj: Any,
   (...)
     73 ) -> T:
     74 	# Note: This is a copy of pydantic.tools.parse_obj_as with the addition of allowing a config argument
     75 	model_type = _get_parsing_type(type_, type_name=type_name, config=config)  # type: ignore[arg-type]
---> 76 	return model_type(__root__=obj).__root__

File ~/Desktop/projects-bench/env/lib/python3.10/site-packages/pydantic/main.py:342, in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for ParsingModel[list[frappe.core.doctype.file.file.File]]
__root__
  value is not a valid list (type=type_error.list)

@hramezani
Copy link
Member

hramezani commented Dec 1, 2022

Thanks @gavindsouza for this 👍

I am not sure whether we want to accept this. but you need to:

  • Add test for your change
  • Update parse_obj_as documentation and examples

@samuelcolvin what do you think about this?

@gavindsouza
Copy link
Author

I hope this change is accepted if parse_obj_as is to be considered a public API. I've currently had to redefine _get_parsing_type and parse_obj_as in frappe/frappe@f7c5b40.

Will add a test and update docs if this change is acceptable @hramezani. (Also, thank you for your good work :D)

@samuelcolvin
Copy link
Member

hi @gavindsouza thanks so much for the contribution, parse_obj_as will be removed in V2 and replaced by the generic validate method, see #4669. Hence it's not really worth your effort to finish this as it'll get deleted just as soon as I get serialisation in rust finished and get onto this.

If you have feedback on the API, best to add it there to #4669. But we will definitely need to allow config to be set with validate(), see also #4673.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants