-
Notifications
You must be signed in to change notification settings - Fork 78
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
Changed typing to improve type checking in PyCharm #16
Conversation
src/xdsl/ir.py
Outdated
@@ -18,7 +20,7 @@ class MLContext: | |||
_registeredAttrs: Dict[str, typing.Type[Attribute]] = field( | |||
default_factory=dict) | |||
|
|||
def register_op(self, op: typing.Type[Operation]) -> None: | |||
def register_op(self, op: OperationType) -> None: |
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.
Here, shouldn't this be typing.Type[OperationType]
?
I'm actually not sure why this change would work.
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 think that both syntaxes work just fine. It might be that the newer syntax has been introduced in some recent Python versions.
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.
So I'm not understanding something.
Here, from what I understand, op
will be an object of type Operation
(or any superclass).
However, we want op
to be a type that is a subclass of Operation
.
Am I understand this right?
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.
Sory, you are absolutely right.
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 typing.Type[OperationType]
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.
Hm. Now I am confused myself.
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.
What I wanted to say here is that the argument is a concrete type that is a subtype of Operation
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.
So normally, typing.Type[Operation]
should work, but typing.Type[OperationType]
as well.
As long as you use the type only once, they should be equivalent
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 was indeed a mistake. I fixed this.
Besides this one comment, this looks good! |
Btw. Using this change, I am not sure if it fixes all the problems I had ... I am confused again how PyCharm behaves :-( |
Yeah, I have some other fixes in a different branch that I should PR soon. |
I realized that we trade here one warning against the other: The code I gave above doesn't warn anymore, but the call to I also can't get the
|
Where does it warns you exactly in both cases? It's also possible that we made a mistake somewhere. |
It warns me when I register the ops for each dialect but more broadly it doesn't understand that a class that has been annotated with the decorator is a subclass of Writing: @irdl_op_definition
class Foo(Operation):
name = "foo" fixes this, but introduces other warnings ... |
Yeah, I saw this |
It just reminds me that I have to implement all abstract methods (which are implemented by the decorator) |
Okay, so for this, I have a fix, which is simply to remove the need for the decorator. |
I fixed a bit the code (PyCharm was complaining on an import), and also added support for Attributes. |
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.
Yes, that looks good.
I assume that the decorator will still work even when we do not make an operation a subclass of Operation
?
I.e.:
@irdl_op_definition
class FuncOp:
name = "buildin.func"
and
@irdl_op_definition
class FuncOp(Operation):
name = "buildin.func"
will behave the same at runtime?
I don't think this is a big issue. Making the subtyping relationship explicit in the user-written code is a good thing, as it makes the relationship clear (which was not clear to me using the API)!
Yes, right now both should work. |
This change no longer highlights code such as:
as a typing error.
With this change PyCharm (and I assume mypy) correctly assumes that the type of
op
in the branch ischoco_ast.FuncDef
and notOperation
),