Skip to main content

Create graphene ObjectType based on the type annotations.

Project description

graphanno

Use Python 3.6+ type annotations to generate the graphene ObjectType classes.

Installation

You can install the project via pip by running:

pip install graphanno

Installation from source:

python setup.py install

Usage

The @graphanno.graph_annotations decorator works only with classes that contains type annotations. If there are no type annotations within the decorator target, NoAnnotationsError exception is raised. Arguments without annotations will be ignored.

To create the graphene.ObjectType object, you can just decorate your class with @graphanno.graph_annotations.

It is recommended to subclass graphanno.ObjectType to provide hints for IDE's. This is not mandatory, the @graphanno.graph_annotations will replace your decorated class with the ObjectType subclass anyway.

import graphene
import graphanno

# the class below...
@graphanno.graph_annotations
class Graphanno(graphanno.ObjectType):
    value: str

# ... is equivalent to:
class Graphene(graphene.ObjectType):
    value = graphene.String()

If you still need your class with type annotations, set __model__ variable to annotated class within the decorated one:

import graphene
import graphanno

class Annotated: # this class will be still available later
    value: str

# the class below...
@graphanno.graph_annotations
class Graphanno(graphanno.ObjectType): 
    """
    This class can inherit from graphene.ObjectType already, 
    but it won't change the @graph_annotations behavior.
    """
    __model__ = Annotated

# ... is equivalent to:
class Graphene(graphene.ObjectType):
    value = graphene.String()

If another class with the same name will be decorated, the SchemaClashError exception will be raised.

Additional parameters

  • __excluded_fields__ (tuple): names of the fields that will be excluded from schema. Private attributes are always excluded.
  • __ignore_unsupported__ (bool): do not raise an exception for unsupported annotations. Default False.

Supported annotations

The class decorated with @graph_annotations can use type annotations listed below.

  • str
  • int
  • float
  • bool
  • datetime.datetime
  • datetime.date
  • datetime.time
  • typing.List
  • custom classes with supported type annotations

Using unsupported annotations will raise the UnsupportedAnnotationError exception. To ignore this exception set __ignore_unsupported__ to True in the decorated class.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphanno-1.3.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

graphanno-1.3.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file graphanno-1.3.1.tar.gz.

File metadata

  • Download URL: graphanno-1.3.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for graphanno-1.3.1.tar.gz
Algorithm Hash digest
SHA256 3b12e3ad53f987065c6f4d29de607aeb2b0054cdae3cc75f617fa4b0fe6f2042
MD5 104ca6e7396297211d17517d9058b21d
BLAKE2b-256 7178522405ba9631f814c6817c5bab35a42b6337a0ddd7f6d16db17ecf383c68

See more details on using hashes here.

File details

Details for the file graphanno-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: graphanno-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for graphanno-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83ad805b399e76034fb95ddb24405bbc0a587c06351b396a9101fb454c20489d
MD5 c3a4645e01c34f84eff88d5bfbc2ad64
BLAKE2b-256 5c432a265a0675ca2e06822cb1e044f67d453e572d018041c9aee83818fda871

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page