Skip to content

Commit

Permalink
style: configure pre-commit to run black, reorder-python-import
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Feb 9, 2023
1 parent 6ebab0c commit 8036a50
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 125 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[run]
source_pkgs = unflatten
parallel = true

[paths]
source =
src
Expand Down
50 changes: 43 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
name: Tests

on: [push]
on:
push:
branches: ["*"]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
tests:
name: Python ${{ matrix.python-version }} (${{ matrix.runner }})
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix:
python-version: [2.7, 3.6, 3.7, 3.8, 3.9, pypy-2.7, pypy-3.7]
runner: [ubuntu-latest]
python-version: [3.7, 3.8, 3.9, pypy-2.7, pypy-3.7]
include:
- python-version: 2.7
runner: ubuntu-20.04
- python-version: 3.6
runner: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -21,3 +32,28 @@ jobs:
pip install tox tox-gh-actions
- name: Test with tox
run: tox
- uses: actions/upload-artifact@v3
with:
name: coverage-data
path: .coverage.*

coverage:
needs: tests
if: ${{ success() || failure() }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: coverage-data
- run: python -m pip install tox
- run: tox -e cover-report

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: python -m pip install pre-commit tox
- run: pre-commit run -a
- run: tox r -e lint
if: ${{ success() || failure() }}
38 changes: 38 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: check-toml
- id: check-symlinks
- id: check-added-large-files
- id: check-vcs-permalinks
- id: debug-statements
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: ["--py26-plus", "--application-directories=.:src"]

- repo: https://github.com/PyCQA/flake8
rev: '6.0.0'
hooks:
- id: flake8
args: ["--append-config", "tox.ini"]
language_version: python3
additional_dependencies:
# NOTE: autoupdate does not pick up flake8-bugbear since it is a
# transitive dependency. Make sure to update flake8-bugbear
# manually on a regular basis.
- flake8-bugbear==23.1.20

- repo: https://github.com/psf/black
rev: '23.1.0'
hooks:
- id: black
5 changes: 4 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
exclude .gitignore
exclude .pre-commit-config.yaml

# these are redundant, as setuptools-scm includes everything in the git repo
include LICENSE.txt README.rst CHANGES.rst
include tox.ini .coveragerc
exclude .gitignore
recursive-include tests *.py
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,3 @@ Authors
.. |build status| image::
https://github.com/dairiki/unflatten/actions/workflows/tests.yml/badge.svg
:target: https://github.com/dairiki/unflatten/actions/workflows/tests.yml

1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ setup_requires =

[bdist_wheel]
universal = 1

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from setuptools import setup

setup(use_scm_version=True)
35 changes: 19 additions & 16 deletions src/unflatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"""
from __future__ import absolute_import

from operator import itemgetter
import re
import sys
from operator import itemgetter

if sys.version_info[0] == 2:
string_type = basestring # noqa: F821
string_type = basestring # noqa: F821
else:
string_type = str

Expand Down Expand Up @@ -47,9 +47,9 @@ def unflatten(arg):
{'foo': [{'bar': 'val'}, {'baz': 'x'}]}
"""
if hasattr(arg, 'iteritems'):
if hasattr(arg, "iteritems"):
items = arg.iteritems()
elif hasattr(arg, 'items'):
elif hasattr(arg, "items"):
items = arg.items()
else:
items = arg
Expand All @@ -59,8 +59,7 @@ def unflatten(arg):
for flat_key, val in items:
parsed_key = _parse_key(flat_key)
obj = data
for depth, (key, next_key) in enumerate(zip(parsed_key,
parsed_key[1:]), 1):
for depth, (key, next_key) in enumerate(zip(parsed_key, parsed_key[1:]), 1):
if isinstance(next_key, string_type):
holder_type = _dict_holder
else:
Expand All @@ -71,17 +70,21 @@ def unflatten(arg):
holders.append((obj, key))
elif not isinstance(obj[key], holder_type):
raise ValueError(
"conflicting types %s and %s for key %r" % (
"conflicting types %s and %s for key %r"
% (
_node_type(obj[key]),
holder_type.node_type,
_unparse_key(parsed_key[:depth])))
_unparse_key(parsed_key[:depth]),
)
)
obj = obj[key]

last_key = parsed_key[-1]
if isinstance(obj.get(last_key), _holder):
raise ValueError(
"conflicting types %s and terminal for key %r" % (
_node_type(obj[last_key]), flat_key))
"conflicting types %s and terminal for key %r"
% (_node_type(obj[last_key]), flat_key)
)
obj[last_key] = val

for obj, key in reversed(holders):
Expand All @@ -92,9 +95,9 @@ def unflatten(arg):

def _node_type(value):
if isinstance(value, _holder):
return value.node_type,
return (value.node_type,)
else:
return 'terminal'
return "terminal"


class _holder(dict):
Expand Down Expand Up @@ -137,7 +140,7 @@ def getvalue(self):
return value


_dot_or_indexes_re = re.compile(r'(\.|(?:\[\d+\])+(?=\.|\Z))')
_dot_or_indexes_re = re.compile(r"(\.|(?:\[\d+\])+(?=\.|\Z))")


def _parse_key(flat_key):
Expand All @@ -148,11 +151,11 @@ def _parse_key(flat_key):
parts = [split_key[0]]
for i in range(1, len(split_key), 2):
sep = split_key[i]
if sep == '.':
if sep == ".":
parts.append(split_key[i + 1])
else:
# Note that split_key[i + 1] is a bogus empty string.
parts.extend(map(int, re.findall(r'\d+', sep)))
parts.extend(map(int, re.findall(r"\d+", sep)))
return parts


Expand All @@ -164,4 +167,4 @@ def _unparse_key(parsed):
else:
fmt = "[%d]"
bits.append(fmt % part)
return ''.join(bits)
return "".join(bits)
Loading

0 comments on commit 8036a50

Please sign in to comment.