Skip to content

mypyc: Add support for new-style attrs API #15274

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented May 20, 2023

Fixes #15267.

We don't support slotted classes yet, so we'll be explicit about it.

@ikonst ikonst force-pushed the mypyc-attrs-new branch from 22004ab to 9716a00 Compare May 20, 2023 04:27
@JelleZijlstra JelleZijlstra changed the title Add support for new-style attrs API mypyc: Add support for new-style attrs API May 20, 2023
@ikonst
Copy link
Contributor Author

ikonst commented May 25, 2023

👋 @JelleZijlstra

@ichard26
Copy link
Collaborator

I can probably take a look tomorrow FWIW. I'm not sure how familiar Jelle is with mypyc.

Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Would like a second pair of eyes, but here's my initial feedback. Thanks for the PR!

return d.fullname.split(".")[0]
name = d.fullname.split(".")[0]
if name == "attrs":
raise ValueError("Slotted attrs classes are not supported")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally this would use the builder error plumbing, but TBF mypyc's error reporting story is pretty terrible anyway (see "usability" under mypyc/mypyc#785).

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not really acceptable to raise ValueError. This should report an error using the mypyc.errors.Errors class.

"dataclasses.dataclass",
"attr.s",
"attr.attrs",
"attrs.define",
Copy link
Collaborator

Choose a reason for hiding this comment

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

For some reason, the MemberExpr for these have fullname set to attr.define which isn't included here... do you know why that's the case? If you remove the slots=False argument, no error is raised. Are you sure mypyc is recognizing attrs' modern APIs?

import attrs

@attrs.define(slots=False)
class Person:
    name: str

@attrs.define(slots=False)
class MyException(Exception):
    level: int

    def __str__(self) -> str:
        return f"level {self.level} storm up ahead!"

(Also subclassing from Exception seems to be broken, but it's broken on main too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't reproduce it. What version of attrs do you have installed in your environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(BTW, not sure how to assert a 'compilation error' in those tests.)

Co-authored-by: Richard Si <[email protected]>
@ikonst
Copy link
Contributor Author

ikonst commented Aug 8, 2023

@ichard26 can take another look?

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.

mypyc doesn't support attrs.field
3 participants