PK!flake8_return/__init__.pyPK!Kͫflake8_return/errors.pyfrom flake8_plugin_utils import Error class UnnecessaryReturnNone(Error): code = 'R501' message = ( 'you shouldn`t add None at any return ' 'if function havn`t return value except None' ) class ImplicitReturnValue(Error): code = 'R502' message = ( 'you should add explicit value at every return ' 'if function have return value except None' ) class ImplicitReturn(Error): code = 'R503' message = ( 'you should add explicit return at end of the function ' 'if function have return value except None' ) class UnnecessaryAssign(Error): code = 'R504' message = ( 'you shouldn`t assign value to variable ' 'if it will be use only as return value' ) PK!flake8_return/plugin.pyfrom flake8_plugin_utils import Plugin from .visitors import ReturnVisitor __version__ = '0.3.1' class ReturnPlugin(Plugin): name = 'flake8-return' version = __version__ visitors = [ReturnVisitor] PK!))iiflake8_return/visitors.pyimport ast from collections import defaultdict from typing import Any, Dict, List, Optional, Union from flake8_plugin_utils import Visitor from .errors import ( ImplicitReturn, ImplicitReturnValue, UnnecessaryAssign, UnnecessaryReturnNone, ) NameToLines = Dict[str, List[int]] Function = Union[ast.AsyncFunctionDef, ast.FunctionDef] ASSIGNS = 'assigns' REFS = 'refs' RETURNS = 'returns' class ReturnVisitor(Visitor): def __init__(self) -> None: super().__init__() self._stack: List[Any] = [] @property def assigns(self) -> NameToLines: return self._stack[-1][ASSIGNS] @property def refs(self) -> NameToLines: return self._stack[-1][REFS] @property def returns(self) -> List[ast.Return]: return self._stack[-1][RETURNS] def visit_FunctionDef(self, node: ast.FunctionDef) -> None: self._visit_with_stask(node) def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None: self._visit_with_stask(node) def _visit_with_stask(self, node: Function) -> None: self._stack.append( {ASSIGNS: defaultdict(list), REFS: defaultdict(list), RETURNS: []} ) self.generic_visit(node) self._check_function(node) self._stack.pop() def visit_Return(self, node: ast.Return) -> None: self.returns.append(node) self.generic_visit(node) def visit_Assign(self, node: ast.Assign) -> None: if not self._stack: return for t in node.targets: self._visit_assign_target(t) self.generic_visit(node.value) def _visit_assign_target(self, node: ast.AST) -> None: if isinstance(node, ast.Tuple): for n in node.elts: self._visit_assign_target(n) return if isinstance(node, ast.Name): self.assigns[node.id].append(node.lineno) return # get item, etc. self.generic_visit(node) def visit_Name(self, node: ast.Name) -> None: if self._stack: self.refs[node.id].append(node.lineno) def _check_function(self, node: Function) -> None: if not self.returns or not node.body: return if len(node.body) == 1 and isinstance(node.body[-1], ast.Return): # skip functions that consist only `return None` return if not self._result_exists(): self._check_unnecessary_return_none() return self._check_implicit_return_value() self._check_implicit_return(node.body[-1]) for n in self.returns: if n.value: self._check_unnecessary_assign(n.value) def _result_exists(self) -> bool: for node in self.returns: value = node.value if value and not _is_none(value): return True return False def _check_implicit_return_value(self) -> None: for node in self.returns: if not node.value: self.error_from_node(ImplicitReturnValue, node) def _check_unnecessary_return_none(self) -> None: for node in self.returns: if _is_none(node.value): self.error_from_node(UnnecessaryReturnNone, node) def _check_implicit_return(self, last_node: ast.AST) -> None: if isinstance(last_node, ast.If): if not last_node.body or not last_node.orelse: self.error_from_node(ImplicitReturn, last_node) return self._check_implicit_return(last_node.body[-1]) self._check_implicit_return(last_node.orelse[-1]) return if isinstance(last_node, ast.For) and last_node.orelse: self._check_implicit_return(last_node.orelse[-1]) return if isinstance(last_node, ast.With): self._check_implicit_return(last_node.body[-1]) return if not isinstance( last_node, (ast.Return, ast.Raise, ast.While, ast.Try) ): self.error_from_node(ImplicitReturn, last_node) def _check_unnecessary_assign(self, node: ast.AST) -> None: if not isinstance(node, ast.Name): return var_name = node.id return_lineno = node.lineno if var_name not in self.assigns: return if var_name not in self.refs: self.error_from_node(UnnecessaryAssign, node) return if self._has_refs_before_next_assign(var_name, return_lineno): return self.error_from_node(UnnecessaryAssign, node) def _has_refs_before_next_assign( self, var_name: str, return_lineno: int ) -> bool: before_assign = 0 after_assign: Optional[int] = None for lineno in sorted(self.assigns[var_name]): if lineno > return_lineno: after_assign = lineno break if lineno <= return_lineno: before_assign = lineno for lineno in self.refs[var_name]: if lineno == return_lineno: continue if after_assign: if before_assign < lineno <= after_assign: return True elif before_assign < lineno: return True return False def _is_none(node: Optional[ast.AST]) -> bool: return isinstance(node, ast.NameConstant) and node.value is None PK!Hd1:.flake8_return-0.3.1.dist-info/entry_points.txtNINK(I+ϋ 25䔦gYy`PK!611%flake8_return-0.3.1.dist-info/LICENSEMIT License Copyright (c) 2019 Afonasev Evgeniy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTU#flake8_return-0.3.1.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hnő &flake8_return-0.3.1.dist-info/METADATAV[o6~ׯ8E04ޢ.ی[tX n/Pұę"5CJe'Y``Pw /вYp%0{V-P 6$:&m]L7Sɣu%؂YH LZCX1Q ~V%qameq\5J0.OQ¿ZZ£{VB)-dWvMbɸur7 nk4uC` qtrUy'S*ҽ}~yZ\2+&-?{%@j^Yj\I҆jjALeu[Gds-jD\Q2Ef﬑ך֐yd "3Ɠ-4tpN| R(G8+XfM%<@cEV7aڊ={bf2-NKf,xs!74 ڧ4M.ܼxe7-cKf[$8M]mg2#/vN].P=-6ƥDM{K;5-h v]֕1J=3ot}}^] >t.zPQhLBA3N5f7u'MjGR<ߺ\ti#)]N(ƃdK .CsIc"PK!H$flake8_return-0.3.1.dist-info/RECORDI@},Ar AeC&&$WveUZB)AN)p,oR+E])Fт3Xn)+{ B-`(2ǕP <*EVpM>=<Z/h~p҆%#Yȶʊ=4qgXt|dbǖ7(a>W(PK!flake8_return/__init__.pyPK!Kͫ7flake8_return/errors.pyPK!hflake8_return/plugin.pyPK!))iirflake8_return/visitors.pyPK!Hd1:.flake8_return-0.3.1.dist-info/entry_points.txtPK!611%flake8_return-0.3.1.dist-info/LICENSEPK!HڽTU#flake8_return-0.3.1.dist-info/WHEELPK!Hnő &flake8_return-0.3.1.dist-info/METADATAPK!H$m$flake8_return-0.3.1.dist-info/RECORDPK i&