1414 Callable ,
1515 ClassVar ,
1616 Concatenate ,
17- Hashable ,
1817 Literal ,
1918 ParamSpec ,
2019 Protocol ,
2423)
2524
2625from pydantic import Field , GetCoreSchemaHandler
27- from pydantic .main import BaseModel as PydanticBase
2826from pydantic_core import CoreSchema
2927from pydantic_core .core_schema import tagged_union_schema
3028
31- from algobattle .docker_util import (
29+ from algobattle .program import (
3230 Generator ,
3331 ProgramRunInfo ,
3432 ProgramUi ,
@@ -229,13 +227,13 @@ class Config(BaseModel):
229227 :meth:`Battle.run` method with its fields set accordingly.
230228 """
231229
232- type : str = "Iterated"
230+ type : str
233231 """Type of battle that will be used."""
234232
235233 @classmethod
236- def __get_pydantic_core_schema__ (cls , source : Type [ PydanticBase ] , handler : GetCoreSchemaHandler ) -> CoreSchema :
234+ def __get_pydantic_core_schema__ (cls , source : Type , handler : GetCoreSchemaHandler ) -> CoreSchema :
237235 # there's two bugs we need to catch:
238- # 1. this function is called during the pydantic BaseModel metaclass's __new__, so the Battle class
236+ # 1. this function is called during the pydantic BaseModel metaclass's __new__, so the BattleConfig class
239237 # won't be ready at that point and be missing in the namespace
240238 # 2. pydantic uses the core schema to build child classes core schema. for them we want to behave like a
241239 # normal model, only our own schema gets modified
@@ -244,17 +242,19 @@ def __get_pydantic_core_schema__(cls, source: Type[PydanticBase], handler: GetCo
244242 return handler (source )
245243 except NameError :
246244 return handler (source )
247- battle_classes = Battle .all ()
248- match len (battle_classes ):
245+ match len (Battle ._battle_types ):
249246 case 0 :
250247 return handler (source )
251248 case 1 :
252- return handler (next (iter (battle_classes . values ())). Config )
249+ return handler (next (iter (Battle . _battle_types . values ())))
253250 case _:
254- choices : dict [Hashable , CoreSchema ] = {
255- name : handler (sublass .Config ) for name , sublass in battle_classes .items ()
256- }
257- return tagged_union_schema (choices = choices , discriminator = "type" )
251+ return tagged_union_schema (
252+ choices = {
253+ battle .Config .model_fields ["type" ].default : battle .Config .__pydantic_core_schema__
254+ for battle in Battle ._battle_types .values ()
255+ },
256+ discriminator = "type" ,
257+ )
258258
259259 class UiData (BaseModel ):
260260 """Object containing custom diplay data.
0 commit comments