Python Utils

This package contains dependency-free Python utility functions used throughout the codebase.

Each utility should belong in its own file and be the default export.

These functions are not part of the module interface and are subject to change.


Convert from CamelCase to snake_case

graphql.pyutils.snake_to_camel(s, upper=True)

Convert from snake_case to CamelCase

If upper is set, then convert to upper CamelCase, otherwise the first character keeps its case.


A cached property.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.

graphql.pyutils.dedent(text: str) → str

Fix indentation of given text by removing leading spaces and tabs.

Also removes leading newlines and trailing spaces and tabs, but keeps trailing newlines.

graphql.pyutils.did_you_mean(suggestions: Sequence[str], sub_message: str = None) → str

Given [ A, B, C ] return ‘ Did you mean A, B, or C?’

graphql.pyutils.register_description(base: type) → None

Register a class that shall be accepted as a description.

graphql.pyutils.unregister_description(base: type) → None

Unregister a class that shall no more be accepted as a description.

class graphql.pyutils.EventEmitter(loop: Optional[] = None)

A very simple EventEmitter.

add_listener(event_name: str, listener: Callable)

Add a listener.

emit(event_name, *args, **kwargs)

Emit an event.

remove_listener(event_name, listener)

Removes a listener.

class graphql.pyutils.EventEmitterAsyncIterator(event_emitter: graphql.pyutils.event_emitter.EventEmitter, event_name: str)

Create an AsyncIterator from an EventEmitter.

Useful for mocking a PubSub system for tests.

graphql.pyutils.identity_func(x: T = <INVALID>, *_args) → T

Return the first received argument.

graphql.pyutils.inspect(value: Any) → str

Inspect value and a return string representation for error messages.

Used to print values in error messages. We do not use repr() in order to not leak too much of the inner Python representation of unknown objects, and we do not use json.dumps() because not all objects can be serialized as JSON and we want to output strings with single quotes like Python repr() does it.

We also restrict the size of the representation by truncating strings and collections and allowing only a maximum recursion depth.

graphql.pyutils.is_finite(value: Any) → bool

Return true if a value is a finite number.

graphql.pyutils.is_integer(value: Any) → bool

Return true if a value is an integer number.

graphql.pyutils.is_invalid(value: Any) → bool

Return true if a value is undefined, or NaN.

graphql.pyutils.is_nullish(value: Any) → bool

Return true if a value is null, undefined, or NaN.

graphql.pyutils.suggestion_list(input_: str, options: Collection[str]) → List[str]

Get list with suggestions for a given input.

Given an invalid input string and list of valid options, returns a filtered list of valid options sorted based on their similarity with the input.

class graphql.pyutils.FrozenError

Error when trying to change a frozen (read only) collection.

class graphql.pyutils.FrozenList

List that can only be read, but not changed.

class graphql.pyutils.FrozenDict

Dictionary that can only be read, but not changed.

class graphql.pyutils.Path

A generic path of string or integer indices

add_key(key: Union[str, int]) → graphql.pyutils.path.Path

Return a new Path containing the given key.

as_list() → List[Union[str, int]]

Return a list of the path keys.


current index in the path (string or integer)


path with the previous indices

graphql.pyutils.print_path_list(path: Sequence[Union[str, int]])

Build a string describing the path.