Static Python code analyzer, that tries to check if functions in code are pure or not and why.
DISCLAIMER: this library is very experimental and has a lot of edge cases. Functions that mr. Proper marks as pure can be not pure, but they are usually cleaner than other functions.
pip install mr_proper
- that function has no blacklisted calls (like
print
) and blacklisted attributes access (likesmth.count
); - that function not uses global objects (only local vars and function arguments);
- that function has al least one return;
- that function not mutates it's arguments;
- that function has no local imports;
- that function has no arguments of forbidden types (like ORM objects);
- that function not uses
self
,class
orsuper
; - that function has calls of only pure functions.
This list is not enough to say that function is pure and some points are quite controversial, but it's a nice start.
Console usage:
# test.py
def add_one(n: int) -> int:
return n + 1
def print_amount_of_users(users_qs: QuerySet) -> None:
print(f'Current amount of users is {users_qs.count()}')
$ mr_propper test.py
add_one is pure!
print_amount_of_users is not pure because of:
it uses forbidden argument types (QuerySet)
it calls not pure functions (print)
it has no return
Usage inside Python code sample:
>>> import ast
>>> from mr_propper.utils import is_function_pure
>>> funcdef = ast.parse('''
def add_one(n: int) -> int:
return n + 1
''').body[0]
>>> is_function_pure(funcdef)
True
>>> is_function_pure(funcdef, with_errors=True)
(True, [])
CLI interface:
filepath
: path to .py file to check (directories are not supported for now);--recursive
: require inner calls to be pure for function pureness.
- Python 3.7+;
- Functions are fully type-annotated;
- No dynamic calls (like
getattr(sender, 'send_' + message_type)(message)
).
We would love you to contribute to our project. It's simple:
- Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
- Create a pull request. Make sure all checks are green.
- Fix review comments if any.
- Be awesome.
Here are useful tips:
- You can run all checks and tests with
make check
. Please do it before TravisCI does. - We use BestDoctor python styleguide.
- We respect Django CoC. Make soft, not bullshit.