Skip to content

Commit

Permalink
Move to ruff (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbjorge authored Feb 8, 2024
1 parent 484be8a commit 33acf51
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 90 deletions.
6 changes: 0 additions & 6 deletions .flake8

This file was deleted.

12 changes: 4 additions & 8 deletions .github/workflows/fmu-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,13 @@ jobs:
- name: List all installed packages
run: pip freeze

- name: Lint with isort
- name: Ruff check
if: ${{ always() }}
run: isort --check-only --profile black src tests
run: ruff check .

- name: Lint with black
- name: Ruff format check
if: ${{ always() }}
run: black --check src tests

- name: Lint with flake8
if: ${{ always() }}
run: flake8 src tests
run: ruff format . --check

- name: Check typing with mypy
if: ${{ always() }}
Expand Down
8 changes: 3 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ If you are proposing a feature:
## Code standards

It is very important to be complient to code standards. fmu-config uses
[black](https://pypi.org/project/black/),
[flake8](https://pypi.org/project/flake8/),
[isort](https://pypi.org/project/isort/), and
[ruff](https://pypi.org/project/ruff/),
[mypy](https://mypy.readthedocs.io/en/stable/) to format and lint all code.


Expand All @@ -62,10 +60,10 @@ It is very important to be complient to code standards. fmu-config uses
- Code shall be be Python 3.8+ compliant


### Use flake8 and/or pylint to check
### Linting

```sh
python -m flake8 mycode.py
ruff check . && ruff format . --check
```

The pylint is rather strict and sometimes excpetions are needed... , but anyway quite useful!
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![fmu-config](https://github.com/equinor/fmu-config/workflows/fmu-config/badge.svg)
![Python Version](https://img.shields.io/badge/python-3.8%20|%203.9%20|%203.10%20|%203.11%20|%203.12-blue.svg)
[![License: LGPL v3](https://img.shields.io/github/license/equinor/fmu-tools)](https://www.gnu.org/licenses/lgpl-3.0)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![PyPI](https://img.shields.io/pypi/v/fmu-config.svg)](https://pypi.org/project/fmu-config/)

FMU config is a small Python library to facilitate configuration of global
Expand Down
41 changes: 30 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ dependencies = ["pyyaml"]
dev = [
"astroid",
"autoapi",
"black",
"coverage",
"flake8",
"isort",
"mypy",
"myst-parser",
"pylint",
"pytest",
"pytest-runner",
"pytest",
"rstcheck",
"Sphinx",
"ruff",
"sphinx_rtd_theme",
"Sphinx",
"types-PyYAML",
]

Expand All @@ -57,12 +55,6 @@ Repository = "https://github.com/equinor/fmu-config"
[project.scripts]
fmuconfig = "fmu.config.fmuconfigrunner:main"

[tool.black]
line-length = 88

[tool.isort]
profile = "black"

[tool.mypy]
ignore_missing_imports = true

Expand All @@ -84,3 +76,30 @@ markers = [

[tool.rstcheck]
ignore_directives = ["argparse", "automodule"]

[tool.ruff]
line-length = 88
exclude = ["docs/"]

[tool.ruff.lint]
ignore = [
"C901",
]
select = [
"C",
"E",
"F",
"I",
"PIE",
"Q",
"RET",
"RSE",
"SIM",
"W",
# "C90",
# "NPY",
# "PD",
# "PL",
]
[tool.ruff.lint.isort]
combine-as-imports = true
33 changes: 18 additions & 15 deletions src/fmu/config/_configparserfmu_ipl.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,11 @@ def _ipl_freeform_format(self, template=False):


def _freeform_handle_entry(
variable, myvalue, myvalues, dtype, template
variable,
myvalue,
myvalues,
dtype,
template,
): # pylint: disable=too-many-statements
"""Handling of any entry as single value or list in IPL.
Expand Down Expand Up @@ -487,19 +491,18 @@ def _fix_date_format(var, dtype, value, aslist=False):
mynewvalues.append(val)
returnv = mynewvalues

if dtype == "datepair":
if values:
mynewvalues = []
for val in values:
date1, date2 = val
if isinstance(date1, (datetime.datetime, datetime.date)):
date1 = str(date1)
date1 = date1.replace("-", "")
if isinstance(date2, (datetime.datetime, datetime.date)):
date2 = str(date2)
date2 = date2.replace("-", "")
mynewvalues.append(date1 + "_" + date2)

returnv = mynewvalues
if dtype == "datepair" and values:
mynewvalues = []
for val in values:
date1, date2 = val
if isinstance(date1, (datetime.datetime, datetime.date)):
date1 = str(date1)
date1 = date1.replace("-", "")
if isinstance(date2, (datetime.datetime, datetime.date)):
date2 = str(date2)
date2 = date2.replace("-", "")
mynewvalues.append(date1 + "_" + date2)

returnv = mynewvalues

return returnv
23 changes: 7 additions & 16 deletions src/fmu/config/configparserfmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
except ImportError:
__version__ = "0.0.0"

from fmu.config import _configparserfmu_ipl, etc
from fmu.config import oyaml as yaml
from fmu.config import _configparserfmu_ipl, etc, oyaml as yaml
from fmu.config._loader import ConstructorError, FmuLoader

xfmu = etc.Interaction()
Expand Down Expand Up @@ -286,10 +285,7 @@ def to_json(
# remove dtype, value(s) from RMS/IPL freeform entries
newcfg = self._strip_rmsdtype()

if tool is not None:
mycfg = newcfg[tool]
else:
mycfg = newcfg
mycfg = newcfg[tool] if tool is not None else newcfg

mystream = json.dumps(mycfg, indent=4, default=str, ensure_ascii=False)

Expand Down Expand Up @@ -493,16 +489,14 @@ def _get_sysinfo(commentmarker="#"):
ver = __version__
cmt = commentmarker

meta = [
return [
"{} Autogenerated from global configuration.\n".format(cmt),
"{} DO NOT EDIT THIS FILE MANUALLY!\n".format(cmt),
"{} Machine {} by user {}, at {}, using fmu.config ver. {}\n".format(
cmt, host, user, now, ver
),
]

return meta

@staticmethod
def _force_create_folders(folderlist):
for folder in folderlist:
Expand Down Expand Up @@ -544,11 +538,11 @@ def _strip_rmsdtype(self):
logger.debug(key, val)
if isinstance(val, dict):
logger.debug(val.keys())
if "dtype" and "value" in val.keys():
if "dtype" and "value" in val:
newcfg["rms"][key] = deepcopy(val["value"])
elif "dtype" and "values" in val.keys():
elif "dtype" and "values" in val:
newcfg["rms"][key] = deepcopy(val["values"])
elif "dtype" in val.keys():
elif "dtype" in val:
raise RuntimeError(
'Wrong input YAML?. It seems that "{}" has '
'"dtype" but no "value" or "values" ({})'.format(
Expand Down Expand Up @@ -601,10 +595,7 @@ def _get_required_form(stream, template=False, ipl=False):
else:
result = value + " // " + tvalue
else:
if template:
result = tvalue
else:
result = value
result = tvalue if template else value

else:
raise ValueError("Input for templateconversion neither string " "or list")
Expand Down
3 changes: 1 addition & 2 deletions src/fmu/config/fmuconfigrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def _do_parse_args(args):
print("QUIT")
raise SystemExit

args = parser.parse_args(args)
return args
return parser.parse_args(args)


def main(args=None):
Expand Down
8 changes: 3 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import os.path
from os.path import join

import pytest

import fmu.config as config
from fmu.config import oyaml as yaml
from fmu.config import utilities as ut
import pytest
from fmu.config import oyaml as yaml, utilities as ut

# import fmu.config.fmuconfigrunner as fmurun

Expand All @@ -25,7 +23,7 @@

# always this statement
if not fmux.testsetup():
raise SystemExit()
raise SystemExit

TMPD = fmux.tmpdir

Expand Down
2 changes: 1 addition & 1 deletion tests/test_config_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# always this statement
if not FMUX.testsetup():
raise SystemExit()
raise SystemExit


def test_ipl_guess_dtype():
Expand Down
9 changes: 3 additions & 6 deletions tests/test_etc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

import pytest
import yaml
from fmu.config import etc, utilities as util
from yaml.constructor import ConstructorError

from fmu.config import etc
from fmu.config import utilities as util

fmux = etc.Interaction()
logger = fmux.basiclogger(__name__)

Expand All @@ -18,7 +16,7 @@

# always this statement
if not fmux.testsetup():
raise SystemExit()
raise SystemExit


def test_info_logger_plain():
Expand All @@ -31,8 +29,7 @@ def test_info_logger_plain():
def fixture_mylogger():
"""Add logger"""
# need to do it like this...
mlogger = fmux.basiclogger(__name__, level="DEBUG")
return mlogger
return fmux.basiclogger(__name__, level="DEBUG")


def test_info_logger(mylogger, caplog):
Expand Down
16 changes: 5 additions & 11 deletions tests/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import io
from os.path import join

import pytest

import fmu.config as fcfg
import pytest
from fmu.config import utilities as ut

fmux = fcfg.etc.Interaction()
logger = fmux.basiclogger(__name__)

# always this statement
if not fmux.testsetup():
raise SystemExit()
raise SystemExit


def test_dict_with_scalar_and_lists1(tmp_path):
Expand Down Expand Up @@ -121,7 +120,7 @@ def test_process_value2(tmp_path):
assert tmpl["global"]["WHATEVER"] == "<ONX>"


@pytest.mark.parametrize("exponent", range(0, 16))
@pytest.mark.parametrize("exponent", range(16))
def test_small_float(tmp_path, exponent):
"""Test for issue 1, small floats in input yaml can give non-compliant
formats in output IPL (IPL does not support Pythons scientific format)
Expand All @@ -146,13 +145,8 @@ def test_small_float(tmp_path, exponent):
out = str(tmp_path.resolve())

cfg.to_ipl(rootname="foo", destination=out, template=out)
ipl_lines = [
line
for line in open(
join(str(tmp_path.resolve()), "foo.ipl"), encoding="utf-8"
).readlines()
if line.startswith("SMALLFLOAT")
]
with open(join(str(tmp_path.resolve()), "foo.ipl"), encoding="utf-8") as f:
ipl_lines = [line for line in f.readlines() if line.startswith("SMALLFLOAT")]
significand = ipl_lines[0].split(" = ")[1].split("E")[0].split("e")[0]

# Verify we have a floating point significand if we have written
Expand Down
5 changes: 2 additions & 3 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Testing fmu-config tools."""
import pytest

import fmu.config as config
import pytest
from fmu.config import utilities as utils

# import fmu.config.fmuconfigrunner as fmurun
Expand All @@ -13,7 +12,7 @@

# always this statement
if not fmux.testsetup():
raise SystemExit()
raise SystemExit


def test_basic_tools():
Expand Down

0 comments on commit 33acf51

Please sign in to comment.