Skip to content

Commit

Permalink
require Python 3.10 and associated ruff updates (#39)
Browse files Browse the repository at this point in the history
This is a bit of a mixed bag of changes similar to which we merged in
https://github.com/Deltares/Ribasim last weeks.

First of all we now require at least Python 3.10. Before this was 3.9.
The differences are not too big, but I don't think we need to spend out
time supporting old Python versions at this moment, 3.10 is sufficiently
old.

Since we now tell ruff to target Python 3.10, and ask it to give syntax
upgrade suggestions, we fix those. Most of this is nicer typing
annotations, using e.g. `dict` instead of `from typing import Dict`. The
ruff updates also includes some configuration to make it work well with
notebooks.

Lastly I removed the typos check. I don't think it is suitable for this
repository as there are many Dutch terms used in the code and docs.
  • Loading branch information
visr authored Nov 20, 2023
1 parent 631659d commit 7c222ba
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 89 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# pixi environments
.pixi

# visual studio code
.vscode/settings.json

.ipynb_checkpoints
# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,3 @@ repos:
rev: 0.6.1
hooks:
- id: nbstripout
- repo: https://github.com/crate-ci/typos
rev: v1.16.21
hooks:
- id: typos
2 changes: 0 additions & 2 deletions .typos.toml

This file was deleted.

3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ms-python.python",
"ms-python.mypy-type-checker",
"charliermarsh.ruff",
"njpwerner.autodocstring"
"njpwerner.autodocstring",
"quarto.quarto"
]
}
4 changes: 4 additions & 0 deletions .vscode/settings_template.json → .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"source.fixAll.ruff": true,
"source.organizeImports.ruff": true
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
Expand Down
3 changes: 1 addition & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ dependencies:
- pandas
- pandas-stubs
- pip
- pip:
- quartodoc
- pre-commit
- pyarrow
- pydantic=1
- pyogrio
- pytest
- pytest-cov
- python>=3.9
- quartodoc
- rasterstats
- ruff
- shapely>=2.0
Expand Down
41 changes: 19 additions & 22 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
select = ["D", "E", "F", "NPY", "PD", "C4", "I"]
# See https://docs.astral.sh/ruff/rules/
select = ["D", "E", "F", "NPY", "PD", "C4", "I", "UP"]
ignore = ["D1", "D202", "D205", "D400", "D404", "E501", "PD002", "PD901"]
fixable = ["I"]
extend-include = ["*.ipynb"]
target-version = "py310"

[pydocstyle]
convention = "numpy"
45 changes: 22 additions & 23 deletions src/hydamo/hydamo/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import warnings
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional
from typing import Any, Literal

import fiona
import geopandas as gpd
Expand Down Expand Up @@ -40,26 +40,26 @@
"number": "float",
}

default_properties: Dict[str, Any] = {
default_properties: dict[str, Any] = {
"id": None,
"dtype": "str",
"required": False,
"unique": False,
}


def map_definition(definition: Dict[str, Any]) -> List[Dict[str, Any]]:
def map_definition(definition: dict[str, Any]) -> list[dict[str, Any]]:
"""
Parameters
----------
definition : Dict
definition : dict[str, Any]
HyDAMO definition as specified in the HyDAMO JSON specification.
Returns
-------
List
list[dict[str, Any]]
Validation schema for the HyDAMO class.
"""
Expand All @@ -70,7 +70,7 @@ def map_definition(definition: Dict[str, Any]) -> List[Dict[str, Any]]:
for k, v in definition.items():
# convert geometry if shape
if k == "shape":
properties: Dict[str, Any] = {"id": "geometry"}
properties: dict[str, Any] = {"id": "geometry"}
dtype = v["type"]
if not isinstance(dtype, list):
dtype = [dtype]
Expand Down Expand Up @@ -116,21 +116,20 @@ class ExtendedGeoDataFrame(gpd.GeoDataFrame): # type: ignore

def __init__(
self,
validation_schema: List[Dict[str, Any]],
geotype: Optional[
List[
Literal[
"LineString",
"MultiLineString",
"Point",
"PointZ",
"Polygon",
"MultiPolygon",
]
validation_schema: list[dict[str, Any]],
geotype: list[
Literal[
"LineString",
"MultiLineString",
"Point",
"PointZ",
"Polygon",
"MultiPolygon",
]
],
]
| None,
layer_name: str = "",
required_columns: List[str] = [],
required_columns: list[str] = [],
logger=logging,
*args,
**kwargs,
Expand All @@ -144,7 +143,7 @@ def __init__(
# else:
kwargs["columns"] = required_columns

super(ExtendedGeoDataFrame, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

self.validation_schema = validation_schema
self.required_columns = required_columns
Expand Down Expand Up @@ -301,7 +300,7 @@ def __init__(
self,
version: str = "2.2",
schemas_path: Path = SCHEMAS_DIR,
ignored_layers: List[str] = [
ignored_layers: list[str] = [
"afvoeraanvoergebied",
"imwa_geoobject",
"leggerwatersysteem",
Expand All @@ -311,7 +310,7 @@ def __init__(
):
self.version = version
self.schema_json = schemas_path.joinpath(f"HyDAMO_{version}.json")
self.layers: List[str] = []
self.layers: list[str] = []
self.ignored_layers = ignored_layers

self.init_datamodel()
Expand All @@ -322,7 +321,7 @@ def data_layers(self):

def init_datamodel(self) -> None:
"""Initialize DataModel from self.schemas_path."""
self.validation_schemas: Dict[str, Any] = {}
self.validation_schemas: dict[str, Any] = {}

# read schema as dict
with open(self.schema_json) as src:
Expand Down
2 changes: 1 addition & 1 deletion src/hydamo/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
]
license = { text = "MIT" }

requires-python = ">=3.9"
requires-python = ">=3.10"
dependencies = [
"geopandas",
]
Expand Down
2 changes: 1 addition & 1 deletion src/ribasim_nl/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
]
license = { text = "MIT" }

requires-python = ">=3.9"
requires-python = ">=3.10"
dependencies = [
"geopandas",
]
Expand Down
Loading

0 comments on commit 7c222ba

Please sign in to comment.