-
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
|
This is the place where we would end up if there would be conflicting declarations in the class body. This is just one possible source for declarations of an attribute.
This is the other place, correct. Here, we loop over declarations of attributes in methods. We currently return early, but we probably need to change the loop logic to collect all declared types, and then emit a diagnostic in case there are more than one. Finally, we also need to consider the option that there is a declaration on the class body, and a conflicting declaration in a method. --
We are currently in the process of changing some infrastructure to make this easier. Maybe it would be good to split this task into two parts: (1) collect all conflicting declared types so we would in principle be able to emit a diagnostic — assuming we could do it from anywhere, and (2) actually create a new diagnostic and make the required changes to be able to emit it. I think you could already start with part 1, and for part 2, it might make sense to wait for those changes to the diagnostics infrastructure. Let me know if I failed to answer any of the questions that would be required for part 1. |
Thank you so much, this made it clearer. |
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: