Skip to content

Commit

Permalink
[flake8-pyi]: PYI011, PYI014 (#3238)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbdchd committed Feb 26, 2023
1 parent 5f83851 commit 3a78b59
Show file tree
Hide file tree
Showing 15 changed files with 759 additions and 0 deletions.
79 changes: 79 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.py
@@ -0,0 +1,79 @@
def f12(
x,
y: str = os.pathsep, # OK
) -> None:
...


def f11(*, x: str = "x") -> None: # OK
...


def f13(
x: list[str] = [
"foo",
"bar",
"baz",
] # OK
) -> None:
...


def f14(
x: tuple[str, ...] = (
"foo",
"bar",
"baz",
) # OK
) -> None:
...


def f15(
x: set[str] = {
"foo",
"bar",
"baz",
} # OK
) -> None:
...


def f16(x: frozenset[bytes] = frozenset({b"foo", b"bar", b"baz"})) -> None: # OK
...


def f17(
x: str = "foo" + "bar", # OK
) -> None:
...


def f18(
x: str = b"foo" + b"bar", # OK
) -> None:
...


def f19(
x: object = "foo" + 4, # OK
) -> None:
...


def f20(
x: int = 5 + 5, # OK
) -> None:
...


def f21(
x: complex = 3j - 3j, # OK
) -> None:
...


def f22(
x: complex = -42.5j + 4.3j, # OK
) -> None:
...
63 changes: 63 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi
@@ -0,0 +1,63 @@
def f12(
x,
y: str = os.pathsep, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f11(*, x: str = "x") -> None: ... # OK
def f13(
x: list[
str
] = [ # Error PYI011 Only simple default values allowed for typed arguments
"foo",
"bar",
"baz",
]
) -> None: ...
def f14(
x: tuple[
str, ...
] = ( # Error PYI011 Only simple default values allowed for typed arguments
"foo",
"bar",
"baz",
)
) -> None: ...
def f15(
x: set[
str
] = { # Error PYI011 Only simple default values allowed for typed arguments
"foo",
"bar",
"baz",
}
) -> None: ...
def f16(
x: frozenset[
bytes
] = frozenset( # Error PYI011 Only simple default values allowed for typed arguments
{b"foo", b"bar", b"baz"}
)
) -> None: ...
def f17(
x: str = "foo" # Error PYI011 Only simple default values allowed for typed arguments
+ "bar",
) -> None: ...
def f18(
x: str = b"foo" # Error PYI011 Only simple default values allowed for typed arguments
+ b"bar",
) -> None: ...
def f19(
x: object = "foo" # Error PYI011 Only simple default values allowed for typed arguments
+ 4,
) -> None: ...
def f20(
x: int = 5
+ 5, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f21(
x: complex = 3j
- 3j, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f22(
x: complex = -42.5j # Error PYI011 Only simple default values allowed for typed arguments
+ 4.3j,
) -> None: ...
79 changes: 79 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.py
@@ -0,0 +1,79 @@
def f12(
x,
y=os.pathsep, # OK
) -> None:
...


def f11(*, x="x") -> None:
... # OK


def f13(
x=[ # OK
"foo",
"bar",
"baz",
]
) -> None:
...


def f14(
x=( # OK
"foo",
"bar",
"baz",
)
) -> None:
...


def f15(
x={ # OK
"foo",
"bar",
"baz",
}
) -> None:
...


def f16(x=frozenset({b"foo", b"bar", b"baz"})) -> None:
... # OK


def f17(
x="foo" + "bar", # OK
) -> None:
...


def f18(
x=b"foo" + b"bar", # OK
) -> None:
...


def f19(
x="foo" + 4, # OK
) -> None:
...


def f20(
x=5 + 5, # OK
) -> None:
...


def f21(
x=3j - 3j, # OK
) -> None:
...


def f22(
x=-42.5j + 4.3j, # OK
) -> None:
...
45 changes: 45 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.pyi
@@ -0,0 +1,45 @@
def f12(
x,
y=os.pathsep, # Error PYI014
) -> None: ...
def f11(*, x="x") -> None: ... # OK
def f13(
x=[ # Error PYI014
"foo",
"bar",
"baz",
]
) -> None: ...
def f14(
x=( # Error PYI014
"foo",
"bar",
"baz",
)
) -> None: ...
def f15(
x={ # Error PYI014
"foo",
"bar",
"baz",
}
) -> None: ...
def f16(x=frozenset({b"foo", b"bar", b"baz"})) -> None: ... # Error PYI014
def f17(
x="foo" + "bar", # Error PYI014
) -> None: ...
def f18(
x=b"foo" + b"bar", # Error PYI014
) -> None: ...
def f19(
x="foo" + 4, # Error PYI014
) -> None: ...
def f20(
x=5 + 5, # Error PYI014
) -> None: ...
def f21(
x=3j - 3j, # Error PYI014
) -> None: ...
def f22(
x=-42.5j + 4.3j, # Error PYI014
) -> None: ...
15 changes: 15 additions & 0 deletions crates/ruff/src/checkers/ast.rs
Expand Up @@ -3930,6 +3930,21 @@ where
flake8_bugbear::rules::function_call_argument_default(self, arguments);
}

if self.is_interface_definition {
if self
.settings
.rules
.enabled(&Rule::TypedArgumentSimpleDefaults)
{
flake8_pyi::rules::typed_argument_simple_defaults(self, arguments);
}
}
if self.is_interface_definition {
if self.settings.rules.enabled(&Rule::ArgumentSimpleDefaults) {
flake8_pyi::rules::argument_simple_defaults(self, arguments);
}
}

// Bind, but intentionally avoid walking default expressions, as we handle them
// upstream.
for arg in &arguments.posonlyargs {
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/codes.rs
Expand Up @@ -489,6 +489,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Pyi, "008") => Rule::UnrecognizedPlatformName,
(Flake8Pyi, "009") => Rule::PassStatementStubBody,
(Flake8Pyi, "010") => Rule::NonEmptyStubBody,
(Flake8Pyi, "011") => Rule::TypedArgumentSimpleDefaults,
(Flake8Pyi, "014") => Rule::ArgumentSimpleDefaults,
(Flake8Pyi, "021") => Rule::DocstringInStub,

// flake8-pytest-style
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/registry.rs
Expand Up @@ -464,6 +464,8 @@ ruff_macros::register_rules!(
rules::flake8_pyi::rules::PassStatementStubBody,
rules::flake8_pyi::rules::NonEmptyStubBody,
rules::flake8_pyi::rules::DocstringInStub,
rules::flake8_pyi::rules::TypedArgumentSimpleDefaults,
rules::flake8_pyi::rules::ArgumentSimpleDefaults,
// flake8-pytest-style
rules::flake8_pytest_style::rules::IncorrectFixtureParenthesesStyle,
rules::flake8_pytest_style::rules::FixturePositionalArgs,
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/mod.rs
Expand Up @@ -23,6 +23,10 @@ mod tests {
#[test_case(Rule::NonEmptyStubBody, Path::new("PYI010.pyi"))]
#[test_case(Rule::PassStatementStubBody, Path::new("PYI009.py"))]
#[test_case(Rule::PassStatementStubBody, Path::new("PYI009.pyi"))]
#[test_case(Rule::TypedArgumentSimpleDefaults, Path::new("PYI011.py"))]
#[test_case(Rule::TypedArgumentSimpleDefaults, Path::new("PYI011.pyi"))]
#[test_case(Rule::ArgumentSimpleDefaults, Path::new("PYI014.py"))]
#[test_case(Rule::ArgumentSimpleDefaults, Path::new("PYI014.pyi"))]
#[test_case(Rule::DocstringInStub, Path::new("PYI021.py"))]
#[test_case(Rule::DocstringInStub, Path::new("PYI021.pyi"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/rules/mod.rs
Expand Up @@ -2,6 +2,10 @@ pub use docstring_in_stubs::{docstring_in_stubs, DocstringInStub};
pub use non_empty_stub_body::{non_empty_stub_body, NonEmptyStubBody};
pub use pass_statement_stub_body::{pass_statement_stub_body, PassStatementStubBody};
pub use prefix_type_params::{prefix_type_params, PrefixTypeParams};
pub use simple_defaults::{
argument_simple_defaults, typed_argument_simple_defaults, ArgumentSimpleDefaults,
TypedArgumentSimpleDefaults,
};
pub use unrecognized_platform::{
unrecognized_platform, UnrecognizedPlatformCheck, UnrecognizedPlatformName,
};
Expand All @@ -11,3 +15,5 @@ mod non_empty_stub_body;
mod pass_statement_stub_body;
mod prefix_type_params;
mod unrecognized_platform;

mod simple_defaults;

0 comments on commit 3a78b59

Please sign in to comment.