2
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
4
5
+ # fmt: off
6
+
7
+ from __future__ import annotations
8
+
5
9
import functools
6
10
import os
7
11
from typing import Any
@@ -30,7 +34,7 @@ class Configuration:
30
34
enumConfig : dict [str , Any ]
31
35
dictConfig : dict [str , Any ]
32
36
unionConfig : dict [str , Any ]
33
- descriptors : list [" Descriptor" ]
37
+ descriptors : list [Descriptor ]
34
38
interfaces : dict [str , IDLInterface ]
35
39
36
40
def __init__ (self , filename : str , parseData : list [IDLInterface ]) -> None :
@@ -86,46 +90,46 @@ def __init__(self, filename: str, parseData: list[IDLInterface]) -> None:
86
90
c .isCallback () and not c .isInterface ()]
87
91
88
92
# Keep the descriptor list sorted for determinism.
89
- def cmp (x , y ) :
93
+ def cmp (x : str , y : str ) -> int :
90
94
return (x > y ) - (x < y )
91
95
self .descriptors .sort (key = functools .cmp_to_key (lambda x , y : cmp (x .name , y .name )))
92
96
93
97
def getInterface (self , ifname : str ) -> IDLInterface :
94
98
return self .interfaces [ifname ]
95
99
96
- def getDescriptors (self , ** filters ) -> list [" Descriptor" ]:
100
+ def getDescriptors (self , ** filters : IDLInterface ) -> list [Descriptor ]:
97
101
"""Gets the descriptors that match the given filters."""
98
102
curr = self .descriptors
99
103
for key , val in filters .items ():
100
104
if key == 'webIDLFile' :
101
- def getter (x : Descriptor ):
105
+ def getter (x : Descriptor ) -> str :
102
106
return x .interface .location .filename
103
107
elif key == 'hasInterfaceObject' :
104
- def getter (x ) :
108
+ def getter (x : Descriptor ) -> bool :
105
109
return x .interface .hasInterfaceObject ()
106
110
elif key == 'isCallback' :
107
- def getter (x ) :
111
+ def getter (x : Descriptor ) -> bool :
108
112
return x .interface .isCallback ()
109
113
elif key == 'isNamespace' :
110
- def getter (x ) :
114
+ def getter (x : Descriptor ) -> bool :
111
115
return x .interface .isNamespace ()
112
116
elif key == 'isJSImplemented' :
113
- def getter (x ) :
117
+ def getter (x : Descriptor ) -> bool :
114
118
return x .interface .isJSImplemented ()
115
119
elif key == 'isGlobal' :
116
- def getter (x ) :
120
+ def getter (x : Descriptor ) -> bool :
117
121
return x .isGlobal ()
118
122
elif key == 'isInline' :
119
- def getter (x ) -> bool :
123
+ def getter (x : Descriptor ) -> bool :
120
124
return x .interface .getExtendedAttribute ('Inline' ) is not None
121
125
elif key == 'isExposedConditionally' :
122
- def getter (x ) :
126
+ def getter (x : Descriptor ) -> bool :
123
127
return x .interface .isExposedConditionally ()
124
128
elif key == 'isIteratorInterface' :
125
- def getter (x ) :
129
+ def getter (x : Descriptor ) -> bool :
126
130
return x .interface .isIteratorInterface ()
127
131
else :
128
- def getter (x ) :
132
+ def getter (x : Descriptor ) -> Any :
129
133
return getattr (x , key )
130
134
curr = [x for x in curr if getter (x ) == val ]
131
135
return curr
@@ -159,7 +163,7 @@ def getDictConfig(self, name: str) -> dict[str, Any]:
159
163
def getCallbacks (self , webIDLFile : str = "" ) -> list [IDLInterface ]:
160
164
return self ._filterForFile (self .callbacks , webIDLFile = webIDLFile )
161
165
162
- def getDescriptor (self , interfaceName : str ) -> " Descriptor" :
166
+ def getDescriptor (self , interfaceName : str ) -> Descriptor :
163
167
"""
164
168
Gets the appropriate descriptor for the given interface name.
165
169
"""
@@ -172,34 +176,34 @@ def getDescriptor(self, interfaceName: str) -> "Descriptor":
172
176
+ str (len (descriptors )) + " matches" )
173
177
return descriptors [0 ]
174
178
175
- def getDescriptorProvider (self ) -> " DescriptorProvider" :
179
+ def getDescriptorProvider (self ) -> DescriptorProvider :
176
180
"""
177
181
Gets a descriptor provider that can provide descriptors as needed.
178
182
"""
179
183
return DescriptorProvider (self )
180
184
181
185
182
186
class NoSuchDescriptorError (TypeError ):
183
- def __init__ (self , str ) -> None :
187
+ def __init__ (self , str : str ) -> None :
184
188
TypeError .__init__ (self , str )
185
189
186
190
187
191
class DescriptorProvider :
188
192
"""
189
193
A way of getting descriptors for interface names
190
194
"""
191
- def __init__ (self , config ) -> None :
195
+ def __init__ (self , config : Configuration ) -> None :
192
196
self .config = config
193
197
194
- def getDescriptor (self , interfaceName : str ) -> " Descriptor" :
198
+ def getDescriptor (self , interfaceName : str ) -> Descriptor :
195
199
"""
196
200
Gets the appropriate descriptor for the given interface name given the
197
201
context of the current descriptor.
198
202
"""
199
203
return self .config .getDescriptor (interfaceName )
200
204
201
205
202
- def MemberIsLegacyUnforgeable (member : IDLAttribute | IDLMethod , descriptor : " Descriptor" ) -> bool :
206
+ def MemberIsLegacyUnforgeable (member : IDLAttribute | IDLMethod , descriptor : Descriptor ) -> bool :
203
207
return ((member .isAttr () or member .isMethod ())
204
208
and not member .isStatic ()
205
209
and (member .isLegacyUnforgeable ()
@@ -213,7 +217,7 @@ class Descriptor(DescriptorProvider):
213
217
interface : IDLInterface
214
218
uniqueImplementation : bool
215
219
216
- def __init__ (self , config , interface : IDLInterface , desc : dict [str , Any ]) -> None :
220
+ def __init__ (self , config : Configuration , interface : IDLInterface , desc : dict [str , Any ]) -> None :
217
221
DescriptorProvider .__init__ (self , config )
218
222
self .interface = interface
219
223
@@ -289,7 +293,7 @@ def __init__(self, config, interface: IDLInterface, desc: dict[str, Any]) -> Non
289
293
and any (MemberIsLegacyUnforgeable (m , self ) for m in
290
294
self .interface .members ))
291
295
292
- self .operations = {
296
+ self .operations : dict [ str , IDLMethod | None ] = {
293
297
'IndexedGetter' : None ,
294
298
'IndexedSetter' : None ,
295
299
'IndexedDeleter' : None ,
@@ -301,7 +305,7 @@ def __init__(self, config, interface: IDLInterface, desc: dict[str, Any]) -> Non
301
305
302
306
self .hasDefaultToJSON = False
303
307
304
- def addOperation (operation : str , m ) -> None :
308
+ def addOperation (operation : str , m : IDLMethod ) -> None :
305
309
if not self .operations [operation ]:
306
310
self .operations [operation ] = m
307
311
@@ -320,7 +324,7 @@ def addOperation(operation: str, m) -> None:
320
324
if not m .isMethod ():
321
325
continue
322
326
323
- def addIndexedOrNamedOperation (operation : str , m ) -> None :
327
+ def addIndexedOrNamedOperation (operation : str , m : IDLMethod ) -> None :
324
328
if not self .isGlobal ():
325
329
self .proxy = True
326
330
if m .isIndexed ():
@@ -357,8 +361,8 @@ def addIndexedOrNamedOperation(operation: str, m) -> None:
357
361
# array of extended attributes.
358
362
self .extendedAttributes = {'all' : {}, 'getterOnly' : {}, 'setterOnly' : {}}
359
363
360
- def addExtendedAttribute (attribute , config ) -> None :
361
- def add (key : str , members , attribute ) -> None :
364
+ def addExtendedAttribute (attribute : dict [ str , Any ], config : Configuration ) -> None :
365
+ def add (key : str , members : list [ str ] , attribute : dict [ str , Any ] ) -> None :
362
366
for member in members :
363
367
self .extendedAttributes [key ].setdefault (member , []).append (attribute )
364
368
@@ -405,7 +409,7 @@ def add(key: str, members, attribute) -> None:
405
409
config .maxProtoChainLength = max (config .maxProtoChainLength ,
406
410
len (self .prototypeChain ))
407
411
408
- def maybeGetSuperModule (self ):
412
+ def maybeGetSuperModule (self ) -> str | None :
409
413
"""
410
414
Returns name of super module if self is part of it
411
415
"""
@@ -416,10 +420,10 @@ def maybeGetSuperModule(self):
416
420
return filename
417
421
return None
418
422
419
- def binaryNameFor (self , name : str , isStatic : bool ):
423
+ def binaryNameFor (self , name : str , isStatic : bool ) -> str :
420
424
return self ._binaryNames .get ((name , isStatic ), name )
421
425
422
- def internalNameFor (self , name : str ):
426
+ def internalNameFor (self , name : str ) -> str :
423
427
return self ._internalNames .get (name , name )
424
428
425
429
def hasNamedPropertiesObject (self ) -> bool :
@@ -431,8 +435,8 @@ def hasNamedPropertiesObject(self) -> bool:
431
435
def supportsNamedProperties (self ) -> bool :
432
436
return self .operations ['NamedGetter' ] is not None
433
437
434
- def getExtendedAttributes (self , member , getter = False , setter = False ):
435
- def maybeAppendInfallibleToAttrs (attrs , throws ) -> None :
438
+ def getExtendedAttributes (self , member : IDLMethod , getter : bool = False , setter : bool = False ) -> list [ str ] :
439
+ def maybeAppendInfallibleToAttrs (attrs : list [ str ] , throws : bool | None ) -> None :
436
440
if throws is None :
437
441
attrs .append ("infallible" )
438
442
elif throws is True :
@@ -458,7 +462,7 @@ def maybeAppendInfallibleToAttrs(attrs, throws) -> None:
458
462
maybeAppendInfallibleToAttrs (attrs , throws )
459
463
return attrs
460
464
461
- def getParentName (self ):
465
+ def getParentName (self ) -> str | None :
462
466
parent = self .interface .parent
463
467
while parent :
464
468
if not parent .getExtendedAttribute ("Inline" ):
@@ -469,30 +473,30 @@ def getParentName(self):
469
473
def supportsIndexedProperties (self ) -> bool :
470
474
return self .operations ['IndexedGetter' ] is not None
471
475
472
- def isMaybeCrossOriginObject (self ):
476
+ def isMaybeCrossOriginObject (self ) -> bool :
473
477
# If we're isGlobal and have cross-origin members, we're a Window, and
474
478
# that's not a cross-origin object. The WindowProxy is.
475
479
return self .concrete and self .interface .hasCrossOriginMembers and not self .isGlobal ()
476
480
477
- def hasDescendants (self ):
481
+ def hasDescendants (self ) -> bool :
478
482
return (self .interface .getUserData ("hasConcreteDescendant" , False )
479
- or self .interface .getUserData ("hasProxyDescendant" , False ))
483
+ or self .interface .getUserData ("hasProxyDescendant" , False ) or False )
480
484
481
- def hasHTMLConstructor (self ):
485
+ def hasHTMLConstructor (self ) -> bool :
482
486
ctor = self .interface .ctor ()
483
- return ctor and ctor .isHTMLConstructor ()
487
+ return ( ctor and ctor .isHTMLConstructor ()) or False
484
488
485
- def shouldHaveGetConstructorObjectMethod (self ):
489
+ def shouldHaveGetConstructorObjectMethod (self ) -> bool :
486
490
assert self .interface .hasInterfaceObject ()
487
491
if self .interface .getExtendedAttribute ("Inline" ):
488
492
return False
489
493
return (self .interface .isCallback () or self .interface .isNamespace ()
490
- or self .hasDescendants () or self .hasHTMLConstructor ())
494
+ or self .hasDescendants () or self .hasHTMLConstructor () or False )
491
495
492
- def shouldCacheConstructor (self ):
496
+ def shouldCacheConstructor (self ) -> bool :
493
497
return self .hasDescendants () or self .hasHTMLConstructor ()
494
498
495
- def isExposedConditionally (self ):
499
+ def isExposedConditionally (self ) -> bool :
496
500
return self .interface .isExposedConditionally ()
497
501
498
502
def isGlobal (self ) -> bool :
@@ -507,19 +511,19 @@ def isGlobal(self) -> bool:
507
511
# Some utility methods
508
512
509
513
510
- def MakeNativeName (name ) :
514
+ def MakeNativeName (name : str ) -> str :
511
515
return name [0 ].upper () + name [1 :]
512
516
513
517
514
- def getIdlFileName (object : IDLObject ):
518
+ def getIdlFileName (object : IDLObject ) -> str :
515
519
return os .path .basename (object .location .filename ).split ('.webidl' )[0 ]
516
520
517
521
518
- def getModuleFromObject (object : IDLObject ):
522
+ def getModuleFromObject (object : IDLObject ) -> str :
519
523
return ('crate::codegen::GenericBindings::' + getIdlFileName (object ) + 'Binding' )
520
524
521
525
522
- def getTypesFromDescriptor (descriptor : Descriptor ):
526
+ def getTypesFromDescriptor (descriptor : Descriptor ) -> list [ IDLType ] :
523
527
"""
524
528
Get all argument and return types for all members of the descriptor
525
529
"""
@@ -570,7 +574,7 @@ def getUnwrappedType(type: IDLType) -> IDLType:
570
574
return type
571
575
572
576
573
- def iteratorNativeType (descriptor : Descriptor , infer : bool = False ):
577
+ def iteratorNativeType (descriptor : Descriptor , infer : bool = False ) -> str :
574
578
assert descriptor .interface .maplikeOrSetlikeOrIterable is not None
575
579
iterableDecl = descriptor .interface .maplikeOrSetlikeOrIterable
576
580
assert (iterableDecl .isIterable () and iterableDecl .isPairIterator ()) \
0 commit comments