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

[red-knot] Extend instance/class attribute tests #15959

Merged
merged 4 commits into from
Feb 5, 2025

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Feb 5, 2025

Summary

In preparation for creating some (sub) issues for #14164, I'm trying to document the current behavior (and a bug) a bit better.

@sharkdp sharkdp added testing Related to testing Ruff itself red-knot Multi-file analysis & type inference labels Feb 5, 2025
Comment on lines 235 to 236
# TODO: We should probably infer `int | str` here.
reveal_type(c_instance.z) # revealed: int
Copy link
Member

Choose a reason for hiding this comment

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

Since we'll have emitted a diagnostic above complaining about the redeclaration, I think the current behaviour here is also reasonable TBH. I wouldn't worry too much about inferring a union here if it complicates the implementation.

Comment on lines 490 to 491
# TODO: should be `Literal["overwritten on class"]` once/if we support
# local narrowing.
Copy link
Member

Choose a reason for hiding this comment

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

interesting... do mypy/pyright support narrowing like this, where you narrow it on the class but then read it from an instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mypy doesn't seem to support this detection of implicit pure_class_variables at all. Pyright supports narrowing in the case above (C.pure_class_variable), but not here.

Maybe I should remove the TODO here?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I don't think this is something we "have" to support -- I don't think it's something users will expect of us. (I think they will expect attribute narrowing when the attribute is accessed on the same variable, such as C.pure_class_variable above.) Obviously if it's easy and naturally falls out of the implementation, then that's fantastic -- I think it isn't any more unsound than attribute narrowing in general. But I don't think it's something we need a TODO for.

reveal_type(D.x) # revealed: Unknown

# TODO: this should raise `unresolved-attribute` as well, and the type should be `Unknown`
reveal_type(D().x) # revealed: Unknown | Literal[1]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pyright revals int here, which is wrong.

Copy link
Member

Choose a reason for hiding this comment

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

oof. might be worth a bug report to pyright!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I won't be surprised if aliasing staticmethod is just considered out-of-scope for pyright to handle. I will be very happy if we can handle it, however!

@sharkdp sharkdp merged commit eb08345 into main Feb 5, 2025
21 checks passed
@sharkdp sharkdp deleted the david/extend-attribute-tests branch February 5, 2025 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference testing Related to testing Ruff itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants