-
Notifications
You must be signed in to change notification settings - Fork 79
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
dialects: (builtin) Fix documentation for parsing of bools, and add conversion to Python bool #3689
base: main
Are you sure you want to change the base?
Changes from 4 commits
765a9e4
d8dc686
986605f
711157c
d05edd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -167,6 +167,11 @@ def test_IntegerAttr_normalize(): | |||||
IntegerAttr(256, 8) | ||||||
|
||||||
|
||||||
def test_IntegerAttr_to_bool(): | ||||||
assert not IntegerAttr.from_bool(False) | ||||||
assert IntegerAttr.from_bool(True) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
def test_IntegerType_packing(): | ||||||
# i1 | ||||||
nums_i1 = (0, 1, 0, 1) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,6 +294,10 @@ def print_parameter(self, printer: Printer) -> None: | |
with printer.in_angle_brackets(): | ||
printer.print_string(f"{self.data}") | ||
|
||
def __bool__(self) -> bool: | ||
"""Returns True if value is non-zero.""" | ||
return bool(self.data) | ||
|
||
Comment on lines
+297
to
+300
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good to add unit tests for this for good measure |
||
|
||
class Signedness(Enum): | ||
"Signedness semantics for integer" | ||
|
@@ -705,6 +709,10 @@ def constr( | |
), | ||
) | ||
|
||
def __bool__(self) -> bool: | ||
"""Returns True if value is non-zero.""" | ||
return bool(self.value) | ||
|
||
|
||
AnyIntegerAttr: TypeAlias = IntegerAttr[IntegerType | IndexType] | ||
AnyIntegerAttrConstr: BaseAttr[AnyIntegerAttr] = BaseAttr(IntegerAttr) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ def parse_optional_integer( | |
""" | ||
Parse an (possible negative) integer. The integer can either be | ||
decimal or hexadecimal. | ||
Optionally allow parsing of 'true' or 'false' into 1 and 0. | ||
Optionally allow parsing of 'true' or 'false' into -1 and 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not agree with the implementation below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah actually not here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, all of the functions with the changed documentation here return the 1/0 values, it's only when creating the attribute that the value is different. This should be reflected in documentation for the attribute, not the parser. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I did not put a comment on the doc change for the attribute |
||
""" | ||
# Parse true and false if needed | ||
if allow_boolean: | ||
|
@@ -78,7 +78,7 @@ def parse_integer( | |
""" | ||
Parse an (possible negative) integer. The integer can | ||
either be decimal or hexadecimal. | ||
Optionally allow parsing of 'true' or 'false' into 1 and 0. | ||
Optionally allow parsing of 'true' or 'false' into -1 and 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
""" | ||
|
||
return self.expect( | ||
|
@@ -121,7 +121,7 @@ def parse_optional_number( | |
) -> int | float | None: | ||
""" | ||
Parse a (possibly negative) integer or float literal, if present. | ||
Can optionally parse 'true' or 'false' into 1 and 0. | ||
Can optionally parse 'true' or 'false' into -1 and 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
""" | ||
|
||
is_negative = self._parse_optional_token(MLIRTokenKind.MINUS) is not None | ||
|
@@ -149,7 +149,7 @@ def parse_number( | |
) -> int | float: | ||
""" | ||
Parse a (possibly negative) integer or float literal. | ||
Can optionally parse 'true' or 'false' into 1 and 0. | ||
Can optionally parse 'true' or 'false' into -1 and 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
""" | ||
return self.expect( | ||
lambda: self.parse_optional_number(allow_boolean=allow_boolean), | ||
|
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.
Super nit and probably doesn't really matter