mode.utils.objects
Object utilities.
InvalidAnnotation
Bases: Exception
Raised by annotations when encountering an invalid type.
Source code in mode/utils/objects.py
162 163 | |
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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
Unordered
Bases: Generic[_T]
Shield object from being ordered in heapq/__le__/etc.
Source code in mode/utils/objects.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
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
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |
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
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
canoname(obj, *, main_name=None)
Get qualname of obj, trying to resolve the real name of __main__.
Source code in mode/utils/objects.py
250 251 252 253 254 255 256 | |
canonshortname(obj, *, main_name=None)
Get non-qualified name of obj, resolve real name of __main__.
Source code in mode/utils/objects.py
259 260 261 262 263 264 265 | |
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
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
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
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
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
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
label(s)
Return the name of an object as string.
Source code in mode/utils/objects.py
640 641 642 | |
qualname(obj)
Get object qualified name.
Source code in mode/utils/objects.py
235 236 237 238 239 240 | |
shortlabel(s)
Return the shortened name of an object as string.
Source code in mode/utils/objects.py
645 646 647 | |
shortname(obj)
Get object name (non-qualified).
Source code in mode/utils/objects.py
243 244 245 246 247 | |