Skip to content

Commit

Permalink
Turn on strict static analysis checking
Browse files Browse the repository at this point in the history
  • Loading branch information
SethMMorton committed Oct 22, 2024
1 parent 837cd53 commit eb854fe
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 257 deletions.
51 changes: 29 additions & 22 deletions dev/bump.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#! /usr/bin/env python

"""
Bump version of fastnumbers.
Cross-platform bump of version with special CHANGELOG modification.
INTENDED TO BE CALLED FROM PROJECT ROOT, NOT FROM dev/!
"""

from __future__ import annotations

import datetime
import pathlib
import subprocess
import sys

Expand All @@ -30,8 +37,8 @@
sys.exit('bump_type must be one of "major", "minor", or "patch"!')


def git(cmd, *args):
"""Wrapper for calling git"""
def git(cmd: str, *args: str) -> None:
"""Call git."""
try:
subprocess.run(["git", cmd, *args], check=True, text=True)
except subprocess.CalledProcessError as e:
Expand All @@ -58,26 +65,26 @@ def git(cmd, *args):
next_version = ".".join(map(str, version_components))

# Update the changelog.
with open("CHANGELOG.md") as fl:
changelog = fl.read()

# Add a date to this entry.
changelog = changelog.replace(
"Unreleased",
f"Unreleased\n---\n\n[{next_version}] - {datetime.datetime.now():%Y-%m-%d}",
)

# Add links to the entries.
changelog = changelog.replace(
"<!---Comparison links-->",
"<!---Comparison links-->\n[{new}]: {url}/{current}...{new}".format(
new=next_version,
current=current_version,
url="https://github.com/SethMMorton/fastnumbers/compare",
),
)
with open("CHANGELOG.md", "w") as fl:
fl.write(changelog)
changelog = pathlib.Path("CHANGELOG.md").read_text()

# Add a date to this entry.
changelog = changelog.replace(
"Unreleased",
f"Unreleased\n---\n\n[{next_version}] - {datetime.datetime.now():%Y-%m-%d}",
)

# Add links to the entries.
changelog = changelog.replace(
"<!---Comparison links-->",
"<!---Comparison links-->\n[{new}]: {url}/{current}...{new}".format(
new=next_version,
current=current_version,
url="https://github.com/SethMMorton/fastnumbers/compare",
),
)

# Write the changelog
pathlib.Path("CHANGELOG.md").write_text(changelog)

# Add the CHANGELOG.md changes and commit & tag.
git("add", "CHANGELOG.md")
Expand Down
3 changes: 3 additions & 0 deletions dev/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

"""
Cross-platform clean of working directory.
INTENDED TO BE CALLED FROM PROJECT ROOT, NOT FROM dev/!
"""

from __future__ import annotations

import pathlib
import shutil

Expand Down
23 changes: 12 additions & 11 deletions dev/formatting.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#! /usr/bin/env python3
"""
Cross-platform checking if code is appropriately formatted.
INTENDED TO BE CALLED FROM PROJECT ROOT, NOT FROM dev/!
"""

import glob
import shlex
from __future__ import annotations

import pathlib
import subprocess
import sys

Expand All @@ -14,28 +17,26 @@
ruff = ["ruff", "format"]
if check:
ruff.extend(["--check", "--diff"])
print(*map(shlex.quote, ruff))
ruff_ret = subprocess.run(ruff)
ruff_ret = subprocess.run(ruff, check=False, text=True)

# Check that C++ code is formatted
clang_format = [
clang_format: list[pathlib.Path | str] = [
"clang-format",
"--style=file:dev/clang-format.cfg",
"--dry-run" if check else "-i",
]
cpp = glob.glob("src/cpp/*.cpp")
hpp = glob.glob("include/fastnumbers/*.hpp")
hpp += glob.glob("include/fastnumbers/parser/*.hpp")
cpp = list(pathlib.Path("src/cpp").glob("*.cpp"))
hpp = list(pathlib.Path("include/fastnumbers").glob("*.hpp"))
hpp.extend(pathlib.Path("include/fastnumbers/parser").glob("*.hpp"))

clang_format = clang_format + cpp + hpp
print(*map(shlex.quote, clang_format))
clang_format += cpp + hpp
clang_format_ret = subprocess.run(
clang_format,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=False,
)
print(clang_format_ret.stdout)

# Stop here if not checking
if not check:
Expand Down
17 changes: 12 additions & 5 deletions dev/patch-doctest.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#! /usr/bin/env python3
"""
Patch the doctest module.
Copies doctest.py from the stdlib to the current directory,
and modifies it so that
and modifies it so that.
a) It will load "*.so" files as modules just like a "*.py" file
b) It recognizes functions defined in "*.so" files
Expand All @@ -9,15 +12,20 @@
With these enhancements, doctest can be run on a C python extension module.
"""

from __future__ import annotations

import doctest
import inspect
import pathlib
import sys

# Get the source file location
dt_location = inspect.getsourcefile(doctest)
if dt_location is None:
sys.exit("Could not locate doctest module location.")

# Read the module into memory
with open(dt_location) as fl:
doctest_str = fl.read()
doctest_str = pathlib.Path(dt_location).read_text()

# Let's add the glob module.
# Also define a function to detect if we could import this module name.
Expand Down Expand Up @@ -64,5 +72,4 @@
doctest_str = "# type: ignore\n" + doctest_str

# Open up the new output file and write the modified input to it.
with open("doctest.py", "w") as fl:
print(doctest_str, file=fl, end="")
pathlib.Path("doctest.py").write_text(doctest_str)
24 changes: 12 additions & 12 deletions dev/test-runner.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#! /usr/bin/env python3
"""
This script will run tests using gdb as a means to catch segfaults.
Run tests using gdb as a means to catch segfaults.
If gdb is not installed, it just runs the tests.
"""

from __future__ import annotations

import os
import sys

Expand All @@ -12,25 +15,22 @@
try:
# Don't use gdb unless requesting debugging mode
if "FN_DEBUG" not in os.environ:
raise OSError
raise OSError # noqa: TRY301

# Attempt to run pytest with debugger
# fmt: off
os.execlp(
"gdb",
"gdb",
"-ex",
"run",
"-ex",
"bt",
"-ex",
"quit",
"--args",
my_python,
"-m",
"pytest",
"-ex", "run",
"-ex", "bt",
"-ex", "quit",
"--args", my_python,
"-m", "pytest",
"--doctest-glob=README.rst",
*other_args,
)
# fmt: on
except OSError:
# No debugger installed, just run pytest directly
os.execl(
Expand Down
8 changes: 8 additions & 0 deletions mypy_stubs/setuptools_scm.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

def get_version(
root: os.PathLike[str] | str = ".",
version_scheme: str = ...,
local_scheme: str = ...,
relative_to: os.PathLike[str] | str | None = ...,
) -> str: ...
Loading

0 comments on commit eb854fe

Please sign in to comment.