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

core: Allowing overload of ClassVar from superclass #2690

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/dialects/test_irdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,19 @@ def test_class_var_on_op():
def test_class_var_on_op_invalid():
with pytest.raises(PyRDLOpDefinitionError, match="is neither a"):
irdl_op_definition(MyOpWithClassVarInvalid)


class MySuperWithClassVarDef(IRDLOperation):
name = "test.super_has_class_var"

VAR: ClassVar[str]


class MySubWithClassVarOverload(MySuperWithClassVarDef):
name = "test.super_has_class_var"

VAR: ClassVar[str] = "hello_world"


def test_class_var_on_super():
irdl_op_definition(MySubWithClassVarOverload)
16 changes: 16 additions & 0 deletions tests/dialects/test_irdl_with_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ def test_class_var_on_op():
def test_class_var_on_op_invalid():
with pytest.raises(PyRDLOpDefinitionError, match="is neither a"):
irdl_op_definition(MyOpWithClassVarInvalid)


class MySuperWithClassVarDef(IRDLOperation):
name = "test.super_has_class_var"

VAR: ClassVar[str]


class MySubWithClassVarOverload(MySuperWithClassVarDef):
name = "test.super_has_class_var"

VAR: ClassVar[str] = "hello_world"


def test_class_var_on_super():
irdl_op_definition(MySubWithClassVarOverload)
8 changes: 8 additions & 0 deletions xdsl/irdl/irdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,14 @@ def wrong_field_exception(field_name: str) -> PyRDLOpDefinitionError:
annotations = parent_cls.__annotations__

for field_name in annotations:
if field_name.isupper() and (
get_origin(annotations[field_name]) is ClassVar
or (
isinstance(annotations[field_name], str)
and annotations[field_name].startswith("ClassVar")
)
):
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing as we have this in two places now, could we factor this out to make it easier to see that the two checks are identical? And maybe also make this code denser again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds very reasonable, have factored out the check as suggested.

if field_name not in clsdict:
raise wrong_field_exception(field_name)

Expand Down
Loading