question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make OpTree runtime checkable

See original GitHub issue

Is your feature request related to a use case or problem? Please describe. I use code to process circuits or circuit-like objects. It would be nice to have a simple check to see if an object can be mapped to a circuit (i.e. if it is an OpTree).

Describe the solution you’d like Add the @runtime_checkable decorator to the OpTree protocol.

Then I could do isinstance(test_object, cirq.OP_TREE).

Could it be this easy? Would it break anything? I’m not sure…

[optional] Describe alternatives/workarounds you’ve considered Implement a function that recurs through an object to check if it is optree-like. Or see if invoking Circuit on the object crashes.

[optional] Additional context (e.g. screenshots)

What is the urgency from your perspective for this issue? Is it blocking important work? P3 - I’m not really blocked by it, it is an idea I’d like to discuss / suggestion based on principle

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dkafricommented, Mar 5, 2021

I am implementing an API for specifying generic noise models. In order to parse the input I need to check that it can be converted to a circuit. For my purposes it is sufficient to check if Circuit( ) can evaluate the input.

0reactions
maffoocommented, Mar 4, 2021

is_op_tree looks reasonable, but one possible pitfall is that many op-trees (e.g. those coming from decompose methods) are implemented as generators; if you call is_op_tree on a generator you will exhaust it and then if you try to do anything else with it it’ll be empty. As long as generators are allowed I don’t see a way to deal with the generically, so I’d lean more toward just trying to use the object as an OpTree and catching exceptions, e.g. try to make a circuit and if it fails then do something else.

@dkafri, can you describe your use case in more detail? Where are you getting an object from where you don’t know whether or not it is an OpTree, and what are you trying to do with it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 544 – Protocols: Structural subtyping (static duck typing)
In this PEP we specify static and runtime semantics of protocol classes ... There is no intent to make protocols non-optional in the...
Read more >
Internal Error on runtime checkable protocol containing a class
Crash Report mypy crashed while checking a file defining a runtime checkable protocol that contained another class. Traceback mypy_bug.py:4: ...
Read more >
Python Check if Methods or interface is implemented before ...
I'm creating an interface Mixins that contains methods that will throw error if it is not implemented, however it only happens during run...
Read more >
UndefinedBehaviorSanitizer — Clang 16.0.0git documentation
To use the minimal runtime, add -fsanitize-minimal-runtime to the clang command line options. For example, if you're used to compiling with -fsanitize=undefined ...
Read more >
Protocol + @runtime_checkable (intermediate) anthony ...
today I follow up to the video about Protocol and show @ runtime_checkable for doing some light structural type checking at runtime !...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found