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

Become compatible with flake8 5.0.0 #259

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ max-line-length = 80
max-complexity = 12
select = B,C,E,F,W,Y,B9
per-file-ignores =
*.py: E203, E501, W503, W291, W293
*.pyi: E301, E302, E305, E501, E701, E704, W503
# TODO: reenable some of the bugbear checks
*.py: B950, E203, E501, W503, W291, W293
*.pyi: B902, B903, B950, E301, E302, E305, E501, E701, E704, W503
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Bugfixes:
and a `...` or `pass` statement.

Other changes:
* Pin required flake8 version to <5.0.0 (flake8-pyi is not currently compatible with flake8 5.0.0).
* Add support for flake8 5.0.0.

## 22.7.0

Expand Down
63 changes: 37 additions & 26 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple

from flake8 import checker # type: ignore[import]
import flake8 # type: ignore[import]
from flake8 import checker
from flake8.options.manager import OptionManager # type: ignore[import]
from flake8.plugins.manager import Plugin # type: ignore[import]
from flake8.plugins.pyflakes import FlakesChecker # type: ignore[import]
from pyflakes.checker import (
PY2,
ClassDefinition,
ClassScope,
FunctionScope,
ModuleScope,
)
from pyflakes.checker import ClassDefinition, ClassScope, FunctionScope, ModuleScope

if sys.version_info >= (3, 9):
from ast import unparse
Expand All @@ -48,6 +42,7 @@ def unparse(node: ast.AST) -> str:
__version__ = "22.7.0"

LOG = logging.getLogger("flake8.pyi")
FLAKE8_MAJOR_VERSION = flake8.__version_info__[0]


class Error(NamedTuple):
Expand Down Expand Up @@ -223,9 +218,8 @@ def CLASSDEF(self, node: ast.ClassDef) -> None:
self.handleNode(decorator, node)
for baseNode in node.bases:
self.deferHandleNode(baseNode, node)
if not PY2:
for keywordNode in node.keywords:
self.deferHandleNode(keywordNode, node)
for keywordNode in node.keywords:
self.deferHandleNode(keywordNode, node)
self.pushScope(ClassScope)
# doctest does not process doctest within a doctest
# classes within classes are processed.
Expand All @@ -249,21 +243,38 @@ def handleNodeDelete(self, node: ast.AST) -> None:


class PyiAwareFileChecker(checker.FileChecker):
def run_check(self, plugin: Plugin, **kwargs: Any) -> Any:
if self.filename == "-":
filename = self.options.stdin_display_name
else:
filename = self.filename
if FLAKE8_MAJOR_VERSION < 5:

if filename.endswith(".pyi") and plugin["plugin"] == FlakesChecker:
LOG.info(
"Replacing FlakesChecker with PyiAwareFlakesChecker while "
"checking %r",
filename,
)
plugin = dict(plugin)
plugin["plugin"] = PyiAwareFlakesChecker
return super().run_check(plugin, **kwargs)
def run_check(self, plugin, **kwargs: Any) -> Any:
if self.filename == "-":
filename = self.options.stdin_display_name
else:
filename = self.filename

if filename.endswith(".pyi") and plugin["plugin"] is FlakesChecker:
LOG.info(
f"Replacing FlakesChecker with PyiAwareFlakesChecker while "
f"checking {filename!r}",
)
plugin = dict(plugin)
plugin["plugin"] = PyiAwareFlakesChecker
return super().run_check(plugin, **kwargs)

else:

def run_check(self, plugin, **kwargs: Any) -> Any:
if self.filename == "-":
filename = self.options.stdin_display_name
else:
filename = self.filename

if filename.endswith(".pyi") and plugin.obj is FlakesChecker:
LOG.info(
f"Replacing FlakesChecker with PyiAwareFlakesChecker while "
f"checking {filename!r}",
)
plugin = plugin._replace(obj=PyiAwareFlakesChecker)
return super().run_check(plugin, **kwargs)


class LegacyNormalizer(ast.NodeTransformer):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
zip_safe=False,
python_requires=">=3.7",
install_requires=[
"flake8 >= 3.2.1, < 5.0.0",
"flake8 >= 3.2.1",
"pyflakes >= 2.1.1",
'ast-decompiler <1.0; python_version < "3.9"',
],
Expand Down
2 changes: 1 addition & 1 deletion tests/typevar.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BadClass:
def bad_class_method(cls: type[_S], arg: int) -> _S: ... # Y019 Use "_typeshed.Self" instead of "_S", e.g. "def bad_class_method(cls: type[Self], arg: int) -> Self: ..."

class GoodClass:
def __new__(cls: type[Self], *args: list[int], **kwargs: set[str]) -> Self: ...
def __new__(cls: type[Self], *args: list[int], **kwargs: set[str]) -> Self: ...
def good_instance_method_1(self: Self, arg: bytes) -> Self: ...
def good_instance_method_2(self, arg1: _S, arg2: _S) -> _S: ...
@classmethod
Expand Down