pydantic-to-typescript icon indicating copy to clipboard operation
pydantic-to-typescript copied to clipboard

Script raises AttributeError using Pydantic 2.3

Open waterfallwhitebread opened this issue 2 years ago • 1 comments

Using this script in my project with Pydantic 2.3 (likely all 2.x versions, just tried 2.2 also with same result) with example provided in readme leads to an AttributeError:

C:\SDKs\Python\xxx\Lib\site-packages\pydantic\_migration.py:276: UserWarning: `pydantic.generics:GenericModel` has been moved to `pydantic.BaseModel`.
  warnings.warn(f'`{import_path}` has been moved to `{new_location}`.')
2023-09-19 07:18:05,304 Finding pydantic models...
Traceback (most recent call last):
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic2ts\cli\script.py", line 285, in <module>
    main()
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic2ts\cli\script.py", line 276, in main
    return generate_typescript_defs(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic2ts\cli\script.py", line 201, in generate_typescript_defs
    models = extract_pydantic_models(import_module(module))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic2ts\cli\script.py", line 81, in extract_pydantic_models
    for _, model in inspect.getmembers(module, is_concrete_pydantic_model):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\inspect.py", line 595, in getmembers
    return _getmembers(object, predicate, getattr)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\inspect.py", line 586, in _getmembers
    if not predicate or predicate(value):
                        ^^^^^^^^^^^^^^^^
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic2ts\cli\script.py", line 69, in is_concrete_pydantic_model
    return bool(obj.__concrete__)
                ^^^^^^^^^^^^^^^^
  File "C:\SDKs\Python\xxx\Lib\site-packages\pydantic\_internal\_model_construction.py", line 210, in __getattr__
    raise AttributeError(item)
AttributeError: __concrete__

Using Pydantic 1.9 generated correct TS file. Is this script only supposed to be working with Pydantic 1.x? I didn't see such a limitation in documentation. Thanks!

waterfallwhitebread avatar Sep 19 '23 05:09 waterfallwhitebread

There are fixes raised in PRs:

  • #40
  • #38
  • #37

Personally I think this library should be following the versioning strategy of pydantic itself and documented as supporting matching major versions.

nick-somebody avatar Oct 02 '23 05:10 nick-somebody