@@ -229,6 +229,16 @@ exec git pull
229
229
230
230
## Pre-Commit
231
231
232
+ ::::::::::::::::::::::::::::::::::::: callout
233
+
234
+ ## Extra Setup
235
+
236
+ This section requires you to either install ` pre-commit ` at the system level or setup a Virtual Environment.
237
+
238
+ Instructions on doing so can be found at the bottom of the [ document] ( hooks.md#installing_pre-commit ) .
239
+
240
+ ::::::::::::::::::::::::::::::::::::::::::::::::
241
+
232
242
Pre-commit hooks that run before commits are made are _ really_ useful to the extent that they require special discussion
233
243
and will be the focus of the remainder of this episode. Why are they so useful? It's because they shorten the feedback
234
244
loop of changes that need to be made when checking and linting code.
@@ -254,7 +264,7 @@ hook that resides at `.git/hooks/pre-commit`, although we will look at that file
254
264
255
265
::::::::::::::::::::::::::::::::::::::::::::::::
256
266
257
- ### Why are Pre-Commit hooks so important?
267
+ ## Why are Pre-Commit hooks so important?
258
268
259
269
You may be wondering why running hooks prior to commits is so important. The short answer, as we've already hear, is
260
270
that it reduces the feedback loop and speeds up the pace of development. The long answer is that it only really becomes
@@ -267,8 +277,9 @@ base, making some changes and committing them.
267
277
other than Python. Many Linux systems have [ pre-commit] [ pc ] in their package management systems so if you are using
268
278
Linux or OSX you can install these at the system level.
269
279
270
- However, for this course the setup instructions asked you to install [ Miniconda] [ miniconda3 ] and we can install
271
- [ ` pre-commit ` ] [ pc ] in a Conda environment to leverage it. The steps to do so are
280
+ However, for this section of the course you should install [ Miniconda] [ miniconda3 ] so we can install
281
+ [ ` pre-commit ` ] [ pc ] in a Conda environment to leverage it. There are instructions at the bottom of this page on how to
282
+ install Miniconda. Once you have done so you can proceed with creating a conda environment. The steps to do so are
272
283
273
284
1 . Create a Conda environment called ` python-maths ` with ` conda create -n python-maths python=3.11 `
274
285
2 . Activate the newly created ` python-maths ` environment.
@@ -349,29 +360,6 @@ Proceed ([y]/n)?
349
360
pre-commit installed at .git/hooks/pre-commit
350
361
```
351
362
352
- ::::::::::::::::::::::::::::::::::::: callout
353
-
354
- ## Install Pre-commit globally
355
-
356
- Examples of installing [ pre-commit] [ pc ] at the system level for different Linux systems or OSX. Note you will
357
- need to have ` root ` access to install packages on your Linux system.
358
-
359
- ``` bash
360
- # Arch Linux
361
- ❱ pacman -Syu pre-commit
362
- # Gentoo
363
- ❱ emerge -av pre-commit
364
- # Debin/Ubuntu
365
- ❱ apt-get install pre-commit
366
- # OSX Homebrew
367
- ❱ brew install pre-commit
368
- ```
369
-
370
- The advantage of this is that you will be able to ` pre-commit install ` in any repository without first having to
371
- activate a virtual environment.
372
-
373
- ::::::::::::::::::::::::::::::::::::::::::::::::
374
-
375
363
::::::::::::::::::::::::::::::::::::: challenge
376
364
377
365
## Challenge 2 - Checking out the installed ` pre-commit ` hook
@@ -599,16 +587,18 @@ of the others that are defined.
599
587
600
588
## Solution 1 : What version of the ` numpydoc ` repo is configured
601
589
602
- Using [ grep] [ grep ] to search for the ` numpydoc ` string in the ` .pre-commit-config.yaml ` we can hone in on the ` repo ` and
603
- its associated ` rev ` .
590
+ Using [ grep] [ grep ] to search for the [ ` numpydoc ` ] [ numpydoc ] string in the ` .pre-commit-config.yaml ` we can hone in on
591
+ the ` repo ` and its associated ` rev ` .
604
592
605
593
``` bash
606
594
❱ grep -A1 numpydoc .pre-commit-config.yaml | grep -B1 rev
607
595
- repo: https://github.com/numpy/numpydoc
608
596
rev: v1.6.0
609
597
```
610
598
611
- We see that it is ` v1.6.0 ` that is currently configured for ` numpydoc ` .
599
+ We see that it is ` v1.6.0 ` that is currently configured for [ ` numpydoc ` ] [ numpydoc ] .
600
+
601
+ ** NB** This hook ensures the docstrings of Python functions comply with thet [ numpydocstyle] [ numpydocstyle ] guide.
612
602
613
603
:::::::::::::::::::::::::::::::::
614
604
@@ -784,64 +774,12 @@ The file should then be staged, committed and pushed.
784
774
## Adding repos
785
775
786
776
The definitive [list][pc-hooks] of ` pre-commit` repos is maintained on the official website. Each entry links to the
787
- GitHub repository and most contain in their `README.md` instructions on how to use the hooks.
788
-
789
- <!-- :::::::::::::::::::::::::::::::::::: : challenge -->
790
-
791
- <!-- # # Challenge 7: Add the `numpydoc` repo, exclude the `tests/` and `doc/` directories and run it against-->
792
- <!-- the code base -->
793
-
794
- <!-- The [numpydoc](https://github.com/numpy/numpydoc) repo defines hooks that check the Python docstrings -->
795
- <!-- conform to the [Numpydoc style guide](https://numpydoc.readthedocs.io/en/latest/format.html). Following the -->
796
- <!-- instructions add the repo to the `.pre-commit-config.yaml` (on a new branch) -->
797
-
798
- <!-- ::::::::::::::::::::::: : solution -->
799
-
800
- <!-- # # Solution -->
801
-
802
- <!-- Create a branch to undertake the work on. -->
803
-
804
- <!-- ``` bash -->
805
- <!-- ❱ git switch main -->
806
- <!-- ❱ git pull -->
807
- <!-- ❱ git switch -c ns-rse/pre-commit-numpydoc -->
808
- <!-- ``` -->
809
-
810
- <!-- The following should be added to your `.pre-commit-config.yaml` -->
811
-
812
- <!-- ``` yaml -->
813
- <!-- - repo : https://github.com/numpy/numpydoc -->
814
- <!-- rev : v1.6.0 -->
815
- <!-- hooks : -->
816
- <!-- - id : numpydoc-validation -->
817
- <!-- exclude : | -->
818
- <!-- (?x)( -->
819
- <!-- tests/| -->
820
- <!-- docs/ -->
821
- <!-- ) -->
822
- <!-- ``` -->
823
-
824
- <!-- Check that the code base passes the checks, correct any errors that are highlighted. -->
825
-
826
- <!-- ``` bash -->
827
- <!-- ❱ pre-commit run numpydoc --all-files -->
828
- <!-- ``` -->
829
-
830
- <!-- The file should then be staged, committed and pushed. -->
831
-
832
- <!-- ``` bash -->
833
- <!-- ❱ git add .pre-commit-config.yaml -->
834
- <!-- ❱ git commit -m "pre-commit : adds the numpydoc repo/hook" -->
835
- <!-- ❱ git push -->
836
- <!-- ``` -->
837
-
838
- <!-- :::::::::::::::::::::::::::::::: : -->
839
-
840
- <!-- ::::::::::::::::::::::::::::::::::::::::::::::: : -->
777
+ GitHub repository and most contain in their `README.md` instructions on how to use the hooks. Which you will want to use
778
+ will depend very much on your project.
841
779
842
780
# # Local repos
843
781
844
- Local repo are those that do not use hooks defined by others and are instead defined by the user. This comes in handy
782
+ Local repos are those that do not use hooks defined by others and are instead defined by the user. This comes in handy
845
783
when you want to run checks which have dependencies that are specific to the code such as running [pylint][pylint] which
846
784
needs to import all the dependencies that are used or run a test suite.
847
785
@@ -978,12 +916,76 @@ the Pre-commit hooks as part of the Continuous Integration on GitHub which is th
978
916
979
917
::::::::::::::::::::::::::::::::::::::::::::::: :
980
918
919
+ # # Installing Pre-commit
920
+
921
+ # ## Install Pre-commit globally
922
+
923
+ Examples of installing [pre-commit][pc] at the system level for different Linux systems or OSX. Note you will
924
+ need to have `root` access to install packages on your Linux system.
925
+
926
+ ` ` ` bash
927
+ # Arch Linux
928
+ pacman -Syu pre-commit
929
+ # Gentoo
930
+ emerge -av pre-commit
931
+ # Debin/Ubuntu
932
+ apt-get install pre-commit
933
+ # OSX Homebrew
934
+ brew install pre-commit
935
+ ` ` `
936
+
937
+ The advantage of this is that you will be able to `pre-commit install` in any repository without first having to
938
+ activate a virtual environment.
939
+
940
+ # ## Virtual Environments
941
+
942
+ The other option is to install `pre-commit` within a Python Virtual Environment. If you are already familiar with using
943
+ these then you can simply `pip install pre-commit` and you are good to go, although note that `pre-commit` will need
944
+ installing in _every_ new environment you create that you want to use it.
945
+
946
+ If you are not familiar with Python Virtual Environments you can follow the instructions below to install and setup
947
+ [miniconda3][miniconda3] or [miniforge3][miniforge3].
948
+
949
+ :::::::::::::::::::::::::::::::::::: : callout
950
+
951
+ # # Anaconda Licensing
952
+
953
+ It is important to fully understand and adhere to the [Anaconda Licensing][anacondalicense] which permits
954
+ the use of their software (including Miniconda) in educational and research environments _only_ if there is no
955
+ commercial benefit. If the work you undertake involves commercial collaboration you should seek alternative solutions
956
+ for virtual environments (e.g. [miniforge3][miniforge3] or [virtualenvwrapper][virtualenvwrapper]).
957
+
958
+ ::::::::::::::::::::::::::::::::::::::::::::::: :
959
+
960
+ # ## Installing Miniconda/Miniforge3
961
+
962
+ Please follow the instructions at [Installing Miniconda](https://docs.anaconda.com/free/miniconda/miniconda-install/)
963
+ for your Operating System.
964
+
965
+ Should you chose to use `miniforge3` the downloads and installation instructions for different operating systems can be
966
+ found [here][miniforge3-install].
967
+
968
+ # ## Creating A Virtual Environment
969
+
970
+ You will have to create a virtual environment to undertake the course. If you have installed Miniconda as described
971
+ above you open a terminal (Windows use the Git Bash Shell) and create a Virtual Environment called `git-collaboation`.
972
+
973
+ ` ` ` bash
974
+ conda create --name git-collab python=3.11
975
+ conda activate git-collab
976
+ ` ` `
977
+
978
+ [anacondalicense] : https://www.anaconda.com/blog/update-on-anacondas-terms-of-service-for-academia-and-research
981
979
[bash] : https://www.gnu.org/software/bash/
982
980
[black] : https://black.readthedocs.io/en/stable/index.html
983
981
[gh] : https://github.com
984
982
[gl] : https://gitlab.com
985
983
[grep] : https://en.wikipedia.org/wiki/Grep
986
984
[miniconda3] : https://docs.anaconda.com/free/miniconda/
985
+ [miniforge3] : https://conda-forge.org/
986
+ [miniforge3-install] : https://github.com/conda-forge/miniforge
987
+ [numpydoc] : https://github.com/numpy/numpydoc
988
+ [numpydocstyle] : https://numpydoc.readthedocs.io/en/latest/format.html
987
989
[pc] : https://pre-commit.com
988
990
[pc-ci] : https://pre-commit.ci
989
991
[pc-hooks] : https://pre-commit.com/hooks
@@ -992,4 +994,5 @@ the Pre-commit hooks as part of the Continuous Integration on GitHub which is th
992
994
[python] : https://python.org
993
995
[pm] : https://github.com/ns-rse/python-maths
994
996
[ruff] : https://astral.sh/ruff
997
+ [virtualenvwrapper] : https://rse.shef.ac.uk/blog/2024-08-13-python-virtualenvwrapper/
995
998
[yaml] : https://yaml.org
0 commit comments