-
Notifications
You must be signed in to change notification settings - Fork 58
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
Signal: allow to use integral Enum for reset value. #296
Conversation
nmigen/hdl/ast.py
Outdated
@@ -834,6 +834,11 @@ def __init__(self, shape=None, *, name=None, reset=0, reset_less=False, min=None | |||
attrs=None, decoder=None, src_loc_at=0): | |||
super().__init__(src_loc_at=src_loc_at) | |||
|
|||
if isinstance(reset, Enum): | |||
reset = reset.value | |||
if not type(reset) == int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an isinstance(reset, int)
, or reset=True
will fail. (bool
is derived from int
in Python.)
Codecov Report
@@ Coverage Diff @@
## master #296 +/- ##
==========================================
- Coverage 82.13% 82.13% -0.01%
==========================================
Files 34 34
Lines 5647 5652 +5
Branches 1160 1162 +2
==========================================
+ Hits 4638 4642 +4
- Misses 864 865 +1
Partials 145 145
Continue to review full report at Codecov.
|
@@ -797,7 +797,7 @@ class Signal(Value, DUID): | |||
name this ``Signal`` is assigned to. Name collisions are automatically resolved by | |||
prepending names of objects that contain this ``Signal`` and by appending integer | |||
sequences. | |||
reset : int | |||
reset : int, bool or integral Enum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bool
is an integral type, so it's fine to just say int or integral Enum
; this covers the cases where it's bool or bool Enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to mention bool
explicitly here as it makes clear that it is a proper use of the parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inconsistent with all other docstrings nMigen has, which assume that the reader knows bool
is an integral type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change it but it is not how I read autodocs. To me it's not about knowing if bool
is an integral type but if int
as a specification in docstring means one is also supposed to pass bool
for that parameter. To me just specifying int
means one is not supposed use bool
for that parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not how object-oriented programming works. If bool
derives from int
, then it is OK to pass bool
anywhere you'd pass int
; this is called the Liskov Substitution Principle. Over-explaining LSP in a docstring is like over-explaining classes in a docstring: sure, you could do it, but why?
Thanks. Could you add a test as well? |
Pushed a test. |
Pushed change that removes reference to bool in docstring and extended test to check for non-integral Enum type. |
|
Thanks for the PR! |
Follow-up to #294.