Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff and pyproject.toml #239

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ on:
- cron: '17 3 * * 0'

jobs:
flake8:
name: Flake8
ruff:
name: Ruff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
uses: actions/setup-python@v5
with:
# matches compat target in setup.py
python-version: '3.8'
- uses: actions/setup-python@v5
- name: "Main Script"
run: |
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
. ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)"
pip install ruff
ruff check

validate_cff:
name: Validate CITATION.cff
Expand Down
6 changes: 3 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Pytest without Numpy:

Flake8:
script:
- curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
- . ./prepare-and-run-flake8.sh "$CI_PROJECT_NAME"
- pipx install ruff
- ruff check
tags:
- python3
- docker-runner
except:
- tags

Expand Down
97 changes: 97 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools>=63",
]

[project]
name = "pytools"
version = "2024.1.6"
description = "A collection of tools for Python"
readme = "README.rst"
license = { text = "MIT" }
requires-python = "~=3.8"
authors = [
{ name = "Andreas Kloeckner", email = "inform@tiker.net" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Other Audience",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Scientific/Engineering :: Visualization",
"Topic :: Software Development :: Libraries",
"Topic :: Utilities",
]
dependencies = [
"platformdirs>=2.2.0",
"typing_extensions>=4.0; python_version<'3.11'",
]

[project.optional-dependencies]
numpy = [
"numpy>=1.6.0",
]

test = [
"mypy",
"pytest",
"ruff",
]

[project.urls]
Homepage = "https://github.com/inducer/pytools/"
Documentation = "https://documen.tician.de/pytools/"

[tool.setuptools.package-data]
pytools = [
"py.typed",
]

[tool.ruff]
target-version = "py38"
line-length = 85

preview = true
[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"C", # flake8-comprehensions
"E", # pycodestyle
"F", # pyflakes
"I", # flake8-isort
"N", # pep8-naming
"NPY", # numpy
"Q", # flake8-quotes
"W", # pycodestyle
]
extend-ignore = [
"C90", # McCabe complexity
"E221", # multiple spaces before operator
"E226", # missing whitespace around arithmetic operator
"E402", # module-level import not at top of file
]
[tool.ruff.lint.flake8-quotes]
docstring-quotes = "double"
inline-quotes = "double"
multiline-quotes = "double"

[tool.ruff.lint.isort]
combine-as-imports = true

known-local-folder = [
"pytools",
]
lines-after-imports = 2

[tool.mypy]
ignore_missing_imports = true
warn_unused_ignores = true

50 changes: 34 additions & 16 deletions pytools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,24 @@
from functools import reduce, wraps
from sys import intern
from typing import (
Any, Callable, ClassVar, Dict, Generic, Hashable, Iterable, Iterator, List,
Mapping, Optional, Sequence, Set, Tuple, Type, TypeVar, Union)
Any,
Callable,
ClassVar,
Dict,
Generic,
Hashable,
Iterable,
Iterator,
List,
Mapping,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)


try:
Expand Down Expand Up @@ -528,7 +544,8 @@ def __init__(self, value):

def get(self):
from warnings import warn
warn("Reference.get() is deprecated -- use ref.value instead")
warn("Reference.get() is deprecated -- use ref.value instead. "
"This will stop working in 2025.", stacklevel=2)
return self.value

def set(self, value):
Expand Down Expand Up @@ -607,7 +624,7 @@ def one(iterable: Iterable[T]) -> T:
try:
v = next(it)
except StopIteration:
raise ValueError("empty iterable passed to 'one()'")
raise ValueError("empty iterable passed to 'one()'") from None

