Skip to content

Commit e23ea76

Browse files
committed
Enhancements and tips for mypy
1 parent 31c920c commit e23ea76

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

python/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ In Python, single-quoted strings and double-quoted strings are the same. This PE
378378

379379
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in [PEP 257](https://www.python.org/dev/peps/pep-0257).
380380

381-
Here at Rootstrap we prefer to use single-quoted strings over double quotes.
382-
For docstrings we use double quotes since the chance of writing a ' is higher in the documentation string of a class or a method
383-
384381
## Whitespace in Expressions and Statements
385382

386383
### Pet Peeves

python/cookiecutter-django.md

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,6 @@ exclude = '.*\/(migrations|settings)\/.*'
225225
- This command list all the issues found.
226226
- A useful command could be `$ black .` which resolves all the issues. Also, you can run `$ black . --diff` to watch the proposed changes.
227227

228-
### If you want to have this in a new Github repository
229-
If you want to have this project in a new Github repository under the rootstrap organization, then follow these steps:
230-
1. Go to Github.
231-
2. Click on create new repository.
232-
3. Under Owner, select rootstrap.
233-
4. Write the name for the respository equal to the name of the root folder of the project created with DjangoCookieCutter.
234-
5. Copy the `git clone with ssh value` from the project.
235-
6. Go to the root folder project in your terminal.
236-
7. Run `$ git init`
237-
8. Run `$ git remote add origin <git clone with ssh value>`
238-
9. If your current branch hasn't the `main` branch, then run `$ git checkout -b main`
239-
10. Run `$ git add .`
240-
11. Run `$ git commit -m "First commit"`. You can change the `"First commit"` message with whatever you think is correct.
241-
12. Run `$ git push origin main`
242-
13. If you had to run `$ git checkout -b main` then now run `$ git branch -M main`
243-
244228
### [pre-commit](https://pre-commit.com/)
245229
*NOTE: By default this package is already installed.*
246230

@@ -275,32 +259,81 @@ If you add Mypy to pre-commit it will check the typing in the files you change o
275259

276260
...
277261
```
278-
You may need more configuration in `additional_dependencies` depending on your project libraries because these pre-commit stages are running in an independent environment.
279-
280262
</details>
281-
282263
If you want to check the Github Wokflow configuration please take a look at this [section](#github-workflow)
283264

284-
A useful command could be `$ mypy .`, which shows you all the typing issues you have in all your Python files.
265+
A useful command could be `$ mypy .`, which shows you all the typing issues you have in all your Python files.
285266

286-
### Single-quotes
287-
> *Here at Rootstrap we prefer to use single-quoted strings over double quotes. For docstrings we use double quotes since the chance of writing a ' is higher in the documentation string of a class or a method.*
288-
>
289-
> [Rootstrap Guides/Python/String Quotes](https://github.com/rootstrap/tech-guides/tree/master/python#string-quotes)
267+
### exclude in mypy
268+
You may need to exclude files in mypy (for example configurations and migrations files). You can get this done
269+
adding a regexp list called `exclude` in the `setup.cfg` file. This is an example:
290270

291-
To convert the existing double quotes to single ones, follow these steps:
292-
1. In your IDE, search by the regex `/(?<!"")(?<=(?!""").)"(?!"")/`
293-
2. Replace the occurrences with the single quote `'`
294-
3. Include only the python files: `*.py`
295-
4. Exclude migrations files and manage.py: `*migrations*, manage.py`
296-
5. Check that everything is well replaced.
271+
```yaml
272+
[mypy]
273+
python_version = 3.10
274+
check_untyped_defs = True
275+
ignore_missing_imports = True
276+
warn_unused_ignores = True
277+
warn_redundant_casts = True
278+
warn_unused_configs = True
279+
plugins = mypy_django_plugin.main
280+
exclude = (?x)(
281+
urls\.py$ | merge_production_dotenvs_in_dotenv.py | api_router.py
282+
)
283+
284+
[mypy.plugins.django-stubs]
285+
django_settings_module = config.settings.test
286+
287+
[mypy-*.migrations.*]
288+
# Django migrations should not produce any errors:
289+
ignore_errors = True
290+
```
297291

298-
The VS Code configuration:
299-
- **Search**: `(?<!"")(?<=(?!""").)"(?!"")`
300-
- **Replace**: `'`
301-
- **files to include**: `*.py`
302-
- **files to exclude**: `*migrations*, manage.py`
303292

293+
#### pre-commit and mypy
294+
Sometimes mypy can generate issues with pre-commit, because it can't recognize some used libraries in the project.
295+
The error shows that doesn't find some dependencies. If that is the case, add those libraries in the `.pre-commit-config.yaml` file.
296+
This is an example configuration:
297+
298+
```yaml
299+
- repo: https://github.com/pre-commit/mirrors-mypy
300+
rev: v0.942
301+
hooks:
302+
- id: mypy
303+
additional_dependencies:
304+
- boto3
305+
- djangorestframework-simplejwt
306+
- drf-spectacular
307+
- django
308+
- psycopg2-binary
309+
- django-storages
310+
- dj-rest-auth
311+
- django-s3direct
312+
- django-cors-headers
313+
- djangorestframework
314+
- django-allauth
315+
- django-crispy-forms
316+
- django-environ
317+
- django-stubs
318+
- django-drip-campaigns
319+
- crispy-bootstrap5
320+
```
321+
322+
## If you want to have this in a new Github repository
323+
If you want to have this project in a new Github repository under the rootstrap organization, then follow these steps:
324+
1. Go to Github.
325+
2. Click on create new repository.
326+
3. Under Owner, select rootstrap.
327+
4. Write the name for the respository equal to the name of the root folder of the project created with DjangoCookieCutter.
328+
5. Copy the `git clone with ssh value` from the project.
329+
6. Go to the root folder project in your terminal.
330+
7. Run `$ git init`
331+
8. Run `$ git remote add origin <git clone with ssh value>`
332+
9. If your current branch hasn't the `main` branch, then run `$ git checkout -b main`
333+
10. Run `$ git add .`
334+
11. Run `$ git commit -m "First commit"`. You can change the `"First commit"` message with whatever you think is correct.
335+
12. Run `$ git push origin main`
336+
13. If you had to run `$ git checkout -b main` then now run `$ git branch -M main`
304337

305338
## CI
306339

0 commit comments

Comments
 (0)