A test that redirects production includes with jw.include-map can silently
read a real production path when the map misses a spec, making the test
depend on the host's /etc without any warning.
Add a jw.include-map-no-absolute key next to the map. When true, an
absolute include spec that matched no map entry raises ValueError, naming
the spec and the including file; an identity entry whitelists an absolute
path that may be read. The key defaults to false and applies to the whole
include tree: _load_yaml() pops it in the top-level document, rejects it
in included documents, and threads it to _parse_include_spec(), where the
check runs before the normal disk lookup.
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
This commit adds support for interpreting a new syntax:
jw.include-map:
- /etc/some/file.conf: ../../../file.conf
- /opt/blah/another/config.yaml: ../../../config.yaml
Production configs include absolute paths outside the repository tree, so a
test that runs a production-style document against repository-local copies
must redirect those includes, ideally without modifying the
files meant to be installed as is.
Add a jw.include-map key to the top-level document: an ordered list
of single-key mappings from LHS pattern to RHS pattern or list of RHS
patterns. Before resolution, the file part of each include spec is
qualified against the including file's directory and matched against the
LHS patterns in table order; the first match rewrites the spec to every RHS
pattern of that entry. Relative LHS and RHS values are interpreted against
the directory of the document that declares the map, so matching runs on
canonical paths and same-named includes from different directories stay
distinguishable. The optional marker and the target path are decomposed
before the rewrite: every rewritten source inherits the spec's optionality,
and the target path applies to the merged result.
- _parse_include_map(): parse and validate the table; only '*' is supported
in an LHS, a RHS may not have more '*' than its LHS, and RHS values
expand $(VAR) and ${VAR} from the environment
- _canonicalize_path(): join a relative path with a base directory and
normalize, without disk access
- _compile_lhs_pattern(): compile a canonical LHS into an anchored regex
whose '*' groups match runs that are not '/'
- _apply_include_map(): match the first entry and fill the LHS captures
into every RHS pattern; log the rewrite at DEBUG
- _load_yaml(): parse the map once in the top-level document, reject it in
included documents, thread it through the include tree
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
Test wrappers that mimic production invocations redirect the includes of
production documents to tree-local copies; the target location depends on
where the tree is checked out, so it would be nice if the wrapper could
take TOPDIR from the environment instead of hardcoding it.
Allow the file part of a top-level include spec to reference environment
variables as $(VAR) or ${VAR}. _load_yaml() receives a depth counter,
zero at the top level and one per include level, and threads it to
_normalize_include_specs() and _parse_include_spec(); the file part passes
through _expand_include_vars() only at depth zero, after the optional
marker and the target path have been split off. Specs in included documents
keep their literal behaviour. An undefined variable raises ValueError,
naming the reference and the file it came from.
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
kebab_to_pascal() and pascal_to_kebab() are needed in jw-pkg, for the
resolution of filter names and, later, the dynamic loading of file
contexts. jw-python depends on jw-pkg, so jw-pkg's util.py is their
canonical home.
This commit imports them from jw.pkg.lib.util and re-exports them, so the
existing importers keep working.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2
Signed-off-by: Jan Lindemann <jan@janware.com>
Merge pull request "Sync the tree with current mypy and isort"
Upstream-repo: ssh://git@code.janware.com/janware/jw-python
Source-branch: jan/fix/20261113-sync-the-tree-with-current-mypy-and-isort
Target-branch: master
Reviewed-on: #4
The current isort formats parenthesized import lists with a trailing comma
and wraps long imports differently than when the files were last formatted,
so make format rewrites them on every run even though nothing is wrong with
the code.
Apply make format to the two drifted import statements so the tree matches
the formatter's output and make format becomes a no-op:
- src/python/jw/util/log.py: trailing comma in the jw.pkg.lib.log import
list
- src/python/jwutils/db/schema/CompositeForeignKey.py: wrap the
CompositeForeignKey import in parentheses
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2
Signed-off-by: Jan Lindemann <jan@janware.com>
mypy 2.4.0 infers the attribute from the None assignment in __init__() and
then flags the right operand of the and check in __parse__() as unreachable
code, failing make check.
The attribute is only ever assigned None, so annotate it as Any and keep
the runtime behaviour unchanged.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2
Signed-off-by: Jan Lindemann <jan@janware.com>
The parse_opts test is a plain script that constructs an Options from
sys.argv and crashed on the first get() call, because the by_index
condition in Options.get() is inverted and raises KeyError for an integer
index. It has several shortcomings, though:
- "make test" doesn't execute it, only make -C test/parse_opts run, so CI
doesn't run it
- It doesn't adhere to standard janware directory layout
- It's very basic
Replace the script with a pytest module under
test/unit/python/jw/util/Options, following the layout of the sibling
unit test directories: a Makefile including py-test.mk and a test.py
discovered by pytest. The module covers parsing of option strings, repeated
keys, JSON values, flags, get() with defaults and the by_index parameter,
__contains__, __iter__, keys(), str() and update().
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
The by_index branch of Options.get() raises KeyError for every integer
index, because the first isinstance guard is inverted: it fires for exactly
the keys the branch is supposed to handle, so the range check and the
lookup in the list of parsed keys below it are dead code. A non-integer key
with by_index set slips past the guard and falls through to the regular key
lookup instead. A previous make-check fallout commit flipped the original
type(key) != int test into a positive isinstance test, and a later mypy fix
then added redundant isinstance guards to the unreachable branches,
cementing the broken flow.
Invert the guard back to not isinstance(key, int), so that a non-integer
key raises KeyError, an in-range integer index returns the key stored at
that position in the option string, and an out-of-range index returns the
default or raises KeyError. Drop the redundant isinstance guards from the
now-reachable branches.
TODO: The bug actually made "make -C test/parse_opts run" fail, but since
jw-python's nightly CI doesn't run that, this has gone unnoticed. The test
now passes, but should be replaced with a better test (executed by "make
test", and pytest-based).
Signed-off-by: Jan Lindemann <jan@janware.com>
The test tree needs to be aligned with type checking, too, because jw-pkg
is going to required it for test, too. The load_yaml and StringTree tests
receive their fixtures as bare Callable and Generator without type
arguments, and the StringTree tests pass the st fixture without an
annotation. The cast script suppresses errors with un-coded type ignore
comments, and the parse_opts script declares its loop variable as
"str | int", so indexing the Options object with it is rejected.
Introduce _YamlFile, _TempDir, and _St aliases for the fixture types and
give the st parameters an annotation. Code the type ignore comments with
the argument mismatch they suppress, and split the parse_opts loop variable
into i for the index loops and k for the key loops.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit fixes tightening type check fallout.
add_subcommands() with a string pattern collects every class of the calling
module whose name matches the pattern. inspect.getmembers() yields plain
objects, so the unannotated collection list is inferred as list[object],
which is not compatible with the declared list[type[Cmd]] parameter type,
and make check fails on it.
Annotate the collection list as list[type[Cmd]] and narrow it to subclasses
of Cmd via issubclass(). This makes the implementation match the declared
signature and stops classes lacking the Cmd interface from being registered
as subcommands, where they would break later when instantiated and treated
as Cmd in __add_cmd_to_parser().
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
Add a store_dict() function to write the contents of a mapping to
a file as JSON, using the same formatting as dump_dict().
Signed-off-by: Jan Lindemann <jan@janware.com>
For a reviewable diff, .gitignore is updated in two commits. The first sorts,
the second is the result of "make gitignore".
This commit is step 2.
Signed-off-by: Jan Lindemann <jan@janware.com>
For a reviewable diff, .gitignore is updated in two commits. The first sorts,
the second is the result of "make gitignore".
This commit is step 1.
Signed-off-by: Jan Lindemann <jan@janware.com>
For in-tree tests, the jw package is split across several projects, and
each directory containing a py-ns-dir.mk contributes its subtree. So far,
it was up to that directory how the contribution is handled. With current
changes in jw-pkg, py-ns-dir.mk handles the contribution method centrally
by default, and the __init__.py currently under version control should be
removed. jw-python contains a custom __init__.py there, so protect it from
whatever py-ns-dir.mk is up to.
Signed-off-by: Jan Lindemann <jan@janware.com>
The optional marker is a leading dash in the include spec, but in
a YAML block list the leading dash is the list syntax and is
consumed by the YAML parser before load_yaml() sees the spec. An
include written as a plain list item "- file" is therefore
required, not optional, and a missing file raises, even though the
dash reads like the optional marker.
Note in the module docstring that in a block list the item itself
must carry the marker, as in "- -file.yaml" or the quoted form
"- "-file.yaml"".
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
An optional include with a target path that points to a missing
file leaves a trace in the output: _load_yaml() still calls
_merge_at_path() with the empty document, which creates the target
key as an empty mapping. A root-level optional include of the same
file contributes nothing, so the outcome depends on whether the
spec names a target path.
Skip included documents that are empty: an empty document
contributes nothing, at the root as well as at a target path, and
creates no key. Update the module docstring accordingly and add
tests covering a missing optional include with a target path.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>