Skip to content

Commit

Permalink
Ruff and precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak committed Aug 4, 2024
1 parent 9eaf8e8 commit 03bf1ed
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 110 deletions.
8 changes: 3 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
},
"extensions": [
"ms-python.python",
"ms-python.black-formatter",
"ms-python.flake8",
"ms-python.isort",
"matangover.mypy",
"ms-python.vscode-pylance"
"ms-python.vscode-pylance",
"charliermarsh.ruff",
"matangover.mypy"
]
}
},
Expand Down
23 changes: 0 additions & 23 deletions .flake8

This file was deleted.

8 changes: 3 additions & 5 deletions .github/workflows/python-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ jobs:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pip install --upgrade -e .[pinned,dev]
- name: Run pre-commit checks
run: |
./pre-commit
./pre-commit lint
test:
runs-on: ubuntu-latest
strategy:
Expand All @@ -32,8 +31,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pip install --upgrade -e .[pinned,dev,test]
- name: Run pytest
run: |
pytest --doctest-modules -m "not runbot"
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ __pycache__
dist
.ipynb_checkpoints
*.egg-info
.mypy_cache
.pytest_cache
.*_cache
demo*.ipynb
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Alternatively, clone and setup the repository manually.
git clone $url
cd odoo-connect
# Install dev libraries
pip install -r requirements.txt
pip install -e .[pinned,dev,test]
./pre-commit install
# Run some tests
pytest
Expand Down
2 changes: 1 addition & 1 deletion odoo_connect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import urllib.parse
from typing import Optional

from .odoo_rpc import OdooClient, OdooModel, OdooServerError # noqa
from .odoo_rpc import OdooClient, OdooModel, OdooServerError

__doc__ = """Simple Odoo RPC library."""

Expand Down
11 changes: 4 additions & 7 deletions odoo_connect/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_attachment(
return b''
value = value[0]
elif field_info.get("type") != "binary":
raise RuntimeError("%s is neither a binary or an ir.attachment field" % field_name)
raise RuntimeError(f"{field_name} is neither a binary or an ir.attachment field")
if isinstance(value, int):
return get_attachment(model, value)
return decode_binary(value)
Expand Down Expand Up @@ -96,10 +96,7 @@ def list_attachments(
if model.model == 'ir.attachment':
attachments = model
data = attachments.search_read(
[
('id', 'in', ids),
]
+ domain,
[('id', 'in', ids), *domain],
fields,
)
else:
Expand All @@ -109,8 +106,8 @@ def list_attachments(
('res_model', '=', model.model),
('res_id', 'in', ids),
('id', '!=', 0), # to get all res_field
]
+ domain,
*domain,
],
fields,
)
# Get contents
Expand Down
22 changes: 11 additions & 11 deletions odoo_connect/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ def load_data(
formatter = Formatter(model)

log.info("Load: convert and format data")
if method_row_type == dict:
if method_row_type is dict:
data = __convert_to_type_dict(data, fields)
data = [formatter.format_dict(d) for d in data]
elif method_row_type == list:
elif method_row_type is list:
data, fields = __convert_to_type_list(data, fields)
formatter_functions = [formatter.format_function[f] for f in fields]
data = [[ff(v) for ff, v in zip(formatter_functions, d)] for d in data]
else:
raise Exception('Unsupported method row type: %s' % method_row_type)
raise Exception(f'Unsupported method row type: {method_row_type}')

log.info("Load data using %s.%s(), %d records", model.model, method, len(data))
if method == 'load':
Expand Down Expand Up @@ -126,7 +126,7 @@ def __convert_to_type_list(
fields = first_row
data = idata
elif not isinstance(data, list):
data = [first_row] + list(data)
data = [first_row, *list(data)]
return data, fields


Expand All @@ -141,11 +141,11 @@ def __convert_to_type_dict(data: Iterable, fields: Optional[list[str]]) -> Itera
data = (dict(zip(fields, d)) for d in idata)
else:
if not isinstance(data, list):
data = [first_row] + list(idata)
data = [first_row, *list(idata)]
if fields:
data = ({f: d[i] for i, f in enumerate(fields)} for d in data)
elif not isinstance(data, list):
data = [first_row] + list(idata)
data = [first_row, *list(idata)]
return data


Expand Down Expand Up @@ -371,7 +371,7 @@ def add_fields(
"""
domain_by_field: list[Any] = [(by_field, 'in', [d[by_field] for d in data])]
domain = ['&'] + domain_by_field + (domain if domain else domain_by_field)
fetched_data = model.search_read_dict(domain, fields + [by_field])
fetched_data = model.search_read_dict(domain, [*fields, by_field])
index = {d.pop(by_field): d for d in fetched_data}
if len(index) != len(fetched_data):
raise Exception(f'{by_field} is not unique in {model.model} when adding fields')
Expand Down Expand Up @@ -405,7 +405,7 @@ def add_xml_id(model: OdooModel, data: list, *, id_name='id', xml_id_field='xml_
else:
ids.add(row[cast(int, id_index)])
else:
raise TypeError('Cannot append the url to %s' % type(row))
raise TypeError(f'Cannot append the url to {type(row)}')
xml_ids = {
i['res_id']: i['complete_name']
for i in model.odoo['ir.model.data'].search_read(
Expand Down Expand Up @@ -445,9 +445,9 @@ def add_url(
base_url = model.odoo.url
if not model_id_func and data:
if isinstance(data[0], list):
model_id_func = lambda r: (model.model, r[0]) # noqa: E731
model_id_func = lambda r: (model.model, r[0])
else:
model_id_func = lambda d: (model.model, d['id']) # noqa: E731
model_id_func = lambda d: (model.model, d['id'])
for row_num, row in enumerate(data):
model_name, id = model_id_func(row)
url = build_url(row, model_name, id)
Expand All @@ -461,7 +461,7 @@ def add_url(
else:
row.append(url)
else:
raise TypeError('Cannot append the url to %s' % type(row))
raise TypeError(f'Cannot append the url to {type(row)}')


def _flatten(value, access: list[str]) -> Any:
Expand Down
14 changes: 6 additions & 8 deletions odoo_connect/odoo_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _find_default_database(self, *, monodb=True) -> str:
# Fail or default
if self.database:
return self.database
raise OdooServerError('Cannot determine the database for [%s]' % self.url)
raise OdooServerError(f'Cannot determine the database for [{self.url}]')

def authenticate(self, username: str, password: str):
"""Authenticate with username and password"""
Expand All @@ -121,7 +121,7 @@ def authenticate(self, username: str, password: str):
self._password = password
if not username:
if old_username:
log.info('Logged out [%s]' % self.url)
log.info(f'Logged out [{self.url}]')
return
if not self._database:
raise OdooServerError('Missing database to connect')
Expand All @@ -135,7 +135,7 @@ def authenticate(self, username: str, password: str):
user_agent_env,
)
if not self._uid:
raise OdooServerError('Failed to authenticate user %s' % username)
raise OdooServerError(f'Failed to authenticate user {username}')
log.info("Login successful [%s], [%s] uid: %d", self.url, self.username, self._uid)

def _json_rpc(self, method: str, params: Any):
Expand Down Expand Up @@ -191,7 +191,7 @@ def get_model(self, model_name: str, check: bool = False) -> "OdooModel":
# let's fetch the fields (which we probably will do anyways)
model.fields()
except: # noqa: E722
raise OdooServerError('Model %s not found' % model)
raise OdooServerError(f'Model {model} not found')
return model

def list_databases(self) -> list[str]:
Expand Down Expand Up @@ -221,9 +221,7 @@ def ref(
if to_return:
return to_return[0]
if raise_if_not_found:
raise ValueError(
'No record found for unique ID %s. It may have been deleted.' % (xml_id)
)
raise ValueError(f'No record found for unique ID {xml_id}. It may have been deleted.')
return {}

def version(self) -> dict:
Expand Down Expand Up @@ -368,7 +366,7 @@ def __prepare_dict_fields(self, fields: Union[list[str], dict[str, dict]]) -> di
new_fields.update({k: v for k, v in fields.items() if k not in new_fields})
return new_fields
return fields
raise ValueError('Invalid fields parameter: %s' % fields)
raise ValueError(f'Invalid fields parameter: {fields}')

def __read_dict_date(self, data, fields):
"""Transform dates into ISO-like format"""
Expand Down
42 changes: 27 additions & 15 deletions pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,46 @@
set -eu
# Check directory
cd "$(dirname "$0")"
[[ "$0" != *.git/hooks/* ]] || cd ../..
[ -d .git ] || cd ../..
[ -d .git ]

pre_commit() {
flake8
black --check .
isort --check-only .
check() {
echo " ruff:"
ruff check
ruff format --check
}

lint() {
pre_commit
mypy --install-types --non-interactive .
check
echo " mypy:"
mypy .
}

fix() {
ruff check --fix-only .
}

format() {
black .
isort .
ruff format
}

# Commands
case "${1:-run}" in
run)
pre_commit
echo "All good to commit"
run|check)
check
echo " all good to commit."
;;
lint)
lint;;
lint
;;
fix)
echo "Fix all..."
fix
format
;;
format)
format;;
format
;;
install)
echo "Installing pre-commit"
cd .git/hooks
Expand All @@ -41,5 +53,5 @@ case "${1:-run}" in
;;
*)
echo "Invalid argument: $*"
echo "Supported options: lint, install, uninstall"
echo "Supported options: lint, fix, format, install, uninstall"
esac
Loading

0 comments on commit 03bf1ed

Please sign in to comment.