Initializer for dynamically created classes. Args: self: The instance of this class. Listed to make the linter hush. *args: Either a ComputedObject to be promoted to this type, or arguments to an algorithm with the same name as this class. **kwargs: Any kwargs pas
(self, *args, **kwargs)
| 482 | """Generates a dynamic API class for a given name.""" |
| 483 | |
| 484 | def init(self, *args, **kwargs): |
| 485 | """Initializer for dynamically created classes. |
| 486 | |
| 487 | Args: |
| 488 | self: The instance of this class. Listed to make the linter hush. |
| 489 | *args: Either a ComputedObject to be promoted to this type, or |
| 490 | arguments to an algorithm with the same name as this class. |
| 491 | **kwargs: Any kwargs passed to this class constructor. |
| 492 | |
| 493 | Returns: |
| 494 | The new class. |
| 495 | """ |
| 496 | a_class = globals()[name] |
| 497 | onlyOneArg = (len(args) == 1) |
| 498 | # Are we trying to cast something that's already of the right class? |
| 499 | if not (onlyOneArg and isinstance(args[0], a_class)): |
| 500 | # Decide whether to call a server-side constructor or just do a |
| 501 | # client-side cast. |
| 502 | ctor = ApiFunction.lookupInternal(name) |
| 503 | firstArgIsPrimitive = not isinstance((args or [None])[0], ComputedObject) |
| 504 | shouldUseConstructor = False |
| 505 | if ctor: |
| 506 | if not onlyOneArg: |
| 507 | # Can't client-cast multiple arguments. |
| 508 | shouldUseConstructor = True |
| 509 | elif firstArgIsPrimitive: |
| 510 | # Can't cast a primitive. |
| 511 | shouldUseConstructor = True |
| 512 | elif args[0].func != ctor: |
| 513 | # We haven't already called the constructor on this object. |
| 514 | shouldUseConstructor = True |
| 515 | |
| 516 | # Apply our decision. |
| 517 | if shouldUseConstructor and ctor: |
| 518 | # Call ctor manually to avoid having promote() called on the output. |
| 519 | promoted_args = ctor.promoteArgs(ctor.nameArgs(args, kwargs)) |
| 520 | ComputedObject.__init__(self, ctor, promoted_args) |
| 521 | else: |
| 522 | # Just cast and hope for the best. |
| 523 | if not onlyOneArg: |
| 524 | # We don't know what to do with multiple args. |
| 525 | raise EEException( |
| 526 | f'Too many arguments for ee.{name}(): {args}') |
| 527 | elif firstArgIsPrimitive: |
| 528 | # Can't cast a primitive. |
| 529 | raise EEException( |
| 530 | 'Invalid argument for ee.{}(): {}. ' |
| 531 | 'Must be a ComputedObject.'.format(name, args)) |
| 532 | |
| 533 | result = args[0] |
| 534 | ComputedObject.__init__(self, result.func, result.args, result.varName) |
| 535 | |
| 536 | properties = {'__init__': init, 'name': lambda self: name} |
| 537 | new_class = type(str(name), (ComputedObject,), properties) |
nothing calls this directly
no test coverage detected