-
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
[flake8-pyi
] Implement PYI063
#11699
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
079cb96
[`flake8-pyi`] Implement `PYI063`
tusharsadhwani f3d403b
Merge branch 'main' into pyi-063
charliermarsh 25bf53e
Rename to PEP 570
charliermarsh bb00f86
Docs
charliermarsh 8456ee9
Merge branch 'main' into pyi-063
charliermarsh 20662ec
Docs
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI063.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# See https://peps.python.org/pep-0484/#positional-only-arguments | ||
# for the full details on which arguments using the older syntax should/shouldn't | ||
# be considered positional-only arguments by type checkers. | ||
from typing import Self | ||
|
||
def bad(__x: int) -> None: ... # PYI063 | ||
def also_bad(__x: int, __y: str) -> None: ... # PYI063 | ||
def still_bad(__x_: int) -> None: ... # PYI063 | ||
|
||
def no_args() -> None: ... | ||
def okay(__x__: int) -> None: ... | ||
# The first argument isn't positional-only, so logically the second can't be either: | ||
def also_okay(x: int, __y: str) -> None: ... | ||
def fine(x: bytes, /) -> None: ... | ||
def no_idea_why_youd_do_this(__x: int, /, __y: str) -> None: ... | ||
def cool(_x__: int) -> None: ... | ||
def also_cool(x__: int) -> None: ... | ||
def unclear_from_pep_484_if_this_is_positional_or_not(__: str) -> None: ... | ||
def _(_: int) -> None: ... | ||
|
||
class Foo: | ||
def bad(__self) -> None: ... # PYI063 | ||
@staticmethod | ||
def bad2(__self) -> None: ... # PYI063 | ||
def bad3(__self, __x: int) -> None: ... # PYI063 | ||
def still_bad(self, __x_: int) -> None: ... # PYI063 | ||
@staticmethod | ||
def this_is_bad_too(__x: int) -> None: ... # PYI063 | ||
@classmethod | ||
def not_good(cls, __foo: int) -> None: ... # PYI063 | ||
|
||
# The first non-self argument isn't positional-only, so logically the second can't be either: | ||
def okay1(self, x: int, __y: int) -> None: ... | ||
# Same here: | ||
@staticmethod | ||
def okay2(x: int, __y_: int) -> None: ... | ||
@staticmethod | ||
def no_args() -> int: ... | ||
def okay3(__self__, __x__: int, __y: str) -> None: ... | ||
def okay4(self, /) -> None: ... | ||
def okay5(self, x: int, /) -> None: ... | ||
def okay6(__self, /) -> None: ... | ||
def cool(_self__: int) -> None: ... | ||
def also_cool(self__: int) -> None: ... | ||
def unclear_from_pep_484_if_this_is_positional_or_not(__: str) -> None: ... | ||
def _(_: int) -> None: ... | ||
@classmethod | ||
def fine(cls, foo: int, /) -> None: ... | ||
|
||
class Metaclass(type): | ||
@classmethod | ||
def __new__(mcls, __name: str, __bases: tuple[type, ...], __namespace: dict, **kwds) -> Self: ... # PYI063 | ||
|
||
class Metaclass2(type): | ||
@classmethod | ||
def __new__(metacls, __name: str, __bases: tuple[type, ...], __namespace: dict, **kwds) -> Self: ... # PYI063 | ||
|
||
class GoodMetaclass(type): | ||
@classmethod | ||
def __new__(mcls, name: str, bases: tuple[type, ...], namespace: dict, /, **kwds) -> Self: ... | ||
|
||
class GoodMetaclass2(type): | ||
@classmethod | ||
def __new__(metacls, name: str, bases: tuple[type, ...], namespace: dict, /, **kwds) -> Self: ... |
64 changes: 64 additions & 0 deletions
64
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI063.pyi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# See https://peps.python.org/pep-0484/#positional-only-arguments | ||
# for the full details on which arguments using the older syntax should/shouldn't | ||
# be considered positional-only arguments by type checkers. | ||
from typing import Self | ||
|
||
def bad(__x: int) -> None: ... # PYI063 | ||
def also_bad(__x: int, __y: str) -> None: ... # PYI063 | ||
def still_bad(__x_: int) -> None: ... # PYI063 | ||
|
||
def no_args() -> None: ... | ||
def okay(__x__: int) -> None: ... | ||
# The first argument isn't positional-only, so logically the second can't be either: | ||
def also_okay(x: int, __y: str) -> None: ... | ||
def fine(x: bytes, /) -> None: ... | ||
def no_idea_why_youd_do_this(__x: int, /, __y: str) -> None: ... | ||
def cool(_x__: int) -> None: ... | ||
def also_cool(x__: int) -> None: ... | ||
def unclear_from_pep_484_if_this_is_positional_or_not(__: str) -> None: ... | ||
def _(_: int) -> None: ... | ||
|
||
class Foo: | ||
def bad(__self) -> None: ... # PYI063 | ||
@staticmethod | ||
def bad2(__self) -> None: ... # PYI063 | ||
def bad3(__self, __x: int) -> None: ... # PYI063 | ||
def still_bad(self, __x_: int) -> None: ... # PYI063 # TODO: doesn't get raised here | ||
@staticmethod | ||
def this_is_bad_too(__x: int) -> None: ... # PYI063 | ||
@classmethod | ||
def not_good(cls, __foo: int) -> None: ... # PYI063 | ||
|
||
# The first non-self argument isn't positional-only, so logically the second can't be either: | ||
def okay1(self, x: int, __y: int) -> None: ... | ||
# Same here: | ||
@staticmethod | ||
def okay2(x: int, __y_: int) -> None: ... | ||
@staticmethod | ||
def no_args() -> int: ... | ||
def okay3(__self__, __x__: int, __y: str) -> None: ... | ||
def okay4(self, /) -> None: ... | ||
def okay5(self, x: int, /) -> None: ... | ||
def okay6(__self, /) -> None: ... | ||
def cool(_self__: int) -> None: ... | ||
def also_cool(self__: int) -> None: ... | ||
def unclear_from_pep_484_if_this_is_positional_or_not(__: str) -> None: ... | ||
def _(_: int) -> None: ... | ||
@classmethod | ||
def fine(cls, foo: int, /) -> None: ... | ||
|
||
class Metaclass(type): | ||
@classmethod | ||
def __new__(mcls, __name: str, __bases: tuple[type, ...], __namespace: dict, **kwds) -> Self: ... # PYI063 | ||
|
||
class Metaclass2(type): | ||
@classmethod | ||
def __new__(metacls, __name: str, __bases: tuple[type, ...], __namespace: dict, **kwds) -> Self: ... # PYI063 | ||
|
||
class GoodMetaclass(type): | ||
@classmethod | ||
def __new__(mcls, name: str, bases: tuple[type, ...], namespace: dict, /, **kwds) -> Self: ... | ||
|
||
class GoodMetaclass2(type): | ||
@classmethod | ||
def __new__(metacls, name: str, bases: tuple[type, ...], namespace: dict, /, **kwds) -> Self: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
crates/ruff_linter/src/rules/flake8_pyi/rules/pre_pep570_positional_argument.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::identifier::Identifier; | ||
use ruff_python_ast::{self as ast, ParameterWithDefault}; | ||
use ruff_python_semantic::analyze::function_type; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::settings::types::PythonVersion; | ||
|
||
/// ## What it does | ||
/// Checks for the presence of [PEP 484]-style positional-only arguments. | ||
/// | ||
/// ## Why is this bad? | ||
/// Historically, [PEP 484] recommended prefixing positional-only arguments | ||
/// with a double underscore (`__`). However, [PEP 570] introduced a dedicated | ||
/// syntax for positional-only arguments, which should be preferred. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// def foo(__x: int) -> None: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```python | ||
/// def foo(x: int, /) -> None: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// [PEP 484]: https://peps.python.org/pep-0484/#positional-only-arguments | ||
/// [PEP 570]: https://peps.python.org/pep-0570 | ||
#[violation] | ||
pub struct PrePep570PositionalArgument; | ||
|
||
impl Violation for PrePep570PositionalArgument { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use PEP 570 syntax for positional-only arguments") | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some("Add `/` to function signature".to_string()) | ||
} | ||
} | ||
|
||
/// PYI063 | ||
pub(crate) fn pre_pep570_positional_argument( | ||
checker: &mut Checker, | ||
function_def: &ast::StmtFunctionDef, | ||
) { | ||
// PEP 570 was introduced in Python 3.8. | ||
if checker.settings.target_version < PythonVersion::Py38 { | ||
return; | ||
} | ||
|
||
if !function_def.parameters.posonlyargs.is_empty() { | ||
return; | ||
} | ||
|
||
if function_def.parameters.args.is_empty() { | ||
return; | ||
} | ||
|
||
let semantic = checker.semantic(); | ||
let scope = semantic.current_scope(); | ||
let function_type = function_type::classify( | ||
&function_def.name, | ||
&function_def.decorator_list, | ||
scope, | ||
semantic, | ||
&checker.settings.pep8_naming.classmethod_decorators, | ||
&checker.settings.pep8_naming.staticmethod_decorators, | ||
); | ||
|
||
// If the method has a `self` or `cls` argument, skip it. | ||
let skip = usize::from(matches!( | ||
function_type, | ||
function_type::FunctionType::Method | function_type::FunctionType::ClassMethod | ||
)); | ||
|
||
if let Some(arg) = function_def.parameters.args.get(skip) { | ||
if is_pre_pep570_positional_only(arg) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
PrePep570PositionalArgument, | ||
arg.identifier(), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
/// Returns `true` if the [`ParameterWithDefault`] is an old-style positional-only argument (i.e., | ||
/// its name starts with `__` and does not end with `__`). | ||
fn is_pre_pep570_positional_only(arg: &ParameterWithDefault) -> bool { | ||
let arg_name = &arg.parameter.name; | ||
arg_name.starts_with("__") && !arg_name.ends_with("__") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can I suggest the test case
This should be fine right?
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.
seems good, I'll add it.