Skip to content
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

get rid of Y090 and Y091 #87

Merged
merged 5 commits into from
Jan 16, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change Log
unreleased
~~~~~~~~~~

* extend Y010 to cover what was previously included in Y090 (disallow
assignments in ``__init__`` methods) and Y091 (disallow ``raise``
statements). The previous checks were disabled by default.
* detect usage of non-integer indices in sys.version_info checks
* extend Y010 to check async functions in addition to normal functions
* introduce Y093 (require using TypeAlias for type aliases)
Expand Down
17 changes: 1 addition & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ currently emitted:
* Y009: Empty body should contain "...", not "pass". This is just a stylistic
choice, but it's the one typeshed made.
* Y010: Function body must contain only "...". Stub files should not contain
code, so function bodies should be empty. Currently, we make exceptions for
raise statements and for assignments in `__init__` methods.
code, so function bodies should be empty.
* Y011: All default values for typed function arguments must be "...". Type
checkers ignore the default value, so the default value is not useful
information in a stub file.
Expand All @@ -75,20 +74,6 @@ currently emitted:

The following warnings are disabled by default:

* Y090: Use explicit attributes instead of assignments in `__init__`. This
is a stricter version of Y010. Instead of::

class Thing:
def __init__(self, x: str) -> None:
self.x = x

you should write::

class Thing:
x: str
def __init__(self, x: str) -> None: ...

* Y091: Function body must not contain "raise".
* Y092: Top-level attribute must not have a default value.
* Y093: Type aliases should be explicitly demarcated with ``typing.TypeAlias``.

Expand Down
13 changes: 1 addition & 12 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,6 @@ def _visit_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
statement.value, ast.Ellipsis
):
continue
# 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)
if node.name == "__init__":
self.error(statement, Y090)
continue
self.error(statement, Y010)

def visit_arguments(self, node: ast.arguments) -> None:
Expand Down Expand Up @@ -569,9 +560,7 @@ def should_warn(self, code):
Y016 = "Y016 Duplicate union member"
Y017 = "Y017 Only simple assignments allowed"
Y018 = 'Y018 {typevarlike_cls} "{typevar_name}" is not used'
Y090 = "Y090 Use explicit attributes instead of assignments in __init__"
Y091 = 'Y091 Function body must not contain "raise"'
Y092 = "Y092 Top-level attribute must not have a default value"
Y093 = "Y093 Use typing_extensions.TypeAlias for type aliases"

DISABLED_BY_DEFAULT = [Y090, Y091, Y092, Y093]
DISABLED_BY_DEFAULT = [Y092, Y093]
17 changes: 10 additions & 7 deletions tests/emptyfunctions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ def passing(x: int) -> float:
pass # Y009 Empty body should contain "...", not "pass"

def raising(x: int) -> float:
raise TypeError
raise TypeError # Y010 Function body must contain only "..."

class GoodClass:
class BadClass:
def __init__(self, x: int) -> None:
self.x = x
self.x = x # Y010 Function body must contain only "..."

class BadClass:
def __init__(self, b: GoodClass, x: int) -> None:
b.x = x
async def async_assign(self, x: int) -> None:
self.x = x # Y010 Function body must contain only "..."

class WorseClass:
def __init__(self, b: BadClass, x: int) -> None:
b.x = x # Y010 Function body must contain only "..."
Akuli marked this conversation as resolved.
Show resolved Hide resolved

def returning(x: int) -> float:
return x / 2 # Y010 Function body must contain only "..."
Expand All @@ -29,7 +32,7 @@ async def passing_async(x: int) -> float:
pass # Y009 Empty body should contain "...", not "pass"

async def raising_async(x: int) -> float:
raise TypeError
raise TypeError # Y010 Function body must contain only "..."

async def returning_async(x: int) -> float:
return x / 2 # Y010 Function body must contain only "..."
Expand Down
4 changes: 0 additions & 4 deletions tests/emptyinit.pyi

This file was deleted.

7 changes: 0 additions & 7 deletions tests/raise.pyi

This file was deleted.