def no_more():
try:
Expand All @@ -629,7 +646,7 @@ def is_single_valued(
try:
first_item = next(it)
except StopIteration:
raise ValueError("empty iterable passed to 'single_valued()'")
raise ValueError("empty iterable passed to 'single_valued()'") from None

for other_item in it:
if not equality_pred(other_item, first_item):
Expand All @@ -656,7 +673,7 @@ def single_valued(
try:
first_item = next(it)
except StopIteration:
raise ValueError("empty iterable passed to 'single_valued()'")
raise ValueError("empty iterable passed to 'single_valued()'") from None

def others_same():
for other_item in it:
Expand Down Expand Up @@ -1241,7 +1258,7 @@ def argmin2(iterable, return_value=False):
try:
current_argmin, current_min = next(it)
except StopIteration:
raise ValueError("argmin of empty iterable")
raise ValueError("argmin of empty iterable") from None

for arg, item in it:
if item < current_min:
Expand All @@ -1259,7 +1276,7 @@ def argmax2(iterable, return_value=False):
try:
current_argmax, current_max = next(it)
except StopIteration:
raise ValueError("argmax of empty iterable")
raise ValueError("argmax of empty iterable") from None

for arg, item in it:
if item > current_max:
Expand Down Expand Up @@ -1326,7 +1343,7 @@ def average(iterable):
s = next(it)
count = 1
except StopIteration:
raise ValueError("empty average")
raise ValueError("empty average") from None

for value in it:
s = s + value
Expand Down Expand Up @@ -1441,7 +1458,7 @@ def generate_decreasing_nonnegative_tuples_summing_to(
yield ()
elif length == 1:
if n <= max_value:
#print "MX", n, max_value
# print "MX", n, max_value
yield (n,)
else:
return
Expand All @@ -1450,7 +1467,7 @@ def generate_decreasing_nonnegative_tuples_summing_to(
max_value = n

for i in range(min_value, max_value+1):
#print "SIG", sig, i
# print "SIG", sig, i
for remainder in generate_decreasing_nonnegative_tuples_summing_to(
n-i, length-1, min_value, i):
yield (i,) + remainder
Expand Down Expand Up @@ -1502,7 +1519,7 @@ def generate_permutations(original):
else:
for perm_ in generate_permutations(original[1:]):
for i in range(len(perm_)+1):
#nb str[0:1] works in both string and list contexts
# nb str[0:1] works in both string and list contexts
yield perm_[:i] + original[0:1] + perm_[i:]


Expand All @@ -1527,7 +1544,7 @@ def enumerate_basic_directions(dimensions):

# {{{ graph algorithms

from pytools.graph import a_star as a_star_moved
from pytools.graph import a_star as a_star_moved # noqa: E402


a_star = MovedFunctionDeprecationWrapper(a_star_moved)
Expand Down Expand Up @@ -1808,7 +1825,7 @@ def string_histogram( # pylint: disable=too-many-arguments,too-many-locals
for value in iterable:
if max_value is not None and value > max_value or value < bin_starts[0]:
from warnings import warn
warn("string_histogram: out-of-bounds value ignored")
warn("string_histogram: out-of-bounds value ignored", stacklevel=2)
else:
bin_nr = bisect(bin_starts, value)-1
try:
Expand Down Expand Up @@ -2425,7 +2442,7 @@ def find_git_revision(tree_root): # pylint: disable=too-many-locals
assert retcode is not None
if retcode != 0:
from warnings import warn
warn("unable to find git revision")
warn("unable to find git revision", stacklevel=1)
return None

return git_rev
Expand Down Expand Up @@ -2704,7 +2721,8 @@ def natsorted(iterable, key=None, reverse=False):
.. versionadded:: 2020.1
"""
if key is None:
key = lambda x: x
def key(x):
return x
return sorted(iterable, key=lambda y: natorder(key(y)), reverse=reverse)

# }}}
Expand Down
4 changes: 2 additions & 2 deletions pytools/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def open_unique_debug_file(stem, extension=""):

# {{{ refcount debugging ------------------------------------------------------

class RefDebugQuit(Exception):
class RefDebugQuit(Exception): # noqa: N818
pass


Expand Down Expand Up @@ -159,7 +159,7 @@ def setup_readline():
e = sys.exc_info()[1]

from warnings import warn
warn(f"Error opening readline history file: {e}")
warn(f"Error opening readline history file: {e}", stacklevel=2)

readline.parse_and_bind("tab: complete")

Expand Down
24 changes: 19 additions & 5 deletions pytools/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,20 @@
"""

from typing import (
Any, Callable, Collection, Dict, Hashable, Iterator, List, Mapping, MutableSet,
Optional, Set, Tuple, TypeVar)
Any,
Callable,
Collection,
Dict,
Hashable,
Iterator,
List,
Mapping,
MutableSet,
Optional,
Set,
Tuple,
TypeVar,
)


try:
Expand Down Expand Up @@ -287,7 +299,7 @@ def compute_topological_order(graph: GraphT[NodeT],

# {{{ compute nodes_to_num_predecessors

nodes_to_num_predecessors = {node: 0 for node in graph}
nodes_to_num_predecessors = dict.fromkeys(graph, 0)

for node in graph:
for child in graph[node]:
Expand Down Expand Up @@ -437,10 +449,12 @@ def as_graphviz_dot(graph: GraphT[NodeT],
from pytools.graphviz import dot_escape

if node_labels is None:
node_labels = lambda x: str(x)
def node_labels(x):
return str(x)

if edge_labels is None:
edge_labels = lambda x, y: ""
def edge_labels(x, y):
return ""

node_to_id = {}

Expand Down
Loading
Loading