-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix @cirq.transformer
decorator to work with default value of context, if provided.
#4879
Comments
@cirq.transformer
decorator to work with "default" value of context if provided.@cirq.transformer
decorator to work with default value of context, if provided.
I'd think we want the |
One problem with making @cirq.transformer
def custom_transformer(circuit, context = TransformerContext(ignore_tags=('custom tag',))):
# The custom default will get overwritten by `TransformerContext()` defined in decorator.
pass This can be a surprising behavior for users. The fix for this would probably also work for the optional default use case. |
We can modify the decorator to inspect the signature of the decorated function and copy the default, if any. Something like: @functools.wraps(method)
def method_with_logging(
self, circuit: 'cirq.AbstractCircuit', *, context: TransformerContext = _default_context(method)
) -> 'cirq.AbstractCircuit':
...
def _default_context(callable):
sig = inspect.signature(callable)
default_context = sig.parameters["context"].default
if default_context == inspect.Parameter.empty:
default_context = TransformerContext()
return default_context |
I don't think we automatically assign a default For example: @cirq.transformer
def transformer_with_no_default(
circuit: cirq.AbstractCircuit, context: cirq.TransformerContext
) -> cirq.Circuit:
pass
transformer_with_no_default(circuit) # mypy will result in error because decorator preserves the original function type, which is one with no defaults. |
Summarize the task
If default values are provided for any of the position arguments --
circuit
orcontext
, the@cirq.transformer
decorator currently ignores them and the returned decorated method expects both arguments to be explicitly passed to the method.This should be fixed so that the following works:
Acceptance criteria - when is the task considered done?
Related issues: #4483
cc @maffoo
The text was updated successfully, but these errors were encountered: