-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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] Diagnostic for conflicting declared attribute types #15964
Comments
__init__
(see TODO in own_instance_member
)
I am not sure if this is helpful, but here is what class C:
"""
Diagnostics:
Declaration "z" is obscured by a declaration of the same name [reportRedeclaration]
"""
z: int
def __init__(self) -> None:
self.x = get_int()
"""
Diagnostics:
Declaration "y" is obscured by a declaration of the same name [reportRedeclaration]
"""
self.y: int = 1
def other_method(self):
self.x = get_str()
self.y: str = "a"
self.z: str = "a" I am trying to learn/understand the technical stuff behind parsers by reading (books, blogs) and contributing to tools I love that has things to do with parsers (ruff ❤). is this issue about adding a new rule? or just fixing/refactoring |
Yes, this is about emitting a new diagnostic (we can probably reuse conflicting-declarations) when we see something like this. We do want to report them in the same places where pyright reports "declaration … is obscured by …", but I think we want to use a slightly different wording, as we generally allow re-declarations of symbols: x: int = 1
x: str = "foo" What's not allowed though, is to have multiple visible conflicting declarations: def _(flag: bool):
if flag:
x: str
else:
x: int
x = 1 # error: [conflicting-declarations] For the case mentioned in this issue, we want something similar, but for declarations of attributes. |
(To be clear, though, this issue is not about adding a new ruff rule, it's about a new red-knot diagnostic. Although they are in the same codebase, red-knot is separate from ruff and shares only the parser/AST with it.) |
Thank you all for the information. All clear. I will give this issue a shot. |
While trying to dive deep into the code, I've found the following comments where I think the ruff/crates/red_knot_python_semantic/src/types.rs Lines 4323 to 4329 in cb8b23d
Also, I've found this TODO: ruff/crates/red_knot_python_semantic/src/types.rs Lines 4224 to 4237 in cb8b23d
However, I believe that Could you please point me to a similar example where a similar rule had been tackled? My thought process is to implement some logic in: ruff/crates/red_knot_python_semantic/src/types/infer.rs Lines 899 to 904 in cb8b23d
and: ruff/crates/red_knot_python_semantic/src/types/infer.rs Lines 929 to 934 in cb8b23d
or to check here if the target is an instance member of class, and check all the symbol declarations in the scope of the class body, by calling ruff/crates/red_knot_python_semantic/src/types/infer.rs Lines 2121 to 2125 in cb8b23d
|
We should emit a diagnostic when we see conflicting declared types of attributes, either between the declaration in the class body and declarations in a method, or between annotated assignments in different methods. We have existing tests for this scenario (see
TODO
comments):ruff/crates/red_knot_python_semantic/resources/mdtest/attributes.md
Lines 212 to 226 in eb08345
part of: #14164
The text was updated successfully, but these errors were encountered: