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

Signal: allow to use integral Enum for reset value. #296

Merged
merged 1 commit into from
Jan 11, 2020
Merged

Signal: allow to use integral Enum for reset value. #296

merged 1 commit into from
Jan 11, 2020

Conversation

Fatsie
Copy link
Contributor

@Fatsie Fatsie commented Jan 6, 2020

Follow-up to #294.

@@ -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:
Copy link
Contributor

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
Copy link

codecov bot commented Jan 6, 2020

Codecov Report

Merging #296 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
nmigen/hdl/ast.py 87.15% <100%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8184efd...b057fb9. Read the comment docs.

nmigen/hdl/ast.py Outdated Show resolved Hide resolved
@@ -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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Sorry, something went wrong.

Copy link
Contributor

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?

Sorry, something went wrong.

@whitequark
Copy link
Contributor

Thanks. Could you add a test as well?

Sorry, something went wrong.

@Fatsie
Copy link
Contributor Author

Fatsie commented Jan 9, 2020

Pushed a test.

@Fatsie
Copy link
Contributor Author

Fatsie commented Jan 9, 2020

Pushed change that removes reference to bool in docstring and extended test to check for non-integral Enum type.

nmigen/hdl/ast.py Outdated Show resolved Hide resolved
@Fatsie
Copy link
Contributor Author

Fatsie commented Jan 10, 2020

ValueError -> TypeError

@whitequark whitequark merged commit e4e2671 into m-labs:master Jan 11, 2020
@whitequark
Copy link
Contributor

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants