Pyeo is an advanced static analysis tool tailored specifically to enforce the principles advocated by Elegant Objects (elegantobjects.org) in Python projects. It serves as a quality control instrument to ensure that your Python code adheres to the core tenets of elegance, simplicity, and maintainability.
Project description
pyeo
Pyeo is an advanced static analysis tool tailored specifically to enforce the principles advocated by Elegant Objects (elegantobjects.org) in Python projects. It serves as a quality control instrument to ensure that your Python code adheres to the core tenets of elegance, simplicity, and maintainability.
Pyeo is proudly based on the robust foundation of Mypy, a leading static type checker for Python. Mypy not only provides excellent type analysis capabilities but also offers a convenient-to-use API and abstractions for working with Python AST (Abstract Syntax Tree). This unique combination empowers Pyeo a seamless static analysis experience, allowing for a deeper understanding of your code's structure and semantics.
The project is inspired by the team that made fun of me because of the lego build. STT lambda ❤️️
pip install eo-styleguide
setup.cfg/mypy.ini
[mypy]
plugins = pyeo.main
pyproject.toml
[tool.mypy]
plugins = ["pyeo.main"]
Simple example of usage:
from typing import Protocol, final
import attrs
# use the "elegant" decorator so that the plugin finds classes to check
from pyeo import elegant
class House(Protocol):
def area(self) -> int: ...
@elegant
@final
@attrs.define(frozen=True)
class HttpHouse(House):
def area(self) -> int:
return 10
Contents
- Principles
-
No ORM or ActiveRecord (why? and why?)
No null
Mypy helps prevent AttributeError
and other type-related errors by providing static type checking for Python code. It allows specifying variable types, function arguments, and return types to catch potential type issues before the program runs. By using Mypy, developers can identify and fix problems related to attribute access and other type mismatches, leading to improved code quality and easier maintenance.
Example:
class Employee(object):
def __init__(self, user_id: int):
self._user_id = user_id
def get_by_id(user_id: int) -> Employee:
if user_id < 0:
return None
return Employee(user_id)
Mypy return next violation:
error: Incompatible return value type (got "None", expected "Employee") [return-value]
So, we must use typing.Optional
or |
(union) operator.
It's works:
def get_by_id(user_id: int) -> Optional[Employee]: ...
def get_by_id(user_id: int) -> Employee | None: ...
No code in constructors
You can use @attrs.define
for skip this. It decorator create ctor for your classes automatically. However, we implement check that your primary and secondary ctors not contain code, with the exception of attributes assingment. Please check NoCodeInCtorFeature.
No getters and setters
Actually we realize functional for to prohibit the use of @property
and @setter
method decorators. You can use @attrs.define(frozen=True)
in order to make an object immutable.
Prohibit the use of @property
decorator not protect from evil of getters, so if you can ideas how we can implement more complex check, create issue please.
No mutable objects
attrs.define(frozen=True)
is a parameter used in the attrs library to create classes with attributes that cannot be modified after the instance is created (i.e., immutable or "frozen" classes).
The attrs library allows defining classes using the @attr.s
decorator or by explicitly calling the attr.define
function, and frozen=True
is one of the parameters for specifying attribute behavior in the class.
When you use attrs.define(frozen=True)
for a class, all its attributes become read-only after the instance is created, making the class "frozen" or "immutable," preventing any changes to its attribute values.
No er suffix
We check you class name not contain -er
or (C|c)lient
suffix by check in NoErNamesFeature
No static methods
Check by NoStaticmethodsFeature
No reflection
Prohibit next function calls:
isinstance
type
issubclass
hasattr
No public methods without a contract
In Python, typing.Protocol
is a class introduced in Python 3.8 as part of the typing module. It is used to define structural subtyping or "duck typing" for classes, which allows you to create interfaces without using explicit inheritance.
EachMethodHasProtocolFeature rule check that all of public class methods has protocol.
No statements in tests
TODO
No ORM
Detect using ORM or ActiveRecord tools on project by design/code review
No inheritance
Each @elegant
object must be typing.final
. Check by FinalClassFeature
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file eo_styleguide-0.0.1.dev1.tar.gz
.
File metadata
- Download URL: eo_styleguide-0.0.1.dev1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.5 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 122663fe82844ff9c39f4ef038181a7aad3e19d24c9e0bd43357b5f42da3dec3 |
|
MD5 | 32f9727ec0809df7263e9ed43bad1fba |
|
BLAKE2b-256 | 69f0a2d31c56d0a7af42c127db6255262008e4cb0a9562cd09e2d9d94e6f5386 |
File details
Details for the file eo_styleguide-0.0.1.dev1-py3-none-any.whl
.
File metadata
- Download URL: eo_styleguide-0.0.1.dev1-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.5 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdad10903a7384de12964a48ea52207e54157901bbf2c59be2dedac52cfd3a0a |
|
MD5 | 8dcd5e268a07dea0845149df617953b5 |
|
BLAKE2b-256 | e3eac752a7b6161cf065cd76ac2f996c8f24db562d38d5a1e556299f44733d8c |