44# NOTE TO IMPLEMENTORS: A future version of this file is intended to be code-generated instead of
55# manually maintained. The makeup of this file is intentionally highly formulaic in order to
66# facilitate a smooth transition to automatically-generated data structures.
7+ import warnings
78
89from dataclasses import dataclass
910from enum import Enum
1011from io import StringIO
11- from typing import Callable , Optional , Sequence , Union
12+ from typing import Callable , NewType , Optional , Sequence , Tuple , Union
1213
1314from ._base import MISSING , T
14- from ..model .types import ModuleRef , PackageId , ValueReference , TypeReference as TypeConName , TypeReference , TypeSynName
1515from ..util .typing import safe_cast
1616
1717
@@ -22,7 +22,9 @@ class Unit:
2222UNIT = Unit ()
2323
2424
25- # PackageRef
25+ # Reference to a package via a package identifier. The identifier is the ascii7
26+ # lowercase hex-encoded hash of the package contents found in the DAML LF Archive.
27+ PackageRef = NewType ('PackageRef' , str )
2628
2729
2830class DottedName :
@@ -33,12 +35,185 @@ class DottedName:
3335 def __init__ (self , segments : Sequence [str ] = ()):
3436 object .__setattr__ (self , 'segments' , tuple (segments ))
3537
38+ def __str__ (self ):
39+ return '.' .join (self .segments )
40+
41+ def __eq__ (self , other ):
42+ return isinstance (other , DottedName ) and self .segments == other .segments
43+
44+ def __hash__ (self ):
45+ return hash (self .segments )
46+
47+
48+ class ModuleRef :
49+ """
50+ A reference to a module.
51+
52+ In dazl 7.0.0, ModuleRef will become a `NewType(str)`, so making assumptions about the structure
53+ of this type should be avoided, and accessor methods should be instead used for callers that
54+ care about the structure of these names.
55+ """
56+ __slots__ = '_package_id' , '_module_name'
57+
58+ def __init__ (self , package_id : 'PackageRef' , module_name : 'DottedName' ):
59+ self ._package_id = PackageRef (safe_cast (str , package_id ))
60+ self ._module_name = safe_cast (DottedName , module_name )
61+
62+ @property
63+ def package_id (self ) -> 'PackageRef' :
64+ warnings .warn (
65+ "Do not use ModuleRef.package_id; use package_ref(...) instead." , DeprecationWarning )
66+ return self ._package_id
67+
68+ @property
69+ def module_name (self ) -> 'DottedName' :
70+ warnings .warn (
71+ "Do not use ModuleRef.module_name; use module_name(...) instead." , DeprecationWarning )
72+ return self ._module_name
73+
74+ def __eq__ (self , other ):
75+ return isinstance (other , ModuleRef ) and \
76+ self ._package_id == other ._package_id and \
77+ self ._module_name == other ._module_name
78+
79+ def __lt__ (self , other ):
80+ return self ._package_id < other .package_id or \
81+ (self ._package_id == other .package_id and self ._module_name < other .module_name )
82+
83+ def __le__ (self , other ):
84+ return self ._package_id < other .package_id or \
85+ (self ._package_id == other .package_id and self ._module_name <= other .module_name )
86+
87+ def __gt__ (self , other ):
88+ return self ._package_id > other .package_id or \
89+ (self ._package_id == other .package_id and self ._module_name > other .module_name )
90+
91+ def __ge__ (self , other ):
92+ return self ._package_id > other .package_id or \
93+ (self ._package_id == other .package_id and self ._module_name >= other .module_name )
94+
95+ def __hash__ (self ):
96+ return hash (self ._package_id ) ^ hash (self ._module_name )
97+
98+ def __str__ (self ):
99+ return f'{ self ._package_id } :{ self ._module_name } '
100+
101+ def __repr__ (self ):
102+ return f'ModuleRef(package_id={ self ._package_id !r} , ' \
103+ f'module_name={ self ._module_name } )'
104+
105+
106+ class _Name :
107+ """
108+ A reference by name to another object.
109+
110+ This implementation powers all of a TypeConName, TypeSynName, and ValName.
111+
112+ In dazl 7.0.0, these will become `NewType(str)`, so making assumptions about the structure of
113+ this type should be avoided, and accessor methods should be instead used for callers that care
114+ about the structure of these names.
115+ """
116+
117+ __slots__ = '_module' , '_name'
118+
119+ def __init__ (self , module : 'ModuleRef' , name : 'Sequence[str]' ):
120+ from collections .abc import Collection
121+ if not isinstance (name , Collection ):
122+ raise TypeError (f'Tuple of strings required here (got { name !r} instead)' )
123+
124+ self ._module = safe_cast (ModuleRef , module )
125+ self ._name = tuple (name ) # type: Tuple[str, ...]
126+
127+ @property
128+ def module (self ) -> 'ModuleRef' :
129+ warnings .warn (
130+ "Do not use Name.module; you can use module_ref(...) instead." ,
131+ DeprecationWarning )
132+ return self ._module
133+
134+ @property
135+ def name (self ) -> 'Sequence[str]' :
136+ warnings .warn (
137+ "Do not use Name.name; you can use module_local_name(...) instead." ,
138+ DeprecationWarning )
139+ return self ._name
140+
141+ @property
142+ def full_name (self ) -> str :
143+ from .util import module_name
144+ warnings .warn (
145+ "Do not use Name.full_name; this format is no longer used, so it has no replacement." ,
146+ DeprecationWarning )
147+ return f"{ module_name (self )} .{ '.' .join (self ._name )} "
148+
149+ @property
150+ def full_name_unambiguous (self ):
151+ from .util import package_local_name
152+ warnings .warn (
153+ "Do not use Name.full_name_unambiguous; use package_local_name(...) instead." ,
154+ DeprecationWarning )
155+ return package_local_name (self )
156+
157+ def __eq__ (self , other ):
158+ return isinstance (other , type (self )) and \
159+ self ._module == other ._module and self ._name == other ._name
160+
161+ def __ne__ (self , other ):
162+ return not isinstance (other , type (self )) or \
163+ self ._module != other ._module or self ._name != other ._name
164+
165+ def __lt__ (self , other ):
166+ if not isinstance (other , _Name ):
167+ raise TypeError ("must compare Name to other names" )
168+
169+ return self ._module < other ._module or \
170+ (self ._module == other ._module and self ._name < other ._name )
171+
172+ def __le__ (self , other ):
173+ if not isinstance (other , _Name ):
174+ raise TypeError ("must compare Name to other names" )
175+ return self ._module <= other ._module or \
176+ (self ._module == other ._module and self ._name <= other ._name )
177+
178+ def __gt__ (self , other ):
179+ if not isinstance (other , _Name ):
180+ raise TypeError ("must compare Name to other names" )
181+ return self ._module > other ._module or \
182+ (self ._module == other ._module and self ._name > other ._name )
183+
184+ def __ge__ (self , other ):
185+ if not isinstance (other , _Name ):
186+ raise TypeError ("must compare Name to other names" )
187+
188+ return self ._module >= other ._module or \
189+ (self ._module == other ._module and self ._name >= other ._name )
190+
191+ def __hash__ (self ):
192+ return hash (self ._module ) ^ hash (self ._name )
193+
194+ def __str__ (self ):
195+ return f"{ self ._module } :{ '.' .join (self ._name )} "
196+
197+ def __repr__ (self ):
198+ return f"{ type (self ).__name__ } ({ str (self )!r} )"
199+
200+
201+ class TypeConName (_Name ):
202+ """
203+ A reference to a type constructor.
204+ """
205+
36206
37- # ModuleRef
38- # TypeConName
207+ class TypeSynName (_Name ):
208+ """
209+ A reference to a type synonym.
210+ """
39211
40212
41- ValName = ValueReference
213+ class ValName (_Name ):
214+ """
215+ A reference to a value.
216+ """
42217
43218
44219@dataclass (frozen = True )
@@ -162,10 +337,10 @@ class Var:
162337 args : 'Sequence[Type]'
163338
164339 class Con :
165- tycon : 'TypeReference '
340+ tycon : 'TypeConName '
166341 args : 'Sequence[Type]'
167342
168- def __init__ (self , tycon : 'TypeReference ' , args : 'Sequence[Type]' ):
343+ def __init__ (self , tycon : 'TypeConName ' , args : 'Sequence[Type]' ):
169344 self .tycon = tycon
170345 self .args = tuple (args )
171346
@@ -413,7 +588,7 @@ def numeric(self) -> 'Optional[str]':
413588 Serialization of number with precision 38 and scale between 0 and 37
414589
415590 Must be a string that matched
416- `-?([0-1]\d*|0)\.( \d*)
591+ `-?([0-1]\\ d*|0)\\ .( \ \ d*)
417592
418593 The number of decimal digits indicate the scale of the number.
419594 """
@@ -504,7 +679,7 @@ def __init__(self, tycon: 'Type.Con', variant_con: str, variant_arg: 'Expr'):
504679
505680 @dataclass (frozen = True )
506681 class EnumCon :
507- tycon : 'TypeReference ' # Always fully applied
682+ tycon : 'TypeConName ' # Always fully applied
508683 enum_con : str
509684
510685 @dataclass (frozen = True )
@@ -991,7 +1166,7 @@ def __init__(self, con, variant, binder):
9911166
9921167 @dataclass (frozen = True )
9931168 class Enum :
994- con : TypeReference
1169+ con : TypeConName
9951170 constructor : str
9961171
9971172 @dataclass (frozen = True )
@@ -1858,5 +2033,5 @@ class PackageMetadata:
18582033
18592034@dataclass (frozen = True )
18602035class Archive :
1861- hash : 'PackageId '
2036+ hash : 'PackageRef '
18622037 package : 'Package'
0 commit comments