Skip to content

Commit

Permalink
Switched to ruff for linting
Browse files Browse the repository at this point in the history
This involved dropping Python 3.6 support.
  • Loading branch information
spookylukey committed Apr 2, 2024
1 parent 586e326 commit dcaf321
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 137 deletions.
62 changes: 17 additions & 45 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
branches: [ master ]

jobs:
build:
test:

runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -22,17 +22,11 @@ jobs:
- "3.10"
- "3.11"
- "3.12"
- "pypy3.6"
- "pypy3.7"
- "pypy3.8"
- "pypy3.9"
- "pypy3.10"
os: [ubuntu-latest]
include:
# Python 3.6 is not available on Ubuntu 22.04,
# so use Ubuntu 20.04:
- python-version: "3.6"
os: ubuntu-20.04


steps:
Expand All @@ -49,51 +43,29 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install -e .
pip install -r requirements.txt
pip install -r requirements-test.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --statistics
- name: Test with pytest
run: |
pytest
linters:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-linters.txt') }}
- name: Install deps
run: |
python -m pip install --upgrade pip
pip install -r requirements-linters.txt
- name: Run isort
run: |
isort --version
isort -c .
- name: Run flake8
if: failure() || success()
run: |
flake8 --version
flake8
- name: Run check-manifest
if: failure() || success()
run: |
check-manifest
- uses: psf/black@stable
with:
version: "22.3.0"
if: failure() || success()
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- uses: actions/cache@v2
with:
path: |
~/.cache/pip
~/.cache/pre-commit
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('requirements*.txt') }}
- name: Install dependencies
run: |
pip install pre-commit
- name: pre-commit linters
run: pre-commit run --all-files --show-diff-on-failure --color=always
35 changes: 4 additions & 31 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,17 @@ repos:
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: 'https://github.com/PyCQA/flake8'
rev: 4.0.1
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: flake8
- repo: 'https://github.com/pre-commit/mirrors-isort'
rev: v5.10.1
hooks:
- id: isort
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- repo: 'https://github.com/mgedmin/check-manifest'
rev: '0.46'
hooks:
- id: check-manifest
- repo: 'https://github.com/myint/autoflake'
rev: v1.4
hooks:
- id: autoflake
args:
- '--remove-all-unused-imports'
- '-i'
language_version: python3.9
- repo: 'https://github.com/pre-commit/mirrors-autopep8'
rev: v1.5.7
hooks:
- id: autopep8
language_version: python3.9
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3.9
- repo: https://github.com/ikamensh/flynt/
rev: '0.76'
hooks:
- id: flynt
language_version: python3.9
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
entry: pyupgrade --py3-plus --py36-plus --keep-runtime-typing
language_version: python3.9
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Unreleased
----------

* Dropped Python 3.6 support

fluent_compiler 1.0 (2023-04-18)
--------------------------------

Expand Down
5 changes: 2 additions & 3 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ development:
If all that is successful, you are in good shape to start developing!

We also have several linters and code formatters that we require use of,
including `flake8 <http://flake8.pycqa.org/en/latest/>`_, `isort
<https://github.com/timothycrosley/isort#readme>`_ and `black
<https://github.com/psf/black>`_. These are most easily add by using
including `ruff <https://github.com/astral-sh/ruff>`_ and `black
<https://github.com/psf/black>`_. These are most easily added by using
`pre-commit <https://pre-commit.com/>`_:

* Install pre-commit globally e.g. ``pipx install pre-commit`` if you
Expand Down
2 changes: 1 addition & 1 deletion RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Release process
===============

* Tests, including flake8, isort and check-manifest
* Tests, including linters and check-manifest

* Update CHANGELOG.rst, removing "(in development)" and adding date

Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Installation

$ pip install fluent_compiler

Python 3.6+ required (CPython or PyPy both tested). Earlier versions of Python 3
Python 3.7+ required (CPython or PyPy both tested). Earlier versions of Python 3
may work but they are not officially supported.
20 changes: 14 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ line-length = 120
# required-version = '22.3.0' # see https://github.com/psf/black/issues/2493
target-version = ['py39']

