Skip to content

Commit 9d55d58

Browse files
authored
style: run ruff lint and format on python files in repo (#1936)
* add ruff lint and format check to CI workflow. * convert pyproject.toml to ruff.toml * reduced number of ignored lint checks * reformat remaining python files * update CONTRIBUTING, DEVELOPER.md, and .vscode/README.md * add vscode formatting tasks * use flynt to convert .format() to f-strings
1 parent baf4300 commit 9d55d58

File tree

322 files changed

+2599
-20218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

322 files changed

+2599
-20218
lines changed

.doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
rtds_action_github_token = os.environ.get("GITHUB_TOKEN", None)
8989

9090
# set master doc for readthedoce
91-
master_doc = 'index'
91+
master_doc = "index"
9292

9393
# Add any paths that contain templates here, relative to this directory.
9494
templates_path = ["_templates"]

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Checklist of items for pull request
66
- [ ] Closed issue #xxxx
77
- [ ] Referenced issue or pull request #xxxx
88
- [ ] Added new test or modified an existing test
9-
- [ ] Ran `black` on new and modified autotests
9+
- [ ] Ran `ruff` on new and modified python scripts in .doc, autotests, doc, distribution, pymake, and utils subdirectories.
1010
- [ ] Formatted new and modified Fortran source files with `fprettify`
1111
- [ ] Added doxygen comments to new and modified procedures
1212
- [ ] Updated meson files, makefiles, and Visual Studio project files for new source files

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ jobs:
5858

5959
- name: Check MSVS project files
6060
run: pixi run check-vfproj
61+
62+
- name: Check python lint
63+
run: pixi run check-python-lint
64+
65+
- name: Check python format
66+
run: pixi run check-python-format
6167

6268
- name: Check CITATION.cff
6369
uses: dieghernan/cff-validator@v3

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ bin/**
7070
UserPrefs.xml
7171

7272
tmp_simulations/
73-
autotest/temp/
74-
autotest/tempbin/
75-
autotest/*.html
76-
autotest/notebooks/
7773

7874
distribution/temp/
7975
distribution/temp_zip/
@@ -135,6 +131,11 @@ modflow6.code-workspace
135131
# unittests
136132
unittests/
137133
autotest/.failed
134+
autotest/.temp/
135+
autotest/temp/
136+
autotest/tempbin/
137+
autotest/*.html
138+
autotest/notebooks/
138139

139140
**/__pycache__
140141
**/.benchmarks

.vscode/README.md

Lines changed: 122 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,129 @@
1-
# Compiling and Visual Debugging with VSCode
1+
# Using VSCode with MODFLOW 6
22

3-
VSCode is a free integrated development environment for Windows, Linux, and
4-
MacOS. There are two major advantages over Visual Studio:
3+
This document describes how to use VSCode to modify, format, build and test MODFLOW 6. VSCode is a free integrated development environment for Windows, Linux, and MacOS. Most of VSCode's source code is open-source and the releases are (proprietary) freeware.
54

6-
* Most of VSCode's source code is open-source and the releases are (proprietary) freeware.
7-
* VSCode runs on Linux.
5+
The folder containing this README markdown file (`.vscode/`) contains the configuration files for using VSCode for MODFLOW 6 development. At the moment they are used for formatting, building, and debugging MODFLOW 6. In order to build MODFLOW 6 follow the steps in [DEVELOPER.md](../DEVELOPER.md)
86

9-
This folder contains the configuration files for using VSCode for MODFLOW 6
10-
development.
11-
At the moment they are used for building and debugging MODFLOW 6.
12-
In order to build MODFLOW 6 follow the steps in [DEVELOPER.md](../DEVELOPER.md)
7+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
8+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
139

14-
## Visual Studio Code
10+
- [Setting up Visual Studio Code](#setting-up-visual-studio-code)
11+
- [Installation](#installation)
12+
- [Visual Studio Code Extensions](#visual-studio-code-extensions)
13+
- [Dependencies](#dependencies)
14+
- [Running VSCode](#running-vscode)
15+
- [Final VSCode setup](#final-vscode-setup)
16+
- [MODFLOW 6 VSCode Tasks](#modflow-6-vscode-tasks)
17+
- [Compiling](#compiling)
18+
- [Debugging](#debugging)
19+
- [Formatting](#formatting)
20+
- [Additional tasks](#additional-tasks)
21+
22+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
23+
24+
## Setting up Visual Studio Code
25+
26+
### Installation
1527

1628
Install VSCode from https://code.visualstudio.com/
1729

18-
### Extensions
30+
### Visual Studio Code Extensions
1931

2032
Install the following VSCode extensions:
2133

2234
- Modern Fortran:
2335
https://marketplace.visualstudio.com/items?itemName=krvajalm.linter-gfortran
2436

25-
Note: The Remote - WSL extension may be required if you want to use a windows VSCode
26-
installation in a WSL environment.
37+
Note: The [Remote - WSL extension](https://code.visualstudio.com/docs/remote/wsl) may be required if you want to use a windows VSCode installation in a WSL environment.
2738

2839
### Dependencies
2940

30-
Required and optional dependencies for MODFLOW 6 are discussed in [DEVELOPER.md](../DEVELOPER.md)
41+
Required and optional dependencies for MODFLOW 6 are discussed in [DEVELOPER.md](../DEVELOPER.md). Required dependencies should be installed prior to setting up VSCode for MODFLOW 6.
3142

32-
### Settings
33-
34-
Add [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) to the
35-
`modflow6/.vscode` directory if not already there. The following settings can be considered a
36-
starting place as the contents of this file are dictated both by desired VSCode behavior and
37-
environmental factors:
43+
### Running VSCode
3844

39-
In general, to determine the path of an installed tool in your environment run:
40-
- bash: `which <toolname>`, e.g. `which fortls`
41-
- cmd: `where <toolname>`, e.g. `where python`
42-
- PowerShell: `Get-Command <toolname>` e.g. `Get-Command fprettify`
45+
Open the top level `modflow6` repository directory on your system when starting VSCode. The program will then look for the `modflow6/.vscode` directory to discover settings relevant to your session.
4346

44-
1. Activate the conda environment:
47+
A nice alternative on any system is to start VSCode from the shell. For example, in a bash or git bash shell (windows), change to the `modflow6` directory and execute the command:
4548

4649
```bash
47-
conda activate modflow6
50+
code .
4851
```
4952

50-
2. Determine the path of `fortls` and `fprettify`
53+
Note the dot (".") at the end of the command. Starting in this way, VSCode will open as desired, inheriting and discovering expected runtime settings in the correct directory location.
5154

52-
3. Set the setting "fortran.fortls.path" and "fortran.formatting.path":
53-
```json
54-
{
55-
"fortran.fortls.path": "/path/to/fortls",
56-
"fortran.formatting.path": "/path/to/fprettify",
57-
}
58-
```
55+
### Final VSCode setup
5956

60-
The fortran formatter can be integrated with VSCode using the following settings:
57+
A few additional steps are required to finalize the VSCode setup for MODFLOW 6. The remaining steps are:
6158

62-
```json
63-
{
64-
"[fortran]": {
65-
"editor.formatOnSave": true,
66-
},
67-
"fortran.formatting.formatter": "fprettify",
68-
"fortran.formatting.fprettifyArgs": ["-c", "/path/to/modflow6/.fprettify.yaml"],
69-
}
70-
```
59+
1. [Select the python interpreter](https://code.visualstudio.com/docs/python/environments#_working-with-python-interpreters) for the MODFLOW 6 workspace. To select the python interpreter run:
7160

72-
Setting the formatter up in this way will cause a source file to reformat with each explicit save.
61+
* Press `Ctrl + Shift + P` in VSCode.
62+
* Type `Python: Select Interpreter`.
63+
* Select `Run Task` (press Enter).
64+
* Select the `modflow6` conda environment.
7365

74-
### Running VSCode
66+
2. Open a new terminal in VSCode and determine the path to the Fortran language server (`fortls`) and `fprettify` using the syntax for your terminal (`cmd`, `bash`, `powershell`).
7567

76-
Open the top level `modflow6` repository directory on your system when starting VSCode. The program will
77-
then look for the `modflow6/.vscode` directory to discover settings relevant to your session.
68+
3. Set the setting "fortran.fortls.path" and "fortran.formatting.path":
7869

79-
A nice alternative on any system is to start VSCode from the shell. For example, in a bash or git bash
80-
shell (windows), change to the `modflow6` directory and execute the command:
70+
```json
71+
{
72+
"fortran.fortls.path": "/path/to/fortls",
73+
"fortran.formatting.path": "/path/to/fprettify",
74+
}
75+
```
76+
77+
4. The fortran formatter can be integrated with VSCode using the following settings:
78+
79+
```json
80+
{
81+
"[fortran]": {
82+
"editor.formatOnSave": true,
83+
},
84+
"fortran.formatting.formatter": "fprettify",
85+
"fortran.formatting.fprettifyArgs": ["-c", "/path/to/modflow6/.fprettify.yaml"],
86+
}
87+
```
88+
89+
Setting the formatter up in this way will cause a source file to reformat with each explicit save.
90+
91+
In general, to determine the path to python, the fortran language server (`fortls`), and `fprettify` in your environment run:
92+
93+
- bash, zsh: `which <toolname>`, e.g. `which fortls`
94+
- cmd: `where <toolname>`, e.g. `where python`
95+
- PowerShell: `Get-Command <toolname>` e.g. `Get-Command fprettify`
8196

82-
```bash
83-
code .
84-
```
97+
## MODFLOW 6 VSCode Tasks
8598

86-
Note the dot. Starting in this way, VSCode will open as desired, inheriting and discovering
87-
expected runtime settings in the correct directory location.
99+
A number of MODFLOW 6 development tasks in the [`.vscode/settings.json`](./settings.json) file in the MODFLOW 6 repository. Debugging tasks are included in the `.vscode/launch.json` file, that you will create (instructions below). The tasks includes tasks to compile, debug, and format files. A number of miscellaneous tasks are also included to help prepare required files for Pull Requests (for example, makefiles).
88100

89101
### Compiling
90102

91103
In order to compile Fortran source run:
92104

93105
* Press `Ctrl + Shift + P` in VSCode.
94-
* Type `Tasks`.
95-
* Select `Run Task` (press Enter).
106+
* Type `Tasks: Run Build Task` (press Enter).
96107
* Select the suitable task for your situation.
97108

109+
Tasks are available to build MODFLOW 6 using gfortran and Intel Fortran (ifort). If you have used one of the Rebuild tasks, Build tasks for the same compiler can be used to just recompile modified source files and relink the object files. There are also pixi tasks for MODFLOW 6 build using gfortran.
110+
111+
Available compiling tasks include:
112+
113+
* `Rebuild mf6 (gfortran, release)`
114+
* `Rebuild mf6 (ifort, release)`
115+
* `Rebuild mf6 (gfortran, debug)`
116+
* `Rebuild mf6 (ifort, debug)`
117+
* `Build mf6 (gfortran, release)`
118+
* `Build mf6 (ifort, release)`
119+
* `Build mf6 (gfortran, debug)`
120+
* `Build mf6 (ifort, debug)`
121+
* `Pixi - Rebuild mf6 (gfortran, release)`
122+
* `Pixi - Rebuild mf6 (gfortran, debug)`
123+
* `Pixi - Build mf6 (gfortran, release)`
124+
* `Pixi - Build mf6 (gfortran, debug)`
125+
126+
98127
### Debugging
99128

100129
Add a `launch.json` in `.vscode` similar to this.
@@ -154,3 +183,39 @@ After building modflow, you can start debugging.
154183

155184
- Set a breakpoint somewhere in the source code.
156185
- Press `Ctrl + F5` to start debugging.
186+
187+
### Formatting
188+
189+
There are a number of VSCode tasks to determine if any of the Fortran source files, python code or scripts, LaTeX files, and markdown files in the MODFLOW 6 repository need to be reformatted.
190+
191+
In order format file run:
192+
193+
* Press `Ctrl + Shift + P` in VSCode.
194+
* Type `Tasks: Run Build Task` (press Enter).
195+
* Select the suitable task for your situation.
196+
197+
Currently available formatting tasks include:
198+
199+
* `Check spelling` - check spelling in Fortran source files, python code or scripts, LaTeX files, and markdown files
200+
* `Check Fortran format` - check the format of Fortran source files using `fprettify`
201+
* `Check python format` - check the python formatting using `ruff`
202+
* `Check python lint` - check python files for programmatic and stylistic errors using `ruff`
203+
204+
The Fortran format task is only needed if the fortran formatter has not been [integrated with VSCode](#settings).
205+
206+
### Additional tasks
207+
208+
A number of additional VSCode tasks are available to perform common tasks that are required before making a Pull Request (see the [Developer documents](../DEVELOPER.md)).
209+
210+
In order run additional tasks run:
211+
212+
* Press `Ctrl + Shift + P` in VSCode.
213+
* Type `Tasks: Run Build Task` (press Enter).
214+
* Select the suitable task for your situation.
215+
216+
Currently available additional tasks include:
217+
218+
* `Run update_flopy.py` - updates FloPy MODFLOW 6 classes in the modflow6 Conda environment using current MODFLOW 6 definition files.
219+
* `Run dfn2f90.py` - updates Fortran definitions using current MODFLOW 6 definition files.
220+
* `Run mf6ivar.py` - Regenerates LaTeX file for the input output guide current MODFLOW 6 definition files.
221+
* `Rebuild makefiles` - Rebuilds the GNU makefile for MODFLOW 6 (`mf6`), ZONEBUDGET 6 (`zbud6`), and the MODFLOW Converter (`mf5to6`).

.vscode/tasks.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,5 +563,62 @@
563563
"clear": true
564564
}
565565
},
566+
{
567+
"label": "Check spelling",
568+
"type": "shell",
569+
"command": "python .github/common/check_spelling.py",
570+
"options": {"cwd": "${workspaceFolder}"},
571+
"group": {
572+
"kind": "build",
573+
"isDefault": true
574+
},
575+
"presentation": {
576+
"clear": true
577+
},
578+
"problemMatcher": []
579+
},
580+
{
581+
"label": "Check Fortran format",
582+
"type": "shell",
583+
"command": "python .github/common/check_format.py",
584+
"options": {"cwd": "${workspaceFolder}"},
585+
"group": {
586+
"kind": "build",
587+
"isDefault": true
588+
},
589+
"presentation": {
590+
"clear": true
591+
},
592+
"problemMatcher": []
593+
},
594+
{
595+
"label": "Check python format",
596+
"type": "shell",
597+
"command": "ruff format --config .github/common/ruff.toml --check .",
598+
"options": {"cwd": "${workspaceFolder}"},
599+
"group": {
600+
"kind": "build",
601+
"isDefault": true
602+
},
603+
"presentation": {
604+
"clear": true
605+
},
606+
"problemMatcher": []
607+
},
608+
{
609+
"label": "Check python lint",
610+
"type": "shell",
611+
"command": "ruff check --config .github/common/ruff.toml .",
612+
"options": {"cwd": "${workspaceFolder}"},
613+
"group": {
614+
"kind": "build",
615+
"isDefault": true
616+
},
617+
"presentation": {
618+
"clear": true
619+
},
620+
"problemMatcher": []
621+
},
622+
566623
]
567624
}

CONTRIBUTING.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Contributions to MODFLOW 6 are welcome. We ask that contributors follow some gui
77

88
- [Conduct](#conduct)
99
- [Submissions](#submissions)
10-
- [Questions](#questions)
1110
- [Bugs](#bugs)
11+
- [Questions](#questions)
1212
- [Requests](#requests)
1313
- [Source code](#source-code)
1414
- [Format](#format)
@@ -66,17 +66,24 @@ To submit a pull request (PR):
6666

6767
1. To avoid duplicating effort, [search](https://github.com/MODFLOW-USGS/modflow6/pulls) for an open or closed PR that relates to your submission.
6868
2. Fork the MODFLOW-USGS/modflow6 repo and make your changes in a new branch, following our style and commit message guidelines and [including appropriate test cases](./DEVELOPER.md#writing-tests).
69-
3. [Rebuild makefiles](./DEVELOPER.md#generating-makefiles) and update MSVS project files if you added, removed, or renamed any source files.
70-
4. [Run the full test suite](./DEVELOPER.md#running-tests) and make sure all tests pass.
71-
5. Push your branch to GitHub and create a pull request to the `develop` branch.
72-
6. If we suggest changes:
69+
3. [Check the spelling and formatting](./DEVELOPER.md#formatting) of any modified or new Fortran source files, python files definition files, markdown, and LaTeX files.
70+
4. [Rebuild makefiles](./DEVELOPER.md#generating-makefiles) and update MSVS project files if you added, removed, or renamed any source files.
71+
5. [Run the full test suite](./DEVELOPER.md#running-tests) and make sure all tests pass.
72+
6. Push your branch to GitHub and create a pull request to the `develop` branch.
73+
7. If we suggest changes:
7374
a. make the required updates
7475
b. make sure tests still pass
7576
c. rebase your branch if needed
7677
d. (force) push to update your PR
7778

7879
That's it! Thank you for your contribution!
7980

81+
If you have installed the pixi environment you can complete steps 3 and 4 using:
82+
83+
```shell
84+
pixi run prepare-pull-request
85+
```
86+
8087
## Source code
8188

8289
### Format

0 commit comments

Comments
 (0)