oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.17k stars 101 forks source link

Version 24 - Pydantic error #373

Closed ghost closed 6 months ago

ghost commented 6 months ago

Hello,

Thanks again for your precedent answer,

I tried to use the version 24.0 for the patch of pydantic-core,

But there is an error with the simple code :

import re
import httpx
import logging
import ipaddress
import traceback
from datetime import datetime
from typing import Dict, List, Union
from pydantic import BaseModel, field_validator

class Test(BaseModel):
    name: str = ""
    test: List[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]] = []

    @field_validator("test")
    @classmethod
    def validate_cidrs(cls, value):
        return [ipaddress.ip_network(v, strict=False) for v in value]

This script works with python and pydantic (pydantic==2.4.2, pydantic_core==2.10.1) but returns an error with graalpy and patched pydantic :

(.venvGraalPy2) jf@MacBook-Pro-de-JF engine % graalpy error.py
Traceback (most recent call last):
  File "/Users/usertest/Desktop/test/test-project/src/main/java/com/test/testmanager/engine/error.py", line 10, in <module>
    class Test(BaseModel):
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 184, in __new__
    complete_model_class(
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 481, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/main.py", line 576, in __get_pydantic_core_schema__
    return __handler(__source)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(__source_type)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 452, in generate_schema
    schema = self._generate_schema(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 684, in _generate_schema
    schema = self._post_process_generated_schema(self._generate_schema_inner(obj))
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 706, in _generate_schema_inner
    return self._model_schema(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 525, in _model_schema
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 525, in <dictcomp>
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 870, in _generate_md_field_schema
    common_field = self._common_field_schema(name, field_info, decorators)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 923, in _common_field_schema
    schema = self._apply_annotations(
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1592, in _apply_annotations
    schema = get_inner_schema(source_type)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(__source_type)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1682, in new_handler
    schema = metadata_get_schema(source, get_inner_schema)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_std_types_schema.py", line 315, in __get_pydantic_core_schema__
    items_schema = handler.generate_schema(self.item_source_type)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 96, in generate_schema
    return self._generate_schema.generate_schema(__source_type)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 452, in generate_schema
    schema = self._generate_schema(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 684, in _generate_schema
    schema = self._post_process_generated_schema(self._generate_schema_inner(obj))
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 711, in _generate_schema_inner
    return self.match_type(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 794, in match_type
    return self._match_generic_type(obj, origin)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 818, in _match_generic_type
    return self._union_schema(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1006, in _union_schema
    choices.append(self.generate_schema(arg))
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 452, in generate_schema
    schema = self._generate_schema(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 684, in _generate_schema
    schema = self._post_process_generated_schema(self._generate_schema_inner(obj))
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 711, in _generate_schema_inner
    return self.match_type(obj)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 790, in match_type
    return self._apply_annotations(source_type, annotations)
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1563, in _apply_annotations
    annotations = list(_known_annotated_metadata.expand_grouped_metadata(annotations))
  File "/Users/usertest/Desktop/test/test-project/src/main/resources/python/.venvGraalPy2/lib/python3.10/site-packages/pydantic/_internal/_known_annotated_metadata.py", line 134, in expand_grouped_metadata
    if isinstance(annotation, at.GroupedMetadata):
  File "/Users/usertest/Downloads/graalpy-community-24.0.0-dev-macos-amd64/lib/python3.10/typing.py", line 1512, in __instancecheck__
    return super().__instancecheck__(instance)
  File "/Users/usertest/Downloads/graalpy-community-24.0.0-dev-macos-amd64/lib/python3.10/_py_abc.py", line 114, in __instancecheck__
    return cls.__subclasscheck__(subclass)
  File "/Users/usertest/Downloads/graalpy-community-24.0.0-dev-macos-amd64/lib/python3.10/_py_abc.py", line 132, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
  File "/Users/usertest/Downloads/graalpy-community-24.0.0-dev-macos-amd64/lib/python3.10/typing.py", line 1570, in _proto_hook
    raise TypeError("Protocols with non-method members"
TypeError: Protocols with non-method members don't support issubclass()

Another example of code maybe even more easier raising the same error is there: https://docs.pydantic.dev/latest/concepts/validators/#special-types

from typing import List

from pydantic import BaseModel, InstanceOf, ValidationError

class Fruit:
    def __repr__(self):
        return self.__class__.__name__

class Banana(Fruit):
    ...

class Apple(Fruit):
    ...

class Basket(BaseModel):
    fruits: List[InstanceOf[Fruit]]

and this code too:

from typing import List

from pydantic import BaseModel, SkipValidation

class Model(BaseModel):
    names: List[SkipValidation[str]]

Have you an idea why it throws this error? And is there any patch we can apply?

Thanks again a lot for your amazing work on graalpython :)

F.N.

msimacek commented 6 months ago

Hi @fakeNews-jpg, thank you for your interest in GraalPy. I've already fixed this bug recently in db21f553ad6e2b66356872609dd98df8dedcce53. Unfortunately, because of the CI issues, that fix didn't yet make it into the snapshot build. Since my fix is just a one-line change in a python file, you could apply it yourself - just apply the change in the commit to lib/python3.10/typing.py

ghost commented 6 months ago

Effectively it works with this patch! :)

Thanks a lot

msimacek commented 5 months ago

@fakeNews-jpg, FYI, there is a dev build finally available: https://github.com/graalvm/graalvm-ce-dev-builds/releases/