Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nodes extensible and weak-referencable #82

Merged
merged 6 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/graphql/execution/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MiddlewareManager:
must be aware of this and check whether values are awaitable before awaiting them.
"""

__slots__ = "middlewares", "_middleware_resolvers", "_cached_resolvers"
__slots__ = "__weakref__", "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers"

_cached_resolvers: Dict[GraphQLFieldResolver, GraphQLFieldResolver]
_middleware_resolvers: Optional[List[Callable]]
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class OperationType(Enum):
class Node:
"""AST nodes"""

__slots__ = ("loc",)
__slots__ = ("__dict__", "__weakref__", "loc",)

loc: Optional[Location]

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

__slots__ = "body", "name", "location_offset"
__slots__ = "__weakref__", "__dict__", "body", "name", "location_offset"

def __init__(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/language/test_ast.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import copy, deepcopy
import weakref

from graphql.language import Location, Node, Source, Token, TokenKind
from graphql.pyutils import inspect
Expand Down Expand Up @@ -224,3 +225,13 @@ 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!"