Package: eo-styleguide
pyeoPyeo  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 ofMypy, 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 ❤️️pipinstalleo-styleguidesetup.cfg/mypy.ini[mypy]
plugins = pyeo.mainpyproject.toml[tool.mypy]
plugins = ["pyeo.main"]Simple example of usage:fromtypingimportProtocol,finalimportattrs# use the "elegant" decorator so that the plugin finds classes to checkfrompyeoimportelegantclassHouse(Protocol):defarea(self)->int:...@elegant@final@attrs.define(frozen=True)classHttpHouse(House):defarea(self)->int:return10ContentsPrinciplesNo null(why?)No code in constructors(why?)No getters and setters(why?)No mutable objects(why?)No readers, parsers, controllers, sorters, and so on(why?)No static methods, not even private ones(why?)No instanceof, type casting, or reflection(why?)No public methods without a contract (interface)(why?)No statements in test methods except assert(why?)No ORM or ActiveRecord(why?andwhy?)No implementation inheritance(why?andwhy?)No nullMypy helps preventAttributeErrorand 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:classEmployee(object):def__init__(self,user_id:int):self._user_id=user_iddefget_by_id(user_id:int)->Employee:ifuser_id<0:returnNonereturnEmployee(user_id)Mypy return next violation:error: Incompatible return value type (got "None", expected "Employee")  [return-value]So, we must usetyping.Optionalor|(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 constructorsYou can use@attrs.definefor 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 checkNoCodeInCtorFeature.No getters and settersActually we realize functional for to prohibit the use of@propertyand@settermethod decorators. You can use@attrs.define(frozen=True)in order to make an object immutable.Prohibit the use of@propertydecorator not protect from evil of getters, so if you can ideas how we can implement more complex check, create issue please.No mutable objectsattrs.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).
Theattrslibrary allows defining classes using the@attr.sdecorator or by explicitly calling theattr.definefunction, andfrozen=Trueis one of the parameters for specifying attribute behavior in the class.
When you useattrs.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 suffixWe check you class name not contain-eror(C|c)lientsuffix by check inNoErNamesFeatureNo static methodsCheck byNoStaticmethodsFeatureNo reflectionProhibit next function calls:isinstancetypeissubclasshasattrNo public methods without a contractIn Python,typing.Protocolis 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.EachMethodHasProtocolFeaturerule check that all of public class methods has protocol.No statements in testsTODONo ORMDetect using ORM or ActiveRecord tools on project by design/code reviewNo inheritanceEach@elegantobject must betyping.final. Check byFinalClassFeature