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..c23c1ec2 100644 --- a/pyi.py +++ b/pyi.py @@ -299,8 +299,9 @@ 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 # 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 "..."',