Skip to content

Commit d4f98fa

Browse files
committed
Add ruff and mypy to pre-commit. Configure ruff as linter and formatter.
Co-authored-by: Virginia Morales <vmorales@arfima.com> Co-authored-by: Arfima Dev <dev@arfima.com> Signed-off-by: Xavier Barrachina Civera <xbarrachina@arfima.com>
1 parent 2b74b7a commit d4f98fa

Some content is hidden

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

61 files changed

+3055
-886
lines changed

.pre-commit-config.yaml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
44
rev: v4.6.0
55
hooks:
6-
- id: trailing-whitespace
76
- id: check-merge-conflict
87
- id: end-of-file-fixer
98
- id: name-tests-test
@@ -15,28 +14,32 @@ repos:
1514
- id: check-yaml
1615
- id: check-symlinks
1716
- id: detect-private-key
18-
- id: check-ast
19-
- id: debug-statements
2017

2118
- repo: https://github.com/Lucas-C/pre-commit-hooks
2219
rev: v1.5.5
2320
hooks:
2421
- id: remove-tabs
2522

26-
- repo: https://github.com/psf/black
27-
rev: '24.4.2'
23+
- repo: https://github.com/astral-sh/ruff-pre-commit
24+
rev: v0.5.6
2825
hooks:
29-
- id: black
30-
- id: black-jupyter
26+
- id: ruff
27+
args: [--fix, --exit-non-zero-on-fix, --config=pyproject.toml]
28+
- id: ruff-format
3129

32-
- repo: https://github.com/s-weigand/flake8-nb
33-
rev: v0.5.3
30+
- repo: local
3431
hooks:
35-
- id: flake8-nb
36-
additional_dependencies:
37-
- pep8-naming
38-
# Ignore all format-related checks as Black takes care of those.
39-
args:
40-
- --ignore=E2, W5, F401, E401, E704
41-
- --select=E, W, F, N
42-
- --max-line-length=120
32+
- id: mypy-cache
33+
name: "create mypy cache"
34+
language: system
35+
pass_filenames: false
36+
entry: bash -c 'if [ ! -d .mypy_cache ];
37+
then /bin/mkdir .mypy_cache; fi; exit 0'
38+
39+
- repo: https://github.com/pre-commit/mirrors-mypy
40+
rev: "v1.10.1"
41+
hooks:
42+
- id: mypy
43+
verbose: true
44+
args: ["--show-error-codes", "--install-types", "--non-interactive"]
45+
additional_dependencies: ["pytest", "types-requests"]

docs/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@
6060
autosummary_generate = True
6161

6262
# Docstrings of private methods
63-
autodoc_default_options = {"members": True, "undoc-members": True, "private-members": False}
63+
autodoc_default_options = {
64+
"members": True,
65+
"undoc-members": True,
66+
"private-members": False,
67+
}
6468

6569
# -- Options for HTML output -------------------------------------------------
6670
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

pyproject.toml

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,11 @@ test = [
7878
"pytest-cov",
7979
"sphinx-pyproject"
8080
]
81-
lint = [
82-
"isort",
83-
"black",
84-
"pyproject-flake8",
85-
"flake8",
86-
"flake8-nb",
87-
"mypy",
88-
"pre-commit",
89-
"tox"
90-
]
91-
"black[jupyter]" = []
92-
pandas = []
9381
dev = [
94-
"pandas>=2.0.3",
95-
"geopandas>=0.13.2"
82+
"mypy",
83+
"pre-commit",
84+
"ruff",
85+
"tox",
9686
]
9787

9888
[tool.pdm.scripts]
@@ -113,40 +103,83 @@ addopts = "-v"
113103
warn_unreachable = true
114104
ignore_missing_imports = true
115105

116-
[tool.isort]
117-
profile = "black"
118-
multi_line_output = 3
119-
include_trailing_comma = true
120-
force_grid_wrap = 0
121-
use_parentheses = true
122-
ensure_newline_before_comments = true
123-
line_length = 120
124-
125106
[tool.coverage.run]
126107
source = "src"
127108
omit = "tests/*"
128109
relative_files = true
129110

130-
[tool.yapf]
131-
blank_line_before_nested_class_or_def = true
132-
column_limit = 88
133-
134-
[tool.black]
135-
line-length = 120
136-
exclude = '''
137-
/(
138-
\.git
139-
| \.tox
140-
| \venv
141-
| \.venv
142-
| \*.env
143-
| \build
144-
| \dist
145-
)/
146-
'''
147-
148-
[tool.flake8]
149-
max-line-length = "120"
150-
extend-ignore = [
151-
"E501",
111+
[tool.ruff]
112+
# Exclude a variety of commonly ignored directories.
113+
exclude = [
114+
".bzr",
115+
".direnv",
116+
".eggs",
117+
".git",
118+
".git-rewrite",
119+
".hg",
120+
".ipynb_checkpoints",
121+
".mypy_cache",
122+
".nox",
123+
".pants.d",
124+
".pyenv",
125+
".pytest_cache",
126+
".pytype",
127+
".ruff_cache",
128+
".svn",
129+
".tox",
130+
".venv",
131+
".vscode",
132+
"__pypackages__",
133+
"_build",
134+
"buck-out",
135+
"build",
136+
"dist",
137+
"node_modules",
138+
"site-packages",
139+
"venv",
140+
]
141+
142+
[tool.ruff.lint]
143+
extend-fixable = [
144+
# Instead of trailing-whitespace
145+
"W291", "W293"
146+
]
147+
148+
extend-select = [
149+
# Instead of pydocstyle
150+
"D",
151+
#Instead of flake8
152+
"E", "F","B",
153+
# Instead of pep8-naming
154+
"N",
155+
# Instead of flake8-debugger or debug-statements
156+
"T10",
152157
]
158+
159+
ignore = [
160+
"E203",
161+
"E501",
162+
# Avoid incompatible rules
163+
"D203",
164+
"D213",
165+
166+
# Ignore this rules so that precommit passes. Uncomment to start fixing them
167+
"B006", "B008", "B904", "B012", "B024",
168+
"D",
169+
"F401",
170+
]
171+
172+
[tool.ruff.lint.extend-per-file-ignores]
173+
# Ignore `D` rules everywhere except for the `src/` directory.
174+
"!src/**.py" = ["D"]
175+
176+
[tool.ruff.lint.pycodestyle]
177+
max-line-length = 120
178+
179+
[tool.ruff.format]
180+
quote-style = "double"
181+
indent-style = "space"
182+
skip-magic-trailing-comma = false
183+
line-ending = "auto"
184+
#docstring-code-format = false
185+
docstring-code-line-length = "dynamic"

src/physrisk/api/v1/common.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ class Asset(BaseModel, extra="allow"):
3535
)
3636
latitude: float = Field(description="Latitude in degrees")
3737
longitude: float = Field(description="Longitude in degrees")
38-
type: Optional[str] = Field(None, description="Type of the asset <level_1>/<level_2>/<level_3>")
38+
type: Optional[str] = Field(
39+
None, description="Type of the asset <level_1>/<level_2>/<level_3>"
40+
)
3941
location: Optional[str] = Field(
40-
None, description="Location (e.g. Africa, Asia, Europe, Global, Oceania, North America, South America)"
42+
None,
43+
description="Location (e.g. Africa, Asia, Europe, Global, Oceania, North America, South America)",
4144
)
4245
capacity: Optional[float] = Field(None, description="Power generation capacity")
4346
attributes: Optional[Dict[str, str]] = Field(
44-
None, description="Bespoke attributes (e.g. number of storeys, structure type, occupancy type)"
47+
None,
48+
description="Bespoke attributes (e.g. number of storeys, structure type, occupancy type)",
4549
)
4650

4751

@@ -81,7 +85,8 @@ class IntensityCurve(BaseModel):
8185

8286
intensities: List[float] = Field([], description="Hazard indicator intensities.")
8387
return_periods: Optional[List[float]] = Field(
84-
[], description="[Deprecated] Return period in years in the case of an acute hazard."
88+
[],
89+
description="[Deprecated] Return period in years in the case of an acute hazard.",
8590
)
8691
index_values: Optional[Union[List[float], List[str]]] = Field(
8792
[],
@@ -100,7 +105,9 @@ class ExceedanceCurve(BaseModel):
100105
"""General exceedance curve (e.g. hazazrd, impact)."""
101106

102107
values: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
103-
exceed_probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
108+
exceed_probabilities: np.ndarray = Field(
109+
default_factory=lambda: np.zeros(10), description=""
110+
)
104111

105112
class Config:
106113
arbitrary_types_allowed = True
@@ -110,7 +117,9 @@ class Distribution(BaseModel):
110117
"""General exceedance curve (e.g. hazazrd, impact)."""
111118

112119
bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(11), description="")
113-
probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
120+
probabilities: np.ndarray = Field(
121+
default_factory=lambda: np.zeros(10), description=""
122+
)
114123

115124
class Config:
116125
arbitrary_types_allowed = True
@@ -119,8 +128,12 @@ class Config:
119128
class HazardEventDistrib(BaseModel):
120129
"""Intensity curve of an acute hazard."""
121130

122-
intensity_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
123-
probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
131+
intensity_bin_edges: np.ndarray = Field(
132+
default_factory=lambda: np.zeros(10), description=""
133+
)
134+
probabilities: np.ndarray = Field(
135+
default_factory=lambda: np.zeros(10), description=""
136+
)
124137
path: List[str] = Field([], description="Path to the hazard indicator data source.")
125138

126139
class Config:
@@ -139,7 +152,9 @@ class VulnerabilityCurve(BaseModel):
139152
intensity: List[float] = Field(...)
140153
intensity_units: str = Field(description="units of the intensity")
141154
impact_mean: List[float] = Field(description="mean impact (damage or disruption)")
142-
impact_std: List[float] = Field(description="standard deviation of impact (damage or disruption)")
155+
impact_std: List[float] = Field(
156+
description="standard deviation of impact (damage or disruption)"
157+
)
143158

144159
class Config:
145160
arbitrary_types_allowed = True
@@ -154,9 +169,15 @@ class VulnerabilityCurves(BaseModel):
154169
class VulnerabilityDistrib(BaseModel):
155170
"""Defines a vulnerability matrix."""
156171

157-
intensity_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
158-
impact_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
159-
prob_matrix: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
172+
intensity_bin_edges: np.ndarray = Field(
173+
default_factory=lambda: np.zeros(10), description=""
174+
)
175+
impact_bin_edges: np.ndarray = Field(
176+
default_factory=lambda: np.zeros(10), description=""
177+
)
178+
prob_matrix: np.ndarray = Field(
179+
default_factory=lambda: np.zeros(10), description=""
180+
)
160181

161182
class Config:
162183
arbitrary_types_allowed = True

src/physrisk/api/v1/exposure_req_resp.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class AssetExposureRequest(BaseModel):
1111

1212
assets: Assets
1313
calc_settings: CalcSettings = Field(
14-
default_factory=CalcSettings, description="Interpolation method." # type:ignore
14+
default_factory=CalcSettings,
15+
description="Interpolation method.", # type:ignore
1516
)
1617
scenario: str = Field("rcp8p5", description="Name of scenario ('rcp8p5')")
1718
year: int = Field(
@@ -29,7 +30,9 @@ class AssetExposureRequest(BaseModel):
2930
class Exposure(BaseModel):
3031
category: str
3132
value: Optional[float]
32-
path: str = Field("unknown", description="Path to the hazard indicator data source.")
33+
path: str = Field(
34+
"unknown", description="Path to the hazard indicator data source."
35+
)
3336

3437

3538
class AssetExposure(BaseModel):
@@ -40,7 +43,9 @@ class AssetExposure(BaseModel):
4043
description="""Asset identifier; will appear if provided in the request
4144
otherwise order of assets in response is identical to order of assets in request.""",
4245
)
43-
exposures: Dict[str, Exposure] = Field({}, description="Category (value) for each hazard type (key).")
46+
exposures: Dict[str, Exposure] = Field(
47+
{}, description="Category (value) for each hazard type (key)."
48+
)
4449

4550

4651
class AssetExposureResponse(BaseModel):

0 commit comments

Comments
 (0)