@@ -5350,71 +5350,44 @@ def postinit(self, *, patterns: list[Pattern]) -> None:
5350
5350
5351
5351
# constants ##############################################################
5352
5352
5353
- CONST_CLS = {
5353
+ # The _proxied attribute of all container types (List, Tuple, etc.)
5354
+ # are set during bootstrapping by _astroid_bootstrapping().
5355
+ CONST_CLS : dict [type , type [NodeNG ]] = {
5354
5356
list : List ,
5355
5357
tuple : Tuple ,
5356
5358
dict : Dict ,
5357
5359
set : Set ,
5358
5360
type (None ): Const ,
5359
5361
type (NotImplemented ): Const ,
5360
5362
type (...): Const ,
5363
+ bool : Const ,
5364
+ int : Const ,
5365
+ float : Const ,
5366
+ complex : Const ,
5367
+ str : Const ,
5368
+ bytes : Const ,
5361
5369
}
5362
5370
5363
5371
5364
- def _update_const_classes ():
5365
- """update constant classes, so the keys of CONST_CLS can be reused"""
5366
- klasses = (bool , int , float , complex , str , bytes )
5367
- for kls in klasses :
5368
- CONST_CLS [kls ] = Const
5369
-
5370
-
5371
- _update_const_classes ()
5372
-
5373
-
5374
- def _two_step_initialization (cls , value ):
5375
- instance = cls ()
5376
- instance .postinit (value )
5377
- return instance
5378
-
5379
-
5380
- def _dict_initialization (cls , value ):
5381
- if isinstance (value , dict ):
5382
- value = tuple (value .items ())
5383
- return _two_step_initialization (cls , value )
5384
-
5385
-
5386
- _CONST_CLS_CONSTRUCTORS = {
5387
- List : _two_step_initialization ,
5388
- Tuple : _two_step_initialization ,
5389
- Dict : _dict_initialization ,
5390
- Set : _two_step_initialization ,
5391
- Const : lambda cls , value : cls (value ),
5392
- }
5393
-
5394
-
5395
- def const_factory (value ):
5396
- """return an astroid node for a python value"""
5397
- # XXX we should probably be stricter here and only consider stuff in
5398
- # CONST_CLS or do better treatment: in case where value is not in CONST_CLS,
5399
- # we should rather recall the builder on this value than returning an empty
5400
- # node (another option being that const_factory shouldn't be called with something
5401
- # not in CONST_CLS)
5372
+ def const_factory (value : Any ) -> List | Set | Tuple | Dict | Const | EmptyNode :
5373
+ """Return an astroid node for a python value."""
5402
5374
assert not isinstance (value , NodeNG )
5403
5375
5404
- # Hack for ignoring elements of a sequence
5405
- # or a mapping, in order to avoid transforming
5406
- # each element to an AST. This is fixed in 2.0
5407
- # and this approach is a temporary hack.
5408
- if isinstance (value , (list , set , tuple , dict )):
5409
- elts = []
5410
- else :
5411
- elts = value
5412
-
5413
- try :
5414
- initializer_cls = CONST_CLS [value .__class__ ]
5415
- initializer = _CONST_CLS_CONSTRUCTORS [initializer_cls ]
5416
- return initializer (initializer_cls , elts )
5417
- except (KeyError , AttributeError ):
5376
+ # This only handles instances of the CONST types. Any
5377
+ # subclasses get inferred as EmptyNode.
5378
+ # TODO: See if we should revisit these with the normal builder.
5379
+ if value .__class__ not in CONST_CLS :
5418
5380
node = EmptyNode ()
5419
5381
node .object = value
5420
5382
return node
5383
+
5384
+ # TODO: We pass an empty list as elements for a sequence
5385
+ # or a mapping, in order to avoid transforming
5386
+ # each element to an AST. This is fixed in 2.0
5387
+ # and this approach is a temporary hack.
5388
+ initializer_cls = CONST_CLS [value .__class__ ]
5389
+ if issubclass (initializer_cls , (Dict , List , Set , Tuple )):
5390
+ instance = initializer_cls ()
5391
+ instance .postinit ([])
5392
+ return instance
5393
+ return Const (value )
0 commit comments