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

Changed typing to improve type checking in PyCharm #16

Merged
merged 7 commits into from
Dec 20, 2021

Conversation

michel-steuwer
Copy link
Member

This change no longer highlights code such as:

def translate_def(ctx: Ctx, op: Operation) -> [Operation]:
    if isinstance(op, choco_ast.FuncDef):
        return translate_fun_def(ctx, op)

def translate_fun_def(ctx: Ctx, fun_def: choco_ast.FuncDef) -> Operation:
...

as a typing error.

With this change PyCharm (and I assume mypy) correctly assumes that the type of op in the branch is choco_ast.FuncDef and not Operation),

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

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.

Copy link
Member Author

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.

Copy link
Collaborator

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?

Copy link
Member Author

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.

Copy link
Member Author

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]

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Collaborator

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

Copy link
Member Author

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.

@math-fehr
Copy link
Collaborator

Besides this one comment, this looks good!

@michel-steuwer
Copy link
Member Author

Btw. Using this change, I am not sure if it fixes all the problems I had ... I am confused again how PyCharm behaves :-(

@math-fehr
Copy link
Collaborator

Yeah, I have some other fixes in a different branch that I should PR soon.
Some other changes include the dataclasses import, or addind signatures in some places.
What I would really like to achieve is making PyCharm properly understand the decorators I used, but that's a bit hard

@michel-steuwer
Copy link
Member Author

I realized that we trade here one warning against the other: The code I gave above doesn't warn anymore, but the call to register_op now does. I thought I had taught the type checker that OperationType must be a subtype of Operation but that does not seem to be the case ...

I also can't get the yapf working:

yapf: toml package is needed for using pyproject.toml as a configuration file

@math-fehr
Copy link
Collaborator

math-fehr commented Dec 17, 2021

Where does it warns you exactly in both cases? It's also possible that we made a mistake somewhere.
Also, for yapf, I just use yapf -ir PATH

@michel-steuwer
Copy link
Member Author

yapf works fine in the other project, but not this one ... I am soo not used to working with python.

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 Operation. Before this PR it didn't understand that the annotated class is different to Operation (as the decorator was returning that type).

Writing:

@irdl_op_definition
class Foo(Operation):
  name = "foo"

fixes this, but introduces other warnings ...

@math-fehr
Copy link
Collaborator

Yeah, I saw this Operation issue, and I have a patch that should fix it (on the builders branch).
I'm curious, which errors start popping when you add the Operation?

@michel-steuwer
Copy link
Member Author

It just reminds me that I have to implement all abstract methods (which are implemented by the decorator)

@math-fehr
Copy link
Collaborator

Okay, so for this, I have a fix, which is simply to remove the need for the decorator.
I'll push this on this branch

@math-fehr
Copy link
Collaborator

I fixed a bit the code (PyCharm was complaining on an import), and also added support for Attributes.
Now PyCharm should complain a bit less, what do you think?

Copy link
Member Author

@michel-steuwer michel-steuwer left a 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)!

@math-fehr
Copy link
Collaborator

Yes, right now both should work.
I plan to make a future PR that enforce making explicit this inheritance. I definitely agree it would make things clearer

@math-fehr math-fehr merged commit fb9a1c9 into main Dec 20, 2021
@math-fehr math-fehr deleted the typing_improvement branch December 20, 2021 16:38
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.

2 participants