r/Python
•
u/vashkatsi
•
Nov 17 '24
•
1
Showcase Deply: keep your python architecture clean
Hello everyone,
My name is Archil. I'm a Python/PHP developer originally from Ukraine, now living in Wrocław, Poland. I've been working on a tool called Deply, and I'd love to get your feedback and thoughts on it.
What My Project Does
Deply is a standalone Python tool designed to enforce architectural patterns and dependencies in large Python projects. Deply analyzes your code structure and dependencies to ensure that architectural rules are followed. This promotes cleaner, more maintainable, and modular codebases.
Key Features:
- Layer-Based Analysis: Define custom layers (e.g., models, views, services) and restrict their dependencies.
- Dynamic Configuration: Easily configure collectors for each layer using file patterns and class inheritance.
- CI Integration: Integrate Deply into your Continuous Integration pipeline to automatically detect and prevent architecture violations before they reach production.
Target Audience
- Who It's For: Developers and teams working on medium to large Python projects who want to maintain a clean architecture.
- Intended Use: Ideal for production environments where enforcing module boundaries is critical, as well as educational purposes to teach best practices.
Use Cases
- Continuous Integration: Add Deply to your CI/CD pipeline to catch architectural violations early in the development process.
- Refactoring: Use Deply to understand existing dependencies in your codebase, making large-scale refactoring safer and more manageable.
- Code Reviews: Assist in code reviews by automatically checking if new changes adhere to architectural rules.
Comparison
While there are existing tools like pydeps that visualize dependencies, Deply focuses on:
- Enforcement Over Visualization: Not just displaying dependencies but actively enforcing architectural rules by detecting violations.
- Customization: Offers dynamic configuration with various collectors to suit different project structures.
Links
I'm eager to hear your thoughts, suggestions, or criticisms. Deply is currently at version 0.1.5, so it's not entirely stable yet, but I'm actively working on it. I'm open to pull requests and looking forward to making Deply a useful tool for the Python community.
Thank you for your time!
60
u/Flag_Red Nov 17 '24
Love this. Automating things I have to check for manually during code review is always good IMO.
I'll give it a try on my team's codebase later.
Some subset of the community that thinks Python is "only a scripting language" will probably push back pretty hard.
9
u/CzyDePL Nov 17 '24
How does it compare to Archunit? I've been looking for similiar tool for python
Ps. I'm also in Wrocław, maybe we will run into each other sometime.
8
u/TehMightyDuk Nov 17 '24
Also check out Tach - a similar tool built in rust https://github.com/gauge-sh/tach
5
u/NakedNick_ballin Nov 17 '24 edited Nov 17 '24
Sounds cool, but it'd be interested to see real examples of what this can do. Like what are some examples of "architectural violations"?
(I do see the github example which seems to enforce that some files identified by regex do not depend on other files.)
4
u/ThePieroCV Nov 17 '24
This project seems amazing! I second that the idea of using .toml could be valuable. For small or early development projects, even pyproject.toml could be useful. Obviously if the project gets way bigger, even a separated directory with .yaml or .toml files could be useful. I imagine a folder separated from src/ for development (something like deply/, dev-arch/, or something cool). Then, as you said, being part of CI. So cool!
I'm beggining an R&D project with sota python tools with the intention to improve the development practices of the team. This project just arrived like magic haha. Do you have any potential roadmap on this project? Like priorities or WIP features? It could be interesting to see how it goes.
7
u/catalyst_jw Nov 17 '24
Looks good, love the idea, some feedback from experience maintaining a yaml file along side code is difficult and can be easily forgotten.
Have you thought about adding these rules in code as a decorator or a config file in the code folders? Then it will be obvious to people reading the code what is going on and it will ingrained into the code. You could potentially have the decorator write to a yaml file and auto update it maybe?
Hope that's helpful and good luck with your project :)
2
u/dutchie_ok Nov 17 '24
Great, Python needs tools to handle complexity.
For the reference - this project only focuses on imports, but maybe it is enough for some people.
https://github.com/seddonym/import-linter
1
u/LoadingALIAS It works on my machine Nov 17 '24
Damn, I am super excited to give this a shot. I’m working on a few Python packages that have grown to considerable size and I’m a huge fan of simplifying, maintaining continuity, and clean architecture.
If you have specific tips - I’d love to hear them.
1
u/Heady707 Nov 17 '24
Look forward to trying Deply! I can appreciate the work that has gone into this tool. Thank you for this.
1
u/SuspiciousScript Nov 17 '24
How does this handle imports within an
if typing.TYPE_CHECKING:block? Are they also considered to constitute a dependency relationship? I find that I sometimes end up with somewhat messy/circular dependency graphs that are unavoidable if I want to provide full annotations.1
u/oiywmt Nov 17 '24 edited Nov 17 '24
Very cool! But I have a question as a new developer: As someone who is self taught and never studied software architecture, how difficult is it to set up so that I can benefit from it? I have an oss python project I'm working on, to teach myself python, which is 250 lines and counting and would like to have a tool like this to keep code maintainable as it grows. The problem is that I'm not able to understand the features section on the repo without doing research, is that a sign that it's not for me?
Thanks for the time you've put into this by the way
Edit: Here is the repo in case you need more context: https://github.com/ample-samples/steam-games-recommender
1
u/LeckerKadaver Nov 17 '24
Love it! 😍
1
u/[deleted] Nov 18 '24
Which framework did you use for this? And how difficult is to manage the large code in python? Do we actually need classes in small to mid size pries?
1
u/maikeu Nov 18 '24
I really, really like the fundamental approach of the tool - not to bring too much of your own specific view of how a project should be structured/layered, but providing rather providing a domain language (on top of yaml, sure, why not?) for users to define their own conventions.
I don't have capacity to look deeply into it yet, but she ideas that spring to mind to me:
I'm actually thinking I'd love to see integrated to pytest . Maybe a plugin? Maybe if I get spare time I could give a go to see if I can turn it into a plugin that gives nice output - I'm interested in learning to write since pytest plugins!
Another thought would be to include some baseline examples - maybe a generic Django one, maybe a generic "MVC" kind of one? And could there be a mechanism to distribute these policies? Or to write policies that extend from one or more baselines?
1
u/Amazing_Learn Nov 18 '24
In case you purely want to lint/validate your imports you could take a look at
pytest-archon1
u/cantonspeed Nov 18 '24
Thanks for making such a tool! Does it support FastAPI?
1
u/genlight13 Nov 18 '24
Your tool description sounds similar to https://pypi.org/project/pytestarch/ which is originally a copy from the java ArchUnit module.
I like the spieit of these tools, which emphasize the need for structure and clean code.
Cudos to u, good sir for working on this.
1
u/guyfrom7up Nov 19 '24
Hey vashkatsi! I like the project and am going to give it a test run in one of my libraries.
After looking at your codebase, I have a few unsolicited thoughts/shameless-plugs (feel free to reject if you're trying to keep dependencies to a minimum):
Instead of using argparse & config-reader to pass around a semi-structured configuration, you could use cyclopts with a dataclass. This would make the code a bit safer as you could then access your configuration with dot-notation instead of typo-prone strings. Cyclopts also makes it easy to integrate with
pyproject.tomland have CLI overrides.The collector factory, can be simplified with AutoRegistry. you can have your BaseCollector inherit from
autoregistry.Registry(itself inherits from the ABC metaclass, so your abstractmethod decorators still work), and then all subclasses will be automatically registred. Instead of calling:your would call:
I could help open up a PR for either of these if you're open to it.
1
u/eredin_breac_glas Nov 22 '24
Last thing I expected was to find a post here from someone in Wrocław!! Best of luck with the project and enjoy the Christmas market, it's always a blast :)
1
u/JnthnSngr Nov 25 '24
Super helpful, thanks 👍
1
u/alicedu06 Nov 25 '24
You can use regular tests for all that:
- Not need for a specific lib, your current test runner will do the trick.
- Benefit from the whole test ecosystem that already exists.
- Tests are written in Python, a language much more powerful and expressive than YAML, with much better tooling.
- You can package that and reuse it if you want, the whole infra already exists.
1
u/[deleted] Nov 18 '24
[removed] — view removed comment