77from typing import (
88 TYPE_CHECKING ,
99 Any ,
10+ ClassVar ,
1011 Literal ,
1112 TypeGuard ,
1213 TypeVar ,
4041@struct
4142class TypeRef :
4243 id : str
43- name : str
44-
45- def __str__ (self ) -> str :
46- return self .name
44+ name : TypeName
4745
4846
4947@sobject
@@ -64,9 +62,6 @@ class Type(SchemaObject, abc.ABC):
6462 def kind (self ) -> TypeKind :
6563 return kind_of_type (self )
6664
67- def __str__ (self ) -> str :
68- return self .name
69-
7065 @functools .cached_property
7166 def generic (self ) -> bool :
7267 sp = self .schemapath
@@ -76,8 +71,7 @@ def generic(self) -> bool:
7671 and sp .name .startswith ("any" )
7772 )
7873
79- @functools .cached_property
80- def type_name (self ) -> TypeName :
74+ def get_type_name (self , schema : Schema ) -> TypeName :
8175 return self .schemapath
8276
8377 def assignable_from (
@@ -318,6 +312,8 @@ def mutable(self, schema: Schema) -> bool:
318312
319313
320314class CollectionType (Type ):
315+ collection_name : ClassVar [str ]
316+
321317 if TYPE_CHECKING :
322318 _mutable_cached : bool | None
323319
@@ -339,7 +335,12 @@ def mutable(self, schema: Schema) -> bool:
339335 object .__setattr__ (self , "_mutable_cached" , mut ) # noqa: PLC2801
340336 return mut
341337
342- def get_id_and_name (self , element_type : Type ) -> tuple [str , str ]:
338+ def get_id_and_name (
339+ self ,
340+ element_type : Type ,
341+ * ,
342+ schema : Mapping [str , Type ],
343+ ) -> tuple [str , str ]:
343344 cls = type (self )
344345 raise NotImplementedError (f"{ cls .__qualname__ } .get_id_and_name()" )
345346
@@ -349,14 +350,11 @@ def get_element_type(self, schema: Schema) -> Type:
349350 else :
350351 return schema [self ._element_type_id ]
351352
352- @functools .cached_property
353- def type_name (self ) -> TypeName :
354- if self .element_type is None :
355- return self .schemapath
356- else :
357- return ParametricTypeName (
358- self .schemapath , [self .element_type .type_name ]
359- )
353+ def get_type_name (self , schema : Schema ) -> TypeName :
354+ return ParametricTypeName (
355+ self .schemapath ,
356+ [self .get_element_type (schema ).get_type_name (schema )],
357+ )
360358
361359 @functools .cached_property
362360 def _element_type_id (self ) -> str :
@@ -386,11 +384,10 @@ def specialize(
386384 element_spec ,
387385 schema = schema ,
388386 )
389- id_ , name = self .get_id_and_name (element_type )
387+ id_ , _ = self .get_id_and_name (element_type , schema = schema )
390388 return dataclasses .replace (
391389 self ,
392390 id = id_ ,
393- name = name ,
394391 element_type = element_type ,
395392 )
396393
@@ -443,7 +440,10 @@ def mutable(self, schema: Schema) -> bool:
443440 return mut
444441
445442 def get_id_and_name (
446- self , element_types : tuple [Type , ...]
443+ self ,
444+ element_types : tuple [Type , ...],
445+ * ,
446+ schema : Mapping [str , Type ],
447447 ) -> tuple [str , str ]:
448448 cls = type (self )
449449 raise NotImplementedError (f"{ cls .__qualname__ } .get_id_and_name()" )
@@ -454,18 +454,14 @@ def get_element_types(self, schema: Schema) -> tuple[Type, ...]:
454454 else :
455455 return tuple (schema [el_tid ] for el_tid in self ._element_type_ids )
456456
457- @functools .cached_property
458- def type_name (self ) -> TypeName :
459- if self .element_types is None :
460- return self .schemapath
461- else :
462- return ParametricTypeName (
463- self .schemapath ,
464- [
465- element_type .type_name
466- for element_type in self .element_types
467- ],
468- )
457+ def get_type_name (self , schema : Schema ) -> TypeName :
458+ return ParametricTypeName (
459+ self .schemapath ,
460+ [
461+ element_type .get_type_name (schema )
462+ for element_type in self .get_element_types (schema )
463+ ],
464+ )
469465
470466 @functools .cached_property
471467 def _element_type_ids (self ) -> list [str ]:
@@ -507,11 +503,10 @@ def specialize(
507503 element_types .append (element_type )
508504
509505 element_types_tup = tuple (element_types )
510- id_ , name = self .get_id_and_name (element_types_tup )
506+ id_ , _ = self .get_id_and_name (element_types_tup , schema = schema )
511507 return dataclasses .replace (
512508 self ,
513509 id = id_ ,
514- name = name ,
515510 element_types = element_types_tup ,
516511 )
517512
@@ -563,6 +558,8 @@ def _assignable_from(
563558
564559@struct
565560class ArrayType (HomogeneousCollectionType ):
561+ collection_name : ClassVar [str ] = "std::array"
562+
566563 array_element_id : str
567564
568565 is_object : Literal [False ]
@@ -582,13 +579,22 @@ def kind(self) -> Literal[TypeKind.Array]:
582579 def _element_type_id (self ) -> str :
583580 return self .array_element_id
584581
585- def get_id_and_name (self , element_type : Type ) -> tuple [str , str ]:
586- id_ , _ = _edgeql .get_array_type_id_and_name (element_type .type_name )
582+ def get_id_and_name (
583+ self ,
584+ element_type : Type ,
585+ * ,
586+ schema : Mapping [str , Type ],
587+ ) -> tuple [str , str ]:
588+ id_ , _ = _edgeql .get_array_type_id_and_name (
589+ element_type .get_type_name (schema )
590+ )
587591 return str (id_ ), "std::array"
588592
589593
590594@struct
591595class RangeType (HomogeneousCollectionType ):
596+ collection_name : ClassVar [str ] = "std::range"
597+
592598 is_object : Literal [False ]
593599 is_scalar : Literal [False ]
594600 is_array : Literal [False ]
@@ -608,13 +614,22 @@ def kind(self) -> Literal[TypeKind.Range]:
608614 def _element_type_id (self ) -> str :
609615 return self .range_element_id
610616
611- def get_id_and_name (self , element_type : Type ) -> tuple [str , str ]:
612- id_ , _ = _edgeql .get_range_type_id_and_name (element_type .type_name )
617+ def get_id_and_name (
618+ self ,
619+ element_type : Type ,
620+ * ,
621+ schema : Mapping [str , Type ],
622+ ) -> tuple [str , str ]:
623+ id_ , _ = _edgeql .get_range_type_id_and_name (
624+ element_type .get_type_name (schema )
625+ )
613626 return str (id_ ), "std::range"
614627
615628
616629@struct
617630class MultiRangeType (HomogeneousCollectionType ):
631+ collection_name : ClassVar [str ] = "std::multirange"
632+
618633 is_object : Literal [False ]
619634 is_scalar : Literal [False ]
620635 is_array : Literal [False ]
@@ -634,9 +649,14 @@ def kind(self) -> Literal[TypeKind.MultiRange]:
634649 def _element_type_id (self ) -> str :
635650 return self .multirange_element_id
636651
637- def get_id_and_name (self , element_type : Type ) -> tuple [str , str ]:
652+ def get_id_and_name (
653+ self ,
654+ element_type : Type ,
655+ * ,
656+ schema : Mapping [str , Type ],
657+ ) -> tuple [str , str ]:
638658 id_ , _ = _edgeql .get_multirange_type_id_and_name (
639- element_type .type_name
659+ element_type .get_type_name ( schema )
640660 )
641661 return str (id_ ), "std::multirange"
642662
@@ -658,6 +678,8 @@ def _element_type_ids(self) -> list[str]:
658678
659679@struct
660680class TupleType (_TupleType ):
681+ collection_name : ClassVar [str ] = "std::tuple"
682+
661683 is_object : Literal [False ]
662684 is_scalar : Literal [False ]
663685 is_array : Literal [False ]
@@ -672,16 +694,21 @@ def kind(self) -> Literal[TypeKind.Tuple]:
672694 return TypeKind .Tuple
673695
674696 def get_id_and_name (
675- self , element_types : tuple [Type , ...]
697+ self ,
698+ element_types : tuple [Type , ...],
699+ * ,
700+ schema : Mapping [str , Type ],
676701 ) -> tuple [str , str ]:
677702 id_ , _ = _edgeql .get_tuple_type_id_and_name (
678- [el .type_name for el in element_types ]
703+ [el .get_type_name ( schema ) for el in element_types ]
679704 )
680705 return str (id_ ), "std::tuple"
681706
682707
683708@struct
684709class NamedTupleType (_TupleType ):
710+ collection_name : ClassVar [str ] = "std::tuple"
711+
685712 is_object : Literal [False ]
686713 is_scalar : Literal [False ]
687714 is_array : Literal [False ]
@@ -696,11 +723,14 @@ def kind(self) -> Literal[TypeKind.NamedTuple]:
696723 return TypeKind .NamedTuple
697724
698725 def get_id_and_name (
699- self , element_types : tuple [Type , ...]
726+ self ,
727+ element_types : tuple [Type , ...],
728+ * ,
729+ schema : Mapping [str , Type ],
700730 ) -> tuple [str , str ]:
701731 id_ , _ = _edgeql .get_named_tuple_type_id_and_name (
702732 {
703- el .name : el_type .type_name
733+ el .name : el_type .get_type_name ( schema )
704734 for el , el_type in zip (
705735 self .tuple_elements , element_types , strict = True
706736 )
@@ -893,7 +923,7 @@ def fetch_types(
893923 cls = _kind_to_class [kind_of_type (t )]
894924 replace : dict [str , Any ] = {}
895925 if issubclass (cls , CollectionType ):
896- replace ["name" ] = _edgeql . unmangle_unqual_name ( t . name )
926+ replace ["name" ] = cls . collection_name
897927 vt = _dataclass_extras .coerce_to_dataclass (
898928 cls ,
899929 t ,
0 commit comments