Search code examples
pythonvalidationpydantic

Pydantic Validation error: Input should be a valid dictionary or instance


I am trying to validate the latitude and longitude:

from pydantic import BaseModel, Field
from pydantic.dataclasses import dataclass

@dataclass(frozen=True)
class Location(BaseModel):
    longitude: float = Field(None, ge=-180, le=180)
    latitude: float = Field(None, ge=-90, le=90)


Location(longitude=1.0, latitude=1.0)

When I run this locally I get the following error:

Input should be a valid dictionary or instance of Location [type=model_type, input_value=ArgsKwargs((), {'longitud...: 1.0, 'latitude': 1.0}), input_type=ArgsKwargs]
    For further information visit https://errors.pydantic.dev/2.4/v/model_type

I have already tried to follow the Pydantic documentation which is given by the error but unfortunately is not clear and does not really give any information in regards of what to change and what to do differently.

I also tried to create a Python dictionary to see if the error could be solved but it still gave the same error.

Honestly, I have no idea of what to do next and how to solve this problem as I have tested it in several files (including in the same file to ensure it had nothing to do with any imports).

Any ideas of why the error occurs?

Update

Without the (BaseModel) when running the an instance of the dataclass in the same file it works. Now, I am creating different unit tests to test if the validators work correctly. Please check the following code snippet:

When creating a unit test and running a such test:

 def test_when_location_is_not_in_range_then_print_exception(self):
        invalid_location = Location(latitude=200.0, longitude=300.0)

        with self.assertRaises(ValueError) as context:
            location = Location(latitude=invalid_location.latitude, longitude=invalid_location.longitude)

        self.assertEqual(
            str(context.exception), "Location is out of range", "Expected exception message: 'Location is out of range'"
        )

Before running the test, a warning appears: Unexpected Argument

After running the test, a new error appears:

longitude
  Input should be less than or equal to 180 [type=less_than_equal, input_value=300.0, input_type=float]
    For further information visit https://errors.pydantic.dev/2.4/v/less_than_equal
latitude

  Input should be less than or equal to 90 [type=less_than_equal, input_value=200.0, input_type=float]
    For further information visit https://errors.pydantic.dev/2.4/v/less_than_equal

The error encountered before does not appear anymore.


Solution

  • I guess you're using dataclass from pydantic.dataclasses.

    In that case, don't inherit from BaseModel

    from pydantic import Field
    from pydantic.dataclasses import dataclass
    
    
    @dataclass(frozen=True)
    class Location:
        longitude: float = Field(None, ge=-180, le=180)
        latitude: float = Field(None, ge=-90, le=90)
    
    
    Location(longitude=1.0, latitude=1.0)
    

    Read more at https://docs.pydantic.dev/latest/concepts/dataclasses/


    Regarding the update to your question - the validation errors come from

        invalid_location = Location(latitude=200.0, longitude=300.0)
    

    You've limited the valid range of values for the latitude and longitude attributes, hence the errors when values provided do not adhere to those requirements.