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 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
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## Unreleased

* Add support for flake8 5.0.0.

## 22.8.0

New error codes:
Expand Down
52 changes: 32 additions & 20 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,19 @@ def unparse(node: ast.AST) -> str:
__version__ = "22.8.0"

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

if FLAKE8_MAJOR_VERSION < 5:
import warnings

warnings.warn(
(
"flake8-pyi will drop support for running with flake8 < 5.0.0 "
"in a future version. This will not happen until November 2022 "
"at the earliest."
),
category=FutureWarning,
)
Comment on lines +50 to +57
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually verified that this warning is emitted if the plugin is run in an environment with flake8==4.x installed. Other than that, the test suite still passes for me locally with flake8==4.x installed.



class Error(NamedTuple):
Expand Down Expand Up @@ -223,9 +230,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,20 +255,26 @@ def handleNodeDelete(self, node: ast.AST) -> None:


class PyiAwareFileChecker(checker.FileChecker):
def run_check(self, plugin: Plugin, **kwargs: Any) -> Any:
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"] == FlakesChecker:
LOG.info(
"Replacing FlakesChecker with PyiAwareFlakesChecker while "
"checking %r",
filename,
if filename.endswith(".pyi"):
log_msg = (
f"Replacing FlakesChecker with PyiAwareFlakesChecker while "
f"checking {filename!r}"
)
plugin = dict(plugin)
plugin["plugin"] = PyiAwareFlakesChecker
if FLAKE8_MAJOR_VERSION < 5:
if plugin["plugin"] is FlakesChecker:
LOG.info(log_msg)
plugin = dict(plugin)
plugin["plugin"] = PyiAwareFlakesChecker
else:
if plugin.obj is FlakesChecker:
LOG.info(log_msg)
plugin = plugin._replace(obj=PyiAwareFlakesChecker)
return super().run_check(plugin, **kwargs)


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, < 6.0.0",
"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