Happy-Kunal / pydantic2avro

Generate Apache Avro schemas for Pydantic data models.
https://pypi.org/project/pydantic2avro/
MIT License
0 stars 0 forks source link

AttributeError: module 'pydantic' has no attribute 'networks' #3

Closed SROMCY closed 1 month ago

SROMCY commented 1 month ago

There seems to be an issue with this library. Maybe it is an incompatability of the latest pydantic library.

image

image

image

Happy-Kunal commented 1 month ago

Hello @SROMCY ,

First of all, thank you for using pydantic2avro. I was able to import pydantic.networks as shown in the snippet below:

$ python
Python 3.11.9 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import pydantic.networks
>>> 
>>> print(pydantic.networks.__all__)
['AnyUrl', 'AnyHttpUrl', 'FileUrl', 'FtpUrl', 'HttpUrl', 'WebsocketUrl', 'AnyWebsocketUrl', 'UrlConstraints', 'EmailStr', 'NameEmail', 'IPvAnyAddress', 'IPvAnyInterface', 'IPvAnyNetwork', 'PostgresDsn', 'CockroachDsn', 'AmqpDsn', 'RedisDsn', 'MongoDsn', 'KafkaDsn', 'NatsDsn', 'validate_email', 'MySQLDsn', 'MariaDBDsn', 'ClickHouseDsn']
>>> 
>>> pydantic.__version__
'2.8.2'
>>> 

If you don't mind, could I get the code for the pydantic model MoveAndSettleRaw or a minimal version of any other pydantic model that is producing the same error for debugging purposes?

Best Regards, Kunal Sharma

SROMCY commented 1 month ago

Hi Kunal, the model is rather large with many nested models.

I found that the problem propably comes from the typing.Literal .

Here a minimal sample:

` import json from pprint import pprint from typing import Literal

from pydantic import BaseModel, Field from pydantic2avro import PydanticToAvroSchemaMaker

from datatypes.measurements.dynamic import MoveAndSettleRaw from datatypes.util.enum import MeasurementType

class MinimalModel(BaseModel): type: Literal["hello"] = Field(default="hello")

m = MinimalModel() print(m) schema = PydanticToAvroSchemaMaker(MinimalModel).get_schema() pprint(schema) `

image

EDIT: the same goes for typing.Optional

Happy-Kunal commented 1 month ago

Hello @SROMCY ,

First of all, thanks for your patience. I wanted to let you know that an import of pydantic.networks was missing in src/pydantic2avro/schema_maker.py. For some reason, my linter didn't report it, and coincidentally, all the places where I was using pydantic2avro had pydantic.networks imported one way or another. Hence, I didn't notice the problem until you reported it.

I have deployed the pydantic2avro v0.4.1 on pypi. Following code is now working as expected:

from typing import Literal, Optional
from pprint import pprint

from pydantic import BaseModel, Field
from pydantic2avro import PydanticToAvroSchemaMaker

class MinimalModel(BaseModel):
    type: Literal["hello"] = Field(default="hello")

schema = PydanticToAvroSchemaMaker(MinimalModel).get_schema()

pprint(schema)

output:

$ python minimodel.py 
{'fields': [{'name': 'type',
             'type': {'name': 'type', 'symbols': ['hello'], 'type': 'enum'}}],
 'name': 'MinimalModel',
 'type': 'record'}

As for the typing.Optional it will be supported in version 0.5.0 which I am planning to publish by next week, with some minor code changes to fix issue #4 .

Thank you for helping to make pydantic2avro a little bit better. Best Regards, Kunal Sharma