Skip to content

Commit

Permalink
Cosmetic changes, run black
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Mar 18, 2020
1 parent faa80af commit 85635a2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/graphql/execution/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class MiddlewareManager:
must be aware of this and check whether values are awaitable before awaiting them.
"""

__slots__ = "__weakref__", "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers"
# allow custom attributes (not used internally)
__slots__ = "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers"

_cached_resolvers: Dict[GraphQLFieldResolver, GraphQLFieldResolver]
_middleware_resolvers: Optional[List[Callable]]
Expand Down
13 changes: 10 additions & 3 deletions src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Token:
Represents a range of characters represented by a lexical token within a Source.
"""

__slots__ = ("kind", "start", "end", "line", "column", "prev", "next", "value")
__slots__ = "kind", "start", "end", "line", "column", "prev", "next", "value"

kind: TokenKind # the kind of token
start: int # the character offset at which this Node begins
Expand Down Expand Up @@ -162,7 +162,13 @@ class Location:
region of the source from which the AST derived.
"""

__slots__ = ("start", "end", "start_token", "end_token", "source")
__slots__ = (
"start",
"end",
"start_token",
"end_token",
"source",
)

start: int # character offset at which this Node begins
end: int # character offset at which this Node ends
Expand Down Expand Up @@ -214,7 +220,8 @@ class OperationType(Enum):
class Node:
"""AST nodes"""

__slots__ = ("__dict__", "__weakref__", "loc",)
# allow custom attributes and weak references (not used internally)
__slots__ = "__dict__", "__weakref__", "loc"

loc: Optional[Location]

Expand Down
1 change: 1 addition & 0 deletions src/graphql/language/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Source:
"""A representation of source input to GraphQL."""

# allow custom attributes and weak references (not used internally)
__slots__ = "__weakref__", "__dict__", "body", "name", "location_offset"

def __init__(
Expand Down
20 changes: 10 additions & 10 deletions tests/language/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ def can_hash():
assert node3 != node
assert hash(node3) != hash(node)

def can_create_weak_reference():
node = SampleTestNode(alpha=1, beta=2)
ref = weakref.ref(node)
assert ref() is node

def can_create_custom_attribute():
node = SampleTestNode(alpha=1, beta=2)
node.gamma = 3 # type: ignore
assert node.gamma == 3 # type: ignore

def can_create_shallow_copy():
node = SampleTestNode(alpha=1, beta=2)
node2 = copy(node)
Expand Down Expand Up @@ -225,13 +235,3 @@ class Foo(Node):

def provides_keys_as_class_attribute():
assert SampleTestNode.keys == ["loc", "alpha", "beta"]

def can_weakref():
node = SampleTestNode(alpha=1, beta=2)
wr = weakref.ref(node) # That this works is 90% of the test
assert wr() is node

def can_make_attrs():
node = SampleTestNode(alpha=1, beta=2)
node.__new_random_attr = "Hello!" # That this works is 90% of the test
assert node.__new_random_attr == "Hello!"
12 changes: 12 additions & 0 deletions tests/language/test_source.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import weakref

from pytest import raises # type: ignore

from graphql.language import Source, SourceLocation
Expand Down Expand Up @@ -63,6 +65,16 @@ def can_be_compared():
assert not source == "bar"
assert source != "bar"

def can_create_weak_reference():
source = Source("foo")
ref = weakref.ref(source)
assert ref() is source

def can_create_custom_attribute():
node = Source("foo")
node.custom = "bar" # type: ignore
assert node.custom == "bar" # type: ignore

def rejects_invalid_body_and_name():
with raises(TypeError, match="body must be a string\\."):
# noinspection PyTypeChecker
Expand Down

0 comments on commit 85635a2

Please sign in to comment.