Skip to content

Conversation

BobTheBuidler
Copy link
Contributor

This PR emits a builder error when an invalid key is passed into mypyc_attr. This change could have saved me ~20 minutes or so, when the root of my problem was just a simple typo.

@BobTheBuidler
Copy link
Contributor Author

BobTheBuidler commented Oct 1, 2025

I'm not really sure of the best way to test this, but I have a failing test here that confirms the error and note both work as intended:

https://github.com/python/mypy/actions/runs/18171952588/job/51728555657

@BobTheBuidler BobTheBuidler changed the title [mypyc] fix: reject invalid mypyc_attr args [mypyc] fix: reject invalid mypyc_attr args [1/1] Oct 1, 2025
@p-sawicki
Copy link
Collaborator

I'm not really sure of the best way to test this, but I have a failing test here that confirms the error and note both work as intended:

https://github.com/python/mypy/actions/runs/18171952588/job/51728555657

you could add an irbuild test that succeeds if mypyc reports an error, similar to this one. just change the attribute name to something unsupported, like @mypyc_attr(invalid=True)

@BobTheBuidler
Copy link
Contributor Author

I added the test and it looks like its working, but how do I signify to the test that one particular line will emit both an E and a note?

@mypyc_attr(invalid_kwarg=True) # E: 'invalid_kwarg' is not a supported `mypyc_attr`
class InvalidKwarg:
pass
@mypyc_attr(str()) # E: All `mypyc_attr` args must be string literals.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we actually want this? I noticed the current code ignores any values that arent string literals, and I thought an err would be better than a silent ignore, but should we just parse the non-literal string value like anything else in python would?

I dunno, maybe I'm overthinking it and this is fine as-is

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think it's fine as-is. i doubt this will break any existing code and might catch user errors.

Comment on lines 2693 to 2694
@mypyc_attr("allow_interpreted_subclasses", "invalid_arg") # E: 'invalid_arg' is not a supported `mypyc_attr` \
# N: supported keys: 'allow_interpreted_subclasses', 'free_list_len', 'native_class', 'serializable'
Copy link
Collaborator

Choose a reason for hiding this comment

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

style nit: we usually wrap names in double-quotes in errors/notes, so it should be "invalid_arg", "mypyc_attr", "allow_interpreted_subclasses", etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

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