-
Notifications
You must be signed in to change notification settings - Fork 23
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
[Circuit] Detect overwriting of self.io
in the presence of local references
#1298
Comments
I would suspect that 2. is more "pythonic" and perhaps assumed behavior from someone familiar with Python, although I know we originally chose to keep IOs as immutable on principal. Perhaps we can try to enumerate the advantages and costs of keeping it immutable. Perhaps an option 3. is that |
I am starting to watch the issues as a way of catching up. I don't like the idea of modifying self.io in an init. First, I believe we should try to be functional and single assignment as much as possible. Second, we should have clearly delimited stages. Class definition time and generators are the right place to define self.io (it really should be thought of as cls.io). Is there a compelling use case for this feature? |
@phanrahan thanks for the feedback. I agree with the goal of being functional and single assignment as much as possible. As well as delimiting stages as much as possible (specify io, instantiate modules, wire stuff up). However, there are certainly compelling cases for using at very least io = m.IO(<common stuff>)
if flag0:
io += m.IO(<stuff related to flag0>)
if flag1:
io += m.IO(<stuff related to flag1>)
... Of course, designers can always do the following: ports = {<common stuff>}
if flag0:
ports.update({<stuff related to flag0>})
if flag1:
ports.update({<stuff related to flag1>})
io = m.IO(**ports) In practice we've found designers like doing the |
@leonardt I think we can do both (1) and (2) (and your option (3)) altogether (not mutually exclusive changes). I do think invalidating both lhs and rhs after a |
Can we limit the += to the class definition? Once the class is defined it becomes immutable? |
Yes, this is already the case. IO can not be updated after class definition. To clarify something from above, the reason for |
Sounds good. |
Often times it is desirable to both locally refer to
self.io
in a generator's__init__
function (i.e.io = self.io
), as well as incrementally build the io, i.e.self.io += m.IO(...)
. However, since+=
overwrites theself.io
object with a newIO
object, it can lead to a bug where one does the following:To alleviate this I propose the following:
__add__
as a function that returns a newIO
object. Ideally it should check refcount (e.g. sys.getrefcount or gc.get_referrers) and if there is another referrer toself
, then raise a warning or error.__iadd__
as a new function that modifiesself
directly and returnsself
, avoiding the problem altogether by not creating a new object.The text was updated successfully, but these errors were encountered: