From 452ec4f591ee4dde6d8be6db9b0cc3c3818deb92 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 13 Oct 2019 11:24:01 +0200 Subject: [PATCH 1/2] Add check Y091 for raise in function bodies Closes #29 --- README.rst | 1 + pyi.py | 4 +++- tests/raise.pyi | 6 ++++++ tests/test_pyi.py | 10 ++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/raise.pyi diff --git a/README.rst b/README.rst index ebc3fba0..39da360e 100644 --- a/README.rst +++ b/README.rst @@ -81,6 +81,7 @@ The following warnings are disabled by default: x: str def __init__(self, x: str) -> None: ... +* Y091: Function body must not contain "raise". License ------- diff --git a/pyi.py b/pyi.py index d0a39f51..ea3010e3 100644 --- a/pyi.py +++ b/pyi.py @@ -301,6 +301,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None: continue # allow "raise", a number of stubs have this if isinstance(statement, ast.Raise): + self.error(statement, Y091) continue # allow assignments in constructor for now # (though these should probably be changed) @@ -418,5 +419,6 @@ def should_warn(self, code): Y013 = 'Y013 Non-empty class body must not contain "..."' Y014 = 'Y014 Default values for arguments must be "..."' Y090 = "Y090 Use explicit attributes instead of assignments in __init__" +Y091 = 'Y091 Function body must not contain "raise"' -DISABLED_BY_DEFAULT = [Y090] +DISABLED_BY_DEFAULT = [Y090, Y091] diff --git a/tests/raise.pyi b/tests/raise.pyi new file mode 100644 index 00000000..b8fb5e56 --- /dev/null +++ b/tests/raise.pyi @@ -0,0 +1,6 @@ +class Foo: + def __init__(self) -> None: + raise ValueError() # Y091 + + def foo(self) -> None: + raise ValueError() # Y091 diff --git a/tests/test_pyi.py b/tests/test_pyi.py index c169067e..1140845a 100644 --- a/tests/test_pyi.py +++ b/tests/test_pyi.py @@ -191,6 +191,16 @@ def test_empty_init(self) -> None: "emptyinit.pyi", stdout_lines=stdout_lines, extra_options=("--select=Y090",) ) + def test_raise_in_function_body(self) -> None: + self.checkFileOutput("raise.pyi", stdout_lines=()) + stdout_lines = ( + '3:9: Y091 Function body must not contain "raise"', + '6:9: Y091 Function body must not contain "raise"', + ) + self.checkFileOutput( + "raise.pyi", stdout_lines=stdout_lines, extra_options=("--select=Y091",) + ) + def test_defaults(self) -> None: stdout_lines = ( '3:17: Y011 Default values for typed arguments must be "..."', From 058e080a5ed585f54afab93cbd553a7646960c07 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 15 Oct 2019 11:40:29 +0200 Subject: [PATCH 2/2] Correct comment --- pyi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyi.py b/pyi.py index ea3010e3..c23c1ec2 100644 --- a/pyi.py +++ b/pyi.py @@ -299,7 +299,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None: statement.value, ast.Ellipsis ): continue - # allow "raise", a number of stubs have this + # special-case raise for backwards compatibility if isinstance(statement, ast.Raise): self.error(statement, Y091) continue