-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
124 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
language: python | ||
python: | ||
- "3.6" | ||
- "3.7" | ||
- "3.8" | ||
install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import ast | ||
from typing import Tuple, Union | ||
|
||
AnyFuncdef = Union[ast.FunctionDef, ast.AsyncFunctionDef] | ||
|
||
|
||
def get_arguments_amount_for(func_def: AnyFuncdef) -> int: | ||
arguments_amount = 0 | ||
args = func_def.args | ||
arguments_amount += len(args.args) + len(args.kwonlyargs) | ||
if args.vararg: | ||
arguments_amount += 1 | ||
if args.kwarg: | ||
arguments_amount += 1 | ||
return arguments_amount | ||
|
||
|
||
def get_arguments_amount_error(func_def: AnyFuncdef, max_parameters_amount: int) -> Tuple[int, int, str]: | ||
arguments_amount = get_arguments_amount_for(func_def) | ||
if arguments_amount > max_parameters_amount: | ||
return ( | ||
func_def.lineno, | ||
func_def.col_offset, | ||
f'CFQ002 Function "{func_def.name}" has {arguments_amount} arguments' | ||
f' that exceeds max allowed {max_parameters_amount}', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
import ast | ||
from typing import Tuple, Union | ||
|
||
AnyFuncdef = Union[ast.FunctionDef, ast.AsyncFunctionDef] | ||
|
||
|
||
def get_function_start_row(func_def: AnyFuncdef) -> int: | ||
first_meaningful_expression_index = 0 | ||
if ( | ||
isinstance(func_def.body[0], ast.Expr) | ||
and isinstance(func_def.body[0].value, ast.Str) | ||
and len(func_def.body) > 1 | ||
): # First expression is docstring - we ignore it | ||
first_meaningful_expression_index = 1 | ||
return func_def.body[first_meaningful_expression_index].lineno | ||
|
||
|
||
def get_function_last_row(func_def: AnyFuncdef) -> int: | ||
function_last_line = 0 | ||
for statement in ast.walk(func_def): | ||
if hasattr(statement, 'lineno'): | ||
function_last_line = max(statement.lineno, function_last_line) | ||
|
||
return function_last_line | ||
|
||
|
||
def get_length_errors(func_def: AnyFuncdef, max_function_length: int) -> Tuple[int, int, str]: | ||
function_start_row = get_function_start_row(func_def) | ||
function_last_row = get_function_last_row(func_def) | ||
function_lenght = function_last_row - function_start_row + 1 | ||
if function_lenght > max_function_length: | ||
return ( | ||
func_def.lineno, | ||
func_def.col_offset, | ||
f'CFQ001 Function {func_def.name} has length {function_lenght}' | ||
f' that exceeds max allowed length {max_function_length}', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ast | ||
|
||
from typing import Union, Tuple | ||
|
||
from mr_proper.public_api import is_function_pure | ||
|
||
|
||
AnyFuncdef = Union[ast.FunctionDef, ast.AsyncFunctionDef] | ||
|
||
|
||
def check_purity_of_functions(func_def: AnyFuncdef) -> Tuple[int, int, str]: | ||
if 'pure' in func_def.name.split('_') and not is_function_pure(func_def): | ||
return ( | ||
func_def.lineno, | ||
func_def.col_offset, | ||
f'CFQ003 Function "{func_def.name}" is not pure.', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def not_pure_function(users_qs: QuerySet) -> None: | ||
print(f'Current amount of users is {users_qs.count()}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def pure_function(n: int) -> int: | ||
return n + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters