mode.utils.objects
Object utilities.
InvalidAnnotation
Bases: Exception
Raised by annotations
when encountering an invalid type.
Source code in mode/utils/objects.py
140 141 |
|
KeywordReduce
Mixin class for objects that can be "pickled".
"Pickled" means the object can be serialized using the Python binary
serializer -- the pickle
module.
Python objects are made pickleable through defining the __reduce__
method, that returns a tuple of:
(restore_function, function_starargs)
:
class X:
def __init__(self, arg1, kw1=None):
self.arg1 = arg1
self.kw1 = kw1
def __reduce__(self) -> Tuple[Callable, Tuple[Any, ...]]:
return type(self), (self.arg1, self.kw1)
This is tedious since this means you cannot accept **kwargs
in the
constructor, so what we do is define a __reduce_keywords__
argument that returns a dict instead:
class X:
def __init__(self, arg1, kw1=None):
self.arg1 = arg1
self.kw1 = kw1
def __reduce_keywords__(self) -> Mapping[str, Any]:
return {
'arg1': self.arg1,
'kw1': self.kw1,
}
Source code in mode/utils/objects.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
Unordered
Bases: Generic[_T]
Shield object from being ordered in heapq/__le__
/etc.
Source code in mode/utils/objects.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
cached_property
Bases: Generic[RT]
Cached property.
A property descriptor that caches the return value of the get function.
Examples:
@cached_property
def connection(self):
return Connection()
@connection.setter # Prepares stored value
def connection(self, value):
if value is None:
raise TypeError('Connection must be a connection')
return value
@connection.deleter
def connection(self, value):
# Additional action to do at del(self.attr)
if value is not None:
print(f'Connection {value!r} deleted')
Source code in mode/utils/objects.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 |
|
annotations(cls, *, stop=object, invalid_types=None, alias_types=None, skip_classvar=False, globalns=None, localns=None)
Get class field definition in MRO order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type
|
Class to get field information from. |
required |
stop |
Type
|
Base class to stop at (default is |
object
|
invalid_types |
Optional[Set]
|
Set of types that if encountered should raise
:exc: |
None
|
alias_types |
Optional[Mapping]
|
Mapping of original type to replacement type. |
None
|
skip_classvar |
bool
|
Skip attributes annotated with
|
False
|
globalns |
Optional[Dict[str, Any]]
|
Global namespace to use when evaluating forward
references (see |
None
|
localns |
Optional[Dict[str, Any]]
|
Local namespace to use when evaluating forward
references (see |
None
|
Returns:
Type | Description |
---|---|
Tuple[FieldMapping, DefaultsMapping]
|
Tuple[FieldMapping, DefaultsMapping]: Tuple with two dictionaries, the first containing a map of field names to their types, the second containing a map of field names to their default value. If a field is not in the second map, it means the field is required. |
Raises:
Type | Description |
---|---|
InvalidAnnotation
|
if a list of invalid types are provided and an invalid type is encountered. |
Examples:
>>> class Point:
... x: float
... y: float
>>> class 3DPoint(Point):
... z: float = 0.0
>>> fields, defaults = annotations(3DPoint)
>>> fields
{'x': float, 'y': float, 'z': 'float'}
>>> defaults
{'z': 0.0}
Source code in mode/utils/objects.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
canoname(obj, *, main_name=None)
Get qualname of obj, trying to resolve the real name of __main__
.
Source code in mode/utils/objects.py
228 229 230 231 232 233 234 |
|
canonshortname(obj, *, main_name=None)
Get non-qualified name of obj, resolve real name of __main__
.
Source code in mode/utils/objects.py
237 238 239 240 241 242 243 |
|
eval_type(typ, globalns=None, localns=None, invalid_types=None, alias_types=None)
Convert (possible) string annotation to actual type.
Examples:
>>> eval_type('List[int]') == typing.List[int]
>>> eval_type('list[int]') == list[int]
Source code in mode/utils/objects.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
guess_polymorphic_type(typ, *, set_types=SET_TYPES, list_types=LIST_TYPES, tuple_types=TUPLE_TYPES, dict_types=DICT_TYPES)
Try to find the polymorphic and concrete type of an abstract type.
Returns tuple of (polymorphic_type, concrete_type)
.
Examples:
>>> guess_polymorphic_type(List[int])
(list, int)
>>> guess_polymorphic_type(Optional[List[int]])
(list, int)
>>> guess_polymorphic_type(MutableMapping[int, str])
(dict, str)
Source code in mode/utils/objects.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
|
iter_mro_reversed(cls, stop)
Iterate over superclasses, in reverse Method Resolution Order.
The stop argument specifies a base class that when seen will stop iterating (well actually start, since this is in reverse, see Example for demonstration).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type
|
Target class. |
required |
stop |
Type
|
A base class in which we stop iteration. |
required |
Notes
The last item produced will be the class itself (cls
).
Examples:
>>> class A: ...
>>> class B(A): ...
>>> class C(B): ...
>>> list(iter_mro_reverse(C, object))
[A, B, C]
>>> list(iter_mro_reverse(C, A))
[B, C]
Yields:
Type | Description |
---|---|
Iterable[Type]
|
Iterable[Type]: every class. |
Source code in mode/utils/objects.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
label(s)
Return the name of an object as string.
Source code in mode/utils/objects.py
577 578 579 |
|
qualname(obj)
Get object qualified name.
Source code in mode/utils/objects.py
213 214 215 216 217 218 |
|
shortlabel(s)
Return the shortened name of an object as string.
Source code in mode/utils/objects.py
582 583 584 |
|
shortname(obj)
Get object name (non-qualified).
Source code in mode/utils/objects.py
221 222 223 224 225 |
|