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

Stop sharing class decorators across siblings #152

Merged
merged 2 commits into from
Mar 26, 2019
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
4 changes: 2 additions & 2 deletions tests/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ class Consumer(builder.Consumer):
request_method = request_definition_builder

# Verify: Get request definition builder on access
assert Consumer.request_method is request_definition_builder
assert Consumer.request_method is request_definition_builder.copy()

# Verify: Try again after resetting
Consumer.request_method = request_definition_builder
assert Consumer.request_method is request_definition_builder
assert Consumer.request_method is request_definition_builder.copy()

# Verify: We get callable on attribute access for an instance
consumer = Consumer()
Expand Down
3 changes: 3 additions & 0 deletions uplink/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def _process_annotation(self, name, annotation):
def is_done(self):
return self.remaining_args_count == 0

def copy(self):
return self

@property
def _types(self):
types = self._annotations
Expand Down
2 changes: 1 addition & 1 deletion uplink/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _build_definition(self):

def __get__(self, instance, owner):
if instance is None:
return self._request_definition_builder
return self._request_definition_builder.copy()
else:
return instance.session.create(instance, self._request_definition)

Expand Down
8 changes: 8 additions & 0 deletions uplink/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ def argument_handler_builder(self):
def method_handler_builder(self):
return self._method_handler_builder

def copy(self):
return RequestDefinitionBuilder(
self._method,
self._uri,
self._argument_handler_builder.copy(),
self._method_handler_builder.copy(),
)

def _auto_fill_remaining_arguments(self):
uri_vars = set(self.uri.remaining_variables)
missing = list(self.argument_handler_builder.missing_arguments)
Expand Down
14 changes: 11 additions & 3 deletions uplink/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def add_annotation(self, annotation, *args_, **kwargs):
super(MethodAnnotationHandlerBuilder, self).add_annotation(annotation)
return annotation

def copy(self):
clone = MethodAnnotationHandlerBuilder()
clone._class_annotations = list(self._class_annotations)
clone._method_annotations = list(self._method_annotations)
return clone

def build(self):
return MethodAnnotationHandler(
self._class_annotations + self._method_annotations
Expand Down Expand Up @@ -89,16 +95,18 @@ def _is_static_call(cls, *args_, **kwargs):
else:
return is_consumer_class and not (kwargs or args_[1:])

def __call__(self, class_or_builder):
def __call__(self, class_or_builder, **kwargs):
if self._is_consumer_class(class_or_builder):
builders = helpers.get_api_definitions(class_or_builder)
builders = filter(self._is_relevant_for_builder, builders)

for name, b in builders:
b.method_handler_builder.add_annotation(self, is_class=True)
self(b, is_class=True)
helpers.set_api_definition(class_or_builder, name, b)
elif isinstance(class_or_builder, interfaces.RequestDefinitionBuilder):
class_or_builder.method_handler_builder.add_annotation(self)
class_or_builder.method_handler_builder.add_annotation(
self, **kwargs
)
return class_or_builder

def modify_request(self, request_builder):
Expand Down
3 changes: 3 additions & 0 deletions uplink/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def method_handler_builder(self):
def build(self):
raise NotImplementedError

def copy(self):
raise NotImplementedError


class RequestDefinition(object):
def make_converter_registry(self, converters):
Expand Down