Skip to content

stubtest: error if an attribute is a read-only property at runtime, but isn't read-only in the stub #12291

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

Merged
merged 5 commits into from
Mar 21, 2022

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 4, 2022

Description

This PR proposes adding checks to stubtest so that it raises an error if a variable is annotated as being writeable in the stub, but is a read-only property at runtime.

New errors reported by stubtest with this patch applied, when checking typeshed

0; however, this patch was used to prepare the following PR, fixing roughly 50 hits:

Things I tried, which ultimately proved unfruitful

  • Checking the opposite scenario as well: raising an error if an attribute was marked as read-only in a stub but was a read-write property at runtime. This resulted in many false positives and 0 true positives.
  • Adding a similar check for other common descriptors, such as instances of types.GetSetDescriptorType. Unfortunately, it is much harder to accurately determine whether or not an instance of GetSetDescriptorType is a read-only data descriptor, or a read-write data descriptor. (All instances of GetSetDescriptorType have a __set__ method, but many of these __set__ methods raise AttributeError. Whereas with properties you can do a simple if prop.fset is None check, there's no equivalent test for GetSetDescriptors.)

Test Plan

Four Five Four new test cases added, one modified.

cc. @hauntsaninja

@AlexWaygood AlexWaygood changed the title stubtest: error if an attribute is a read-only property at runtime, but not in the stub stubtest: error if an attribute is a read-only property at runtime, but is read-only in the stub Mar 4, 2022
@AlexWaygood AlexWaygood changed the title stubtest: error if an attribute is a read-only property at runtime, but is read-only in the stub stubtest: error if an attribute is a read-only property at runtime, but isn't read-only in the stub Mar 4, 2022
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2022

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

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, looks good. Just one nit about test cases

@hauntsaninja
Copy link
Collaborator

#12403 fixes the lint failure

@AlexWaygood AlexWaygood reopened this Mar 21, 2022
@hauntsaninja hauntsaninja merged commit 27938e7 into python:master Mar 21, 2022
@hauntsaninja
Copy link
Collaborator

Thanks! :-)

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