Skip to content

Commit a288a51

Browse files
authored
Add a cookbook full of wonderful recipes (#33)
1 parent de7e7fd commit a288a51

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

COOKBOOK.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# reLint Cookbook
2+
3+
A collection of recipes for reLint. Please feel free to contribute!
4+
5+
# Table of Contents
6+
7+
- [Python](#python)
8+
- [Django](#django)
9+
- [HTML](#html)
10+
11+
# Python
12+
13+
## Django
14+
15+
### Databases
16+
17+
```yaml
18+
- name: potential database connection limit with use of threading # by @syphar
19+
pattern: "(threading|ThreadPoolExecutor)[^#]*(?!\\s+# noqa)$"
20+
hint: |
21+
When using threads, keep in mind that they might use additional database
22+
connections. This can lead to a connection limit being reached. You may
23+
also need to manually close the database connection in the thread.
24+
filePattern: .*\.py
25+
```
26+
27+
### Utils
28+
29+
```yaml
30+
- name: Do not import datetime or date directly # by @codingjoe
31+
pattern: '(from datetime import|from django.utils.timezone import)'
32+
filePattern: .*\.py
33+
hint: |
34+
To differentiate between naive and timezone-aware dates,
35+
please use the following imports:
36+
* 'from django.utils import timezone'
37+
* 'import datetime'
38+
```
39+
40+
### Management Commands
41+
42+
```yaml
43+
- name: No logger in management commands # by @codingjoe
44+
pattern: (logger|import logging)
45+
hint: Please write to self.stdout or self.stderr in favor of using a logger.
46+
filePattern: \/management\/commands\/.*\.py
47+
```
48+
49+
### Testing
50+
51+
#### PyTest-Django
52+
53+
```yaml
54+
- name: Code and tests seem too complex # by @codingjoe
55+
pattern: '(pytest\.fixture|def (?!test_)).*(?!\\s+# noqa)$'
56+
hint: |
57+
Large test setups hint towards complex or convoluted production code.
58+
Consider breaking down your code into smaller individually testable chunks.
59+
You may also use pytest fixtures inside a conftest.py file. Their usage should
60+
be limited to technical setups, like stubs or IO mocks. Data fixtures should
61+
be implemented via baker recipes only.
62+
filePattern: '.*\/test_.*\.py'
63+
```
64+
65+
```yaml
66+
- name: IO is lava – Avoid using database fixtures # by @codingjoe
67+
pattern: '@pytest.fixture.*\n[ ]*def [^(]+\([^)]*(db|transactional_db)(, |\))'
68+
hint: Please use the "django_db" marker on individual tests only.
69+
filePattern: .*\.py
70+
```
71+
72+
```yaml
73+
- name: IO is lava – Avoid the 'db' fixture # by @codingjoe
74+
pattern: "def test_[^(]+\\([^)]*db[^)]*\\):"
75+
hint: Please use the "django_db" marker instead.
76+
filePattern: .*\.py
77+
```
78+
79+
```yaml
80+
- name: IO is lava – Do not mark a whole test class for database usage # by @codingjoe
81+
pattern: \@pytest\.mark\.django_db[^\n]*\n\w*class
82+
hint: Please use the "django_db" marker on individual tests only.
83+
filePattern: .*\.py
84+
```
85+
86+
#### Model Bakery
87+
88+
```yaml
89+
- name: Follow the recipe # by @codingjoe
90+
pattern: 'baker\.(make|prepare)\('
91+
filePattern: '.*\/test_.*\.py'
92+
hint: |
93+
Please use baker recipes instead of `baker.make` or `baker.prepare`.
94+
This allows us to easily create complex objects with a single line of code.
95+
```
96+
97+
# HTML
98+
99+
```yaml
100+
- name: no inline CSS # by @codingjoe
101+
pattern: 'style=\"[^\"]*;[^\"]+\"'
102+
hint: |
103+
Please do not use more than one inline style attribute.
104+
You may use a CSS class instead.
105+
filePattern: .*\.(html|vue|jsx|tsx)
106+
```

README.md

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
python3 -m pip install relint
1717
```
1818

19+
## [Examples & Recipes – The reLint Cookbook](COOKBOOK.md)
20+
1921
## Usage
2022

2123
You can write your own regular rules in a YAML file, like so:
@@ -90,27 +92,3 @@ following entry to your `.pre-commit-config.yaml`:
9092
- id: relint
9193
args: [-W] # optional, if you want to fail on warnings during commit
9294
```
93-
94-
## Samples
95-
96-
```yaml
97-
- name: db fixtures
98-
pattern: 'def test_[^(]+\([^)]*(customer|product)(, |\))'
99-
hint: Use model_bakery recipes instead of db fixtures.
100-
filePattern: test_.*\.py
101-
102-
- name: model_bakery recipes
103-
pattern: baker\.make\(
104-
hint: Please use baker.make_recipe instead of baker.make.
105-
filePattern: (test_.*|conftest)\.py
106-
107-
- name: the database is lava
108-
pattern: '@pytest.fixture.*\n[ ]*def [^(]+\([^)]*(db|transactional_db)(, |\))'
109-
hint: Please do not create db fixtures but model_bakery recipes instead.
110-
filePattern: .*\.py
111-
112-
- name: No logger in management commands
113-
pattern: (logger|import logging)
114-
hint: Please write to self.stdout or self.stderr in favor of using a logger.
115-
filePattern: \/management\/commands\/.*\.py
116-
```

0 commit comments

Comments
 (0)