Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
6 changes: 4 additions & 2 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
6 changes: 6 additions & 0 deletions tests/raise.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo:
def __init__(self) -> None:
raise ValueError() # Y091

def foo(self) -> None:
raise ValueError() # Y091
10 changes: 10 additions & 0 deletions tests/test_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "..."',
Expand Down