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

Error when TypeAlias is used #34

Open
khalo-sa opened this issue Jan 27, 2023 · 2 comments
Open

Error when TypeAlias is used #34

khalo-sa opened this issue Jan 27, 2023 · 2 comments

Comments

@khalo-sa
Copy link

Hi and thank you for this package!

I would like to report a bug with version 1.0.10 and Python version 3.10.

Assume we have this Python file:

# test.py
from typing import TypeAlias
from pydantic import BaseModel

CustomType: TypeAlias = list[str]

class Test(BaseModel):
	x: int

Now if I run pydantic2ts --module test.py --output ./test.ts I get the following error:

Traceback (most recent call last):
  File "/Users/user/miniforge3/envs/py310/bin/pydantic2ts", line 8, in <module>
    sys.exit(main())
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/site-packages/pydantic2ts/cli/script.py", line 274, in main
    return generate_typescript_defs(
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/site-packages/pydantic2ts/cli/script.py", line 201, in generate_typescript_defs
    models = extract_pydantic_models(import_module(module))
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/site-packages/pydantic2ts/cli/script.py", line 81, in extract_pydantic_models
    for _, model in inspect.getmembers(module, is_concrete_pydantic_model):
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/inspect.py", line 482, in getmembers
    if not predicate or predicate(value):
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/site-packages/pydantic2ts/cli/script.py", line 68, in is_concrete_pydantic_model
    elif GenericModel and issubclass(obj, GenericModel):
  File "/Users/user/miniforge3/envs/py310/lib/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

Note that CustomType is not even used in the PydanticModel. Still, if I comment out the definition of CustomType, the error disappears.

@H-Plus-Time
Copy link

CustomType is (since version 3.9 at least) actually an instance of type GenericAlias. Despite it being the first argument (which is meant to work properly), calling issubclass with anything based on ABCMeta (which BaseModel and GenericModel are) results in a call to _abc_subclasscheck with the subclass last (which must not be a generic).

The pydantic maintainers encountered this early 2021 and fixed it in their pydantic.utils.lenient_issubclass function pydantic/pydantic#2399. Swapping out the two calls to issubclass in is_concrete_pydantic_model to that utility function solves it.

@EricWebsmith
Copy link

I modified the function to

def is_concrete_pydantic_model(obj) -> bool:
    """
    Return true if an object is a concrete subclass of pydantic's BaseModel.
    'concrete' meaning that it's not a GenericModel.
    """
    return isinstance(obj, ModelMetaclass)

and I can generate typescript.

Eerovil added a commit to Eerovil/pydantic-to-typescript that referenced this issue Jun 9, 2023
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

No branches or pull requests

3 participants