mr. Proper
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.
Installation
pip install mr_proper
What mr. Proper check
- 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,classorsuper; - that function has calls of only pure functions.
This list is not enought to say that function is pure and some points are quite controversial, but it's a nice start.
Example
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, [])
Parameters
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.
Code prerequisites
- Python 3.7+;
- Functions are fully type-annotated;
- No dynamic calls (like
getattr(sender, 'send_' + message_type)(message)).
Contributing
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.
Release files for mr-proper 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mr_proper-0.0.7.tar.gz | 11.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mr_proper-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.6 kB
Release files / mr_proper-0.0.7.tar.gz
| Download URL | mr_proper-0.0.7.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
03b517b19e617537f711ce418b125e5f2efd82ec881539cdee83195c78c14a02
|
|
BLAKE2b-256 checksum How to use checksums |
e335a50ba9e3097ee0d71232c996c626a17f80424140b365122c8f1bc9933118
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Release files / mr_proper-0.0.7-py3-none-any.whl
| Download URL | mr_proper-0.0.7-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
74a1b60240c46f10ba518707ef72811a01e5c270da0a78b5dd2dd923d99fdb14
|
|
BLAKE2b-256 checksum How to use checksums |
e5b02b19f1c38cc1ff29af21c1851c1160cfb185d4516834a37b7227d7df01b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|