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
TypeAliasType
ref KeyError
#8320
Comments
Looks to be an issue with recursive type aliases that are (or contain as union cases) BaseModels. More minimal reproductions: import typing_extensions as _te
from pydantic import BaseModel, TypeAdapter
class Div(BaseModel):
components: 'AnyComponent'
AnyComponent = _te.TypeAliasType('AnyComponent', Div)
TypeAdapter(AnyComponent)
import typing_extensions as _te
from pydantic import BaseModel, TypeAdapter, Field
class Div(BaseModel):
type: _te.Literal['Div'] = 'Div'
components: 'AnyComponent'
AnyComponent = _te.TypeAliasType('AnyComponent', _te.Annotated[Div, Field(discriminator='type')])
TypeAdapter(AnyComponent)
(Note: if you want to actually be able to instantiate the types, you'd want to change |
Changing from pydantic_core import SchemaError
def validate_core_schema(schema: CoreSchema) -> CoreSchema:
if 'PYDANTIC_SKIP_VALIDATING_CORE_SCHEMAS' in os.environ:
return schema
try:
return _validate_core_schema(schema)
except SchemaError as e:
pretty_print_core_schema(schema)
raise You can see: {
'type': 'definitions',
'schema': {
'type': 'definition-ref',
'schema_ref': '__main__.AnyComponent:4307210288'
},
'definitions': [
{
'type': 'model',
'cls': <class '__main__.Div'>,
'schema': {
'type': 'model-fields',
'fields': {
'components': {
'type': 'model-field',
'schema': {
'type': 'definition-ref',
'schema_ref': '__main__.AnyComponent:4307210288'
}
}
},
'model_name': 'Div'
},
'ref': '__main__.Div:4313912816'
},
{
'type': 'definition-ref',
'schema_ref': '__main__.Div:4313912816',
'ref': '__main__.AnyComponent:4307210288'
}
]
} The issue is: {
'type': 'definition-ref',
'schema_ref': '__main__.Div:4313912816',
'ref': '__main__.AnyComponent:4307210288'
} Right now, in |
I'm getting the same while trying to use a ref for AnyEvent = TypeAliasType('AnyEvent', Annotated[Union[PageEvent, GoToEvent, BackEvent, AuthEvent], Field(discriminator='type')]) here gives me the same error. |
@samuelcolvin I've pushed branches called
I didn't test extensively, but using both of those branches together seems to resolve this issue, at least it prevents the error on import and some basic validation seemed to work. I didn't check the JSON schema, etc., and I won't be surprised if we need to do a bit more work, but let me know if this seems to be on the right track. |
See https://github.com/samuelcolvin/FastUI/compare/pydantic-ref-bug?expand=1
To reproduce:
python -c 'import fastui'
Exception:
The text was updated successfully, but these errors were encountered: