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

misc: use experimental TypeForm feature of Pyright #3450

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

superlopuh
Copy link
Member

@superlopuh superlopuh commented Nov 14, 2024

This lets us use better typing when dealing with TypeVars, PRs leveraging this incoming.

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.39%. Comparing base (44d321a) to head (fd2752a).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3450      +/-   ##
==========================================
- Coverage   90.40%   90.39%   -0.01%     
==========================================
  Files         467      467              
  Lines       58840    58841       +1     
  Branches     5603     5605       +2     
==========================================
- Hits        53195    53191       -4     
- Misses       4204     4206       +2     
- Partials     1441     1444       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@compor
Copy link
Collaborator

compor commented Nov 15, 2024

@superlopuh can you please link the incoming PRs when they land?

@compor compor self-requested a review November 15, 2024 15:03
Copy link
Collaborator

@alexarice alexarice left a comment

Choose a reason for hiding this comment

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

Will this allow us to simplify isa and isattr?

@superlopuh
Copy link
Member Author

I hope so. I'll wait for @math-fehr's review to merge. The main risk with this would be to rely on an experimental feature, so we might get stuck again if they remove support. OTOH, we pin our version of Pyright, so it's not like everything will break, and I haven't seen any negative feedback on the TypeForm PEP so I have a feeling it'll land eventually.

@alexarice
Copy link
Collaborator

Even if Pyright is pinned, we don't want to get stuck on an old version of pyright again

@superlopuh
Copy link
Member Author

I'll keep exploring other options but I have a feeling that it might be the best one to have a clean interface for param_op...

_T = TypeVar("_T")


def isa(arg: Any, hint: type[_T]) -> TypeGuard[_T]:
def isa(arg: Any, hint: "TypeForm[_T]") -> TypeGuard[_T]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What error do you get here?

Copy link
Member Author

Choose a reason for hiding this comment

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

TypeForm is defined in Pyright, not typing_extensions, so the error is:

E   NameError: name 'TypeForm' is not defined

Comment on lines -248 to +247
if not isa(op_val := op.value, IntegerAttr):
if not isinstance(op_val := op.value, IntegerAttr):
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens in those cases? isa is returning an error, or is it just that isinstance can do it so we should follow that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Argument type is partially unknown
  Argument corresponds to parameter "hint" in function "isa"
  Argument type is "TypeForm[IntegerAttr[Unknown]]"

Copy link
Member Author

Choose a reason for hiding this comment

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

IntegerAttr is a valid reference to the object that refers to the class, whereas IntegerAttr[IntegerType | IndexType] is the valid type annotation, it's a place where type[_T] and TypeForm[_T] don't overlap.

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 assume that this isn't the case for newer versions of Python, where generic arguments can have default values, where IntegerAttr would be shorthand for IntegerAttr[IntegerType | IndexType]

@superlopuh
Copy link
Member Author

Actually I'm now realizing that this will mean that any downstream users also need to enable experimental features locally for their xDSL code to type check... I'm not sure that I want to force that on people. Let's see if we find another solution for the current goal of migrating to param_def, and reopen this if it's the only option.

@superlopuh superlopuh closed this Nov 20, 2024
@superlopuh superlopuh reopened this Dec 4, 2024
@superlopuh
Copy link
Member Author

superlopuh commented Dec 4, 2024

I had an offline discussion with @math-fehr , and he convinced me that it would be fine. In the end, type-checking is an optional feature, and is not required for the framework to run correctly for the user, and it's only a flag away.

Copy link
Collaborator

@n-io n-io left a comment

Choose a reason for hiding this comment

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

Looks fine from our side!

@superlopuh superlopuh merged commit d81d66d into main Dec 4, 2024
15 checks passed
@superlopuh superlopuh deleted the sasha/misc/type-form branch December 4, 2024 09:55
@alexarice
Copy link
Collaborator

Will we likely need to turn on some experimental feature flag downstream?

@superlopuh
Copy link
Member Author

Yep, same one as in this PR. I'll open another PR for the README change

superlopuh added a commit that referenced this pull request Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
misc Miscellaneous
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants