-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Improve consistency between linter rules in determining whether a function is property #12581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes to this file increase the scope of this rule. We could therefore make the changes to this rule a preview-only change. However, I don't see it as a significant increase in scope: I doubt there will be many new hits on user code as a result of this change. The error that the rule is trying to catch is pretty uncommon, anyway. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,16 @@ use std::ops::Deref; | |
use std::path::Path; | ||
|
||
use ruff_index::{newtype_index, IndexSlice, IndexVec}; | ||
use ruff_python_ast::{self as ast, Stmt}; | ||
use ruff_python_ast::name::QualifiedName; | ||
use ruff_python_ast::{self as ast, Stmt, StmtFunctionDef}; | ||
use ruff_text_size::{Ranged, TextRange}; | ||
|
||
use crate::analyze::visibility::{ | ||
class_visibility, function_visibility, method_visibility, module_visibility, Visibility, | ||
class_visibility, function_visibility, is_property, method_visibility, module_visibility, | ||
Visibility, | ||
}; | ||
use crate::model::all::DunderAllName; | ||
use crate::SemanticModel; | ||
|
||
/// Id uniquely identifying a definition in a program. | ||
#[newtype_index] | ||
|
@@ -148,6 +151,17 @@ impl<'a> Definition<'a> { | |
) | ||
} | ||
|
||
pub fn is_property( | ||
&self, | ||
extra_properties: &[QualifiedName], | ||
semantic: &SemanticModel, | ||
) -> bool { | ||
self.as_function_def() | ||
.is_some_and(|StmtFunctionDef { decorator_list, .. }| { | ||
is_property(decorator_list, extra_properties, semantic) | ||
}) | ||
} | ||
|
||
Comment on lines
+154
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it's worth having this method in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to have it here, so that it can be easily reused by other rules that we add in the future |
||
/// Return the name of the definition. | ||
pub fn name(&self) -> Option<&'a str> { | ||
match self { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm reviewing the PRs in the wrong order. This is now behind your new iterator and no longer requires collecting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are, yes :-) This is the first PR in the stack, and the other two PRs are based on top of this branch.
But as I mentioned in #12582 (comment), I think we still need to collect ~always even with my new iterator, because the common pattern seems to be to do something like this:
If
extra_properties
is an iterator there, it could be exhausted after the first iteration of the outerfor
loopThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can
Clone
the iterator to avoid thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good point.