File: dynamic.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (22 lines) | stat: -rw-r--r-- 675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import inspect
from functools import partial

from .mountedtype import MountedType


class Dynamic(MountedType):
    """
    A Dynamic Type let us get the type in runtime when we generate
    the schema. So we can have lazy fields.
    """

    def __init__(self, type_, with_schema=False, _creation_counter=None):
        super(Dynamic, self).__init__(_creation_counter=_creation_counter)
        assert inspect.isfunction(type_) or isinstance(type_, partial)
        self.type = type_
        self.with_schema = with_schema

    def get_type(self, schema=None):
        if schema and self.with_schema:
            return self.type(schema=schema)
        return self.type()