[tool.isort]
profile = "black"
line_length = 120
known_first_party = ["fluent_compiler"]
default_section= "THIRDPARTY"
skip = [".tox", "dist", "build", ".git"]
[tool.ruff]
line-length = 120
target-version = 'py37'

[tool.ruff.lint]
ignore = ["E501","E731"]
extend-select = [
"UP", # enable pyupgrade
"I", # enable isort
"FLY", # enable flynt
]

[tool.ruff.lint.isort]
known-first-party = ["fluent_compiler"]
3 changes: 1 addition & 2 deletions requirements-linters.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
isort==5.10.1
flake8==4.0.1
ruff==0.3.4
check-manifest
7 changes: 1 addition & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ classifiers =
License :: OSI Approved :: Apache Software License
Operating System :: OS Independent
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Expand All @@ -43,6 +42,7 @@ project_urls =
[options]
packages = find:
package_dir = =src
python_requires = >=3.7
install_requires =
fluent.syntax>=0.14
attrs>=19.3.0
Expand All @@ -51,8 +51,3 @@ install_requires =

[options.packages.find]
where = src

[flake8]
exclude = .tox,build,.eggs
ignore = D100,D101,W504,W503
max-line-length = 120
4 changes: 2 additions & 2 deletions src/fluent_compiler/ast_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def NewAst(...):
if sys.version_info >= (3, 8):
Constant = ast.Constant
else:
# For Python 3.6/3.7, in terms of runtime behaviour we could also use
# For Python 3.7, in terms of runtime behaviour we could also use
# Constant for Str/Num, but this seems to trigger bugs when decompiling with
# ast_decompiler, which is needed by tests. So we use the more normal
# ast that Python 3.6/3.7 use for this code.
# ast that Python 3.7 use for this code.
def Constant(arg, **kwargs):
if isinstance(arg, str):
return ast.Str(arg, **kwargs)
Expand Down
4 changes: 1 addition & 3 deletions src/fluent_compiler/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ def add_statement(self, statement):
else:
if statement.parent_block != self:
raise AssertionError(
"Block {} is already child of {}, can't reassign to {}".format(
statement, statement.parent_block, self
)
f"Block {statement} is already child of {statement.parent_block}, can't reassign to {self}"
)

# Safe alternatives to Block.statements being manipulated directly:
Expand Down
13 changes: 3 additions & 10 deletions src/fluent_compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,7 @@ def compile_term(term, block, compiler_env, new_escaper, term_args=None):
if not escapers_compatible(current_escaper, new_escaper):
term_id = ast_to_id(term)
error = TypeError(
"Escaper {} for term {} cannot be used from calling context with {} escaper".format(
new_escaper.name, term_id, current_escaper.name
)
f"Escaper {new_escaper.name} for term {term_id} cannot be used from calling context with {current_escaper.name} escaper"
)
add_static_msg_error(block, error)
compiler_env.add_current_message_error(error)
Expand All @@ -757,10 +755,7 @@ def compile_expr_term_reference(reference, block, compiler_env):

if args:
args_err = FluentFormatError(
"{}: Ignored positional arguments passed to term '{}'".format(
display_ast_location(reference.arguments, compiler_env),
reference_to_id(reference),
)
f"{display_ast_location(reference.arguments, compiler_env)}: Ignored positional arguments passed to term '{reference_to_id(reference)}'"
)
add_static_msg_error(block, args_err)
compiler_env.add_current_message_error(args_err)
Expand Down Expand Up @@ -1026,9 +1021,7 @@ def do_message_call(msg_id, block, compiler_env):
new_escaper = compiler_env.escaper_for_message(msg_id)
if not escapers_compatible(current_escaper, new_escaper):
error = TypeError(
"Escaper {} for message {} cannot be used from calling context with {} escaper".format(
new_escaper.name, msg_id, current_escaper.name
)
f"Escaper {new_escaper.name} for message {msg_id} cannot be used from calling context with {current_escaper.name} escaper"
)
add_static_msg_error(block, error)
compiler_env.add_current_message_error(error)
Expand Down
11 changes: 3 additions & 8 deletions src/fluent_compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def attribute_ast_to_id(attribute, parent_ast):


def allowable_name(ident, for_method=False, allow_builtin=False):

if keyword.iskeyword(ident):
return False

Expand Down Expand Up @@ -130,17 +129,13 @@ def args_match(function_name, args, kwargs, arg_spec):
if len_args > positional_arg_count:
errors.append(
TypeError(
"{}() takes {} positional arguments but {} were given".format(
function_name, positional_arg_count, len_args
)
f"{function_name}() takes {positional_arg_count} positional arguments but {len_args} were given"
)
)
elif len_args < positional_arg_count:
errors.append(
TypeError(
"{}() takes {} positional arguments but {} were given".format(
function_name, positional_arg_count, len_args
)
f"{function_name}() takes {positional_arg_count} positional arguments but {len_args} were given"
)
)
match = False
Expand All @@ -165,7 +160,7 @@ def reference_to_id(ref, ignore_attributes=False):
start = ref.id.name

if not ignore_attributes and ref.attribute:
return "".join([start, ATTRIBUTE_SEPARATOR, ref.attribute.name])
return f"{start}{ATTRIBUTE_SEPARATOR}{ref.attribute.name}"
return start


Expand Down
14 changes: 7 additions & 7 deletions tools/benchmarks/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_single_string_gettext(gettext_translations, benchmark):
"Hello I am a single string literal",
)
assert result == "Hello I am a single string literal in Polish"
assert type(result) is str
assert type(result) is str # noqa: E721


def test_single_string_fluent_interpreter_cached(interpreting_fluent_bundle, benchmark):
Expand All @@ -140,7 +140,7 @@ def test_single_string_fluent_interpreter_cached(interpreting_fluent_bundle, ben
message_val = interpreting_fluent_bundle.get_message("single-string-literal").value
result = benchmark(interpreting_fluent_bundle.format_pattern, message_val)
assert result[0] == "Hello I am a single string literal in Polish"
assert type(result[0]) is str
assert type(result[0]) is str # noqa: E721


def test_single_string_fluent_interpreter(interpreting_fluent_bundle, benchmark):
Expand All @@ -151,13 +151,13 @@ def test_single_string_fluent_interpreter(interpreting_fluent_bundle, benchmark)
)
)
assert result[0] == "Hello I am a single string literal in Polish"
assert type(result[0]) is str
assert type(result[0]) is str # noqa: E721


def test_single_string_fluent_compiler(compiling_fluent_bundle, benchmark):
result = benchmark(compiling_fluent_bundle.format, "single-string-literal")
assert result[0] == "Hello I am a single string literal in Polish"
assert type(result[0]) is str
assert type(result[0]) is str # noqa: E721


def test_single_interpolation_gettext(gettext_translations, benchmark):
Expand All @@ -166,7 +166,7 @@ def test_single_interpolation_gettext(gettext_translations, benchmark):
args = {"username": "Mary"}
result = benchmark(lambda: t("Hello %(username)s, welcome to our website!") % args)
assert result == "Hello Mary, welcome to our website! in Polish"
assert type(result) is str
assert type(result) is str # noqa: E721


def test_single_interpolation_fluent_interpreter(interpreting_fluent_bundle, benchmark):
Expand All @@ -177,14 +177,14 @@ def test_single_interpolation_fluent_interpreter(interpreting_fluent_bundle, ben
)
)
assert result[0] == "Hello Mary, welcome to our website! in Polish"
assert type(result[0]) is str
assert type(result[0]) is str # noqa: E721


def test_single_interpolation_fluent_compiler(compiling_fluent_bundle, benchmark):
args = {"username": "Mary"}
result = benchmark(compiling_fluent_bundle.format, "single-interpolation", args)
assert result[0] == "Hello Mary, welcome to our website! in Polish"
assert type(result[0]) is str
assert type(result[0]) is str # noqa: E721


def test_plural_form_select_gettext(gettext_translations, benchmark):
Expand Down
Loading

0 comments on commit dcaf321

Please sign in to comment.