| 1 |
# |
| 2 |
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license |
| 3 |
# |
| 4 |
|
| 5 |
import weakref |
| 6 |
|
| 7 |
|
| 8 |
_cache = weakref.WeakValueDictionary() |
| 9 |
|
| 10 |
|
| 11 |
class ListMeta(type): |
| 12 |
def __getitem__(cls, key): |
| 13 |
if not isinstance(key, type): |
| 14 |
raise TypeError("Parameters to generic types must be types. " |
| 15 |
"Got {!r}.".format(key)) |
| 16 |
|
| 17 |
t = ListMeta( |
| 18 |
cls.__name__, |
| 19 |
cls.__bases__, |
| 20 |
{ |
| 21 |
'__parameters__': (key,), |
| 22 |
'__init__': cls.__init__, |
| 23 |
} |
| 24 |
) |
| 25 |
|
| 26 |
return _cache.get(key, t) |
| 27 |
|
| 28 |
|
| 29 |
class List(list, metaclass=ListMeta): |
| 30 |
__parameters__ = () |
| 31 |
|
| 32 |
def __init__(self, *_args, **_kwargs): |
| 33 |
raise TypeError("Type List cannot be instantiated; use list() instead") |