-
Notifications
You must be signed in to change notification settings - Fork 3
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
Converting input parameters to Pydantic base models #23
Comments
Yes, this is definitely something that you can do. The problem with the code you have is that One easy way to solve this problem is to use import parametrize_from_file as pff
from pydantic import BaseModel
import builtins
class Person(BaseModel):
first_name: str
last_name: str
@pff.parametrize(schema=[pff.cast(person=Person.model_validate)])
def test_object(person: Person, len: int):
assert person.first_name == "John"
assert builtins.len(person.first_name) == len Another possible way to solve this problem is to use a lambda function to expand out the keyword arguments yourself. In this specific case, I think the @pff.parametrize(schema=[pff.cast(person=lambda x: Person(**x))])
def test_object(person: Person, len: int):
... Let me know if you have any other questions, I'd be happy to help! |
Nice! I used the first approach and it works! I am getting a warning from PyCharm on this part of the decorator:
|
Unfortunately, I have no idea what's going on with that warning. This reminds me a bit of #20, which also involves PyCharm generating warnings where it shouldn't. |
Maybe typing the function's input parameters and defining return types helps. For the warnings mentioned in #20 I changed this part of the code and managed to get rid of the @_decorator_factory
-def parametrize(param_names, param_values, kwargs):
@_decorator_factory
+def parametrize(param_names, param_values, **kwargs): The other warning, |
If adding Also, you should undo |
Hello!
I am using your library to parametrize my tests! I am using Pydantic in my project and would like to use it to (de)serialize the input objects with it. My test case looks as follows:
I don't know if I am using the
schema
and thecast
correctly in this context, but I would like to convert the complex object into a base model. Alternatively, I can do this:which works, but it would be nice if the library could do the conversion on its own! Is there a way of doing this?
The text was updated successfully, but these errors were encountered: