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

Add match_args support to attr.s() #12111

Merged
merged 6 commits into from
Feb 12, 2022
Merged

Add match_args support to attr.s() #12111

merged 6 commits into from
Feb 12, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Feb 2, 2022

This is a very basic implementation. I am not yet very familiar with __match_args__.

Are there any corner cases that I am missing?

@sobolevn
Copy link
Member Author

sobolevn commented Feb 2, 2022

CC @Tinche, you might be interested! I would appreciate your review 🙂

cls=ctx.cls,
name='__match_args__',
typ=tuple_type,
no_serialize=True,
Copy link
Member Author

Choose a reason for hiding this comment

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

Should we serialize this? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, probably we should.

@Tinche
Copy link
Contributor

Tinche commented Feb 2, 2022

Phew, excellent. I was worried I'd have to do this myself but I already have a bunch of other projects on my plate. Can give it a review in the next couple of days.

@@ -290,6 +292,12 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
if kw_only:
ctx.api.fail(KW_ONLY_PYTHON_2_UNSUPPORTED, ctx.reason)
return
if ctx.api.options.python_version[:2] < (3, 10):
if match_args:
ctx.api.fail("match_args is not supported before Python 3.10", info.defn)
Copy link
Member

Choose a reason for hiding this comment

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

Is this what attrs does at runtime? It's a bit strange to me because it seems harmless to set it before 3.10. For example, you might use it in a library if you want 3.10 users to be able to use match, but still support older versions too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah looks like we set it conditionally, presumably to avoid a little work and since it'd be useless.

Since the check is in runtime and based on the running interpreter, I'm not sure I understand your concern. Can you elaborate? We can relax this if needed.

Copy link
Member

Choose a reason for hiding this comment

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

The scenario I'm thinking of is:

  • I'm writing a library that supports both 3.9 and 3.10
  • The library exposes an attrs class in its API
  • I want 3.10 users to be able to use the class with match

In that case, I'd want to be able to just set match_args=True.

It looks like that's already how the runtime works (it just doesn't set the attribute on 3.9 or lower), but the code in this PR would make mypy emit an error if it's checking in 3.9 mode and sees match_args=True.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right, I see. It's true that the parameter exists on earlier Pythons, so it's not an error to use it there. And you can't use __match_args__ on earlier Pythons anyway (I guess you could manually do something with it, but who does that).

Also, match_args defaults to True.

So this check can essentially be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed! 👍

@sobolevn sobolevn mentioned this pull request Feb 6, 2022
@sobolevn
Copy link
Member Author

sobolevn commented Feb 7, 2022

Thank you, @Tinche, very helpful. Having someone with domain knowledge is amazing, many details were wrong 👍

Copy link
Contributor

@Tinche Tinche left a comment

Choose a reason for hiding this comment

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

❤️ Thanks for working on this!

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@sobolevn sobolevn merged commit 5b9318d into python:master Feb 12, 2022
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.

3 participants