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

Minor changes to improve the type annotations #53

Merged
merged 2 commits into from
Feb 23, 2022

Conversation

michel-steuwer
Copy link
Member

These two changes improve the usage of xDSL in PyCharm.

  1. Making RegionDef a subclass of Region allows op and ops (and other properties and methods of Regions) to show up in the autocomplete menu.
    Before:

image

After:

image

  1. Removing the TypeVar in the irdl_op_definition significantly improves type checking experience in PyCharm. (I don't understand why!).

Before, the IDE doesn't see that func_def is of type FuncDef:
image

After, it now sees this correctly:
image

@math-fehr
Copy link
Collaborator

The region change, and the builder change makes sense.
Though I'm not understanding at all why the irdl_op_definition makes it work in your PyCharm. My PyCharm seems to work before your change though.
What version of PyCharm do you have? And which version of Python do you use?

@michel-steuwer
Copy link
Member Author

Latest version of PyCharm (2021.3.2 Professional Edition) and Python 3.9.

I will try to reimport the project and see if it makes a difference.

@math-fehr
Copy link
Collaborator

Oh actually, nevermind, my version seems to also have this bug, but I think it is not always there for some reasons.

This works without issues
image

But this fails:
image

Do you have the same behavior?

@math-fehr
Copy link
Collaborator

I think it might be related to the isinstance

@michel-steuwer
Copy link
Member Author

That the first of your examples works is expected, as you have an explicit type annotation in the first line.

The "type narrowing" that isinstance is doing (https://www.python.org/dev/peps/pep-0647/) should work. The type checker should understand that in the nested branch, the type of op is now narrower than before.
I think this might be a bug of the PyCharm editor.

Does this PR fixes this in your IDE as well?

@math-fehr
Copy link
Collaborator

math-fehr commented Feb 21, 2022

For me, it seems to break for other reasons now.
Essentially, it doesn't show any errors, but this is because PyCharm doesn't understand what FuncOp is
image
Here, op.foo is correctly shown a warning, while the op.foo after the if has no warnings

@michel-steuwer
Copy link
Member Author

... hmm

maybe it is just disabling all checking and reverting to the Any type?

@math-fehr
Copy link
Collaborator

I think it's the case, since we don't provide any information about the resulting type of irdl_op_definition.
I'm trying to find if there is a way to make the isinstance work better though, I'm not having much luck for now

@math-fehr
Copy link
Collaborator

Otherwise, this is indeed a problem with isinstance:
image

@michel-steuwer
Copy link
Member Author

I think the type var that we currently have (before this PR) is modelling the type correctly.

@michel-steuwer
Copy link
Member Author

I am trying to see what mypy thinks about this ...

@math-fehr
Copy link
Collaborator

Yeah, what we had before should be correct according to PEP. Though PyCharm seems to be buggy because of the decorator/TypeVar I think. I'm trying to find a minimum reproducer.

@math-fehr
Copy link
Collaborator

I've made some experiments, and I'm realizing that in fact, nothing was working in the first place.
So even before, the type checker would not show an error on wrong attributes, because it knew the type could have arbitrary attributes in addition to the FuncDef ones. Though the IDE recommendations were still correct.
The problem we are having here is that in addition to that, the isinstance is bugged, thus giving errors instead of casting to "Any".
I'm not sure how to fix that bug, but what I found is that moving the function to the same file as the dialect definition makes it work, so maybe there is an import issue somewhere. I'll try a bit to fix that if there is an easy solution.

@math-fehr
Copy link
Collaborator

Well, I abandon, I have no idea on how to fix this bug.
I guess that we will have to wait that this is fixed somehow, though I still don't have a simple reproducer to fix it.

I would suggest to add your changes, since they at least seem to not give annoying warnings, and that they seem to provide the right suggestions somehow!
We can always fix that later, though I doubt that this will be fixed soon!

@michel-steuwer michel-steuwer merged commit c06d548 into main Feb 23, 2022
@michel-steuwer michel-steuwer deleted the type_annotation_changes branch February 23, 2022 14:37
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