Skip to content
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ lint.select = [
lint.ignore = [ "PLR2004", "S101", "SIM108" ]
lint.per-file-ignores."!tests/**/*.py" = [ "ARG002", "PT" ]
lint.per-file-ignores."src/usethis/_ui/interface/**/*.py" = [ "PLR0913" ]
lint.per-file-ignores."tests/**/*.py" = [ "D", "INP", "S603", "TC" ]

lint.per-file-ignores."tests/**" = [ "D", "INP", "S603", "TC" ]
lint.flake8-bugbear.extend-immutable-calls = [ "typer.Argument", "typer.Option" ]
lint.flake8-builtins.strict-checking = true
lint.flake8-tidy-imports.banned-api."typer.testing.CliRunner".msg = "Use `usethis._test.CliRunner` instead of `typer.CliRunner`."
Expand Down
2 changes: 2 additions & 0 deletions src/usethis/_integrations/project/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def get_layered_architectures(pkg_name: str) -> dict[str, LayeredArchitecture]:

This is intended to inform the basis of a layer contract.

Note that the resulting dictionary may be empty if the package seems to be empty.

Reference:
https://import-linter.readthedocs.io/en/stable/contract_types.html#layers

Expand Down
7 changes: 7 additions & 0 deletions src/usethis/_tool/impl/import_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ def get_root_packages() -> list[str] | NoConfigValue:
keys=["tool", "importlinter", "contracts"],
get_value=lambda: contracts,
),
# N.B. these INI sections are added via
# `ini_contracts_config_items`
# but there might be others so we still need to declare they
# are associated with this tool based on regex.
Path(".importlinter"): ConfigEntry(
keys=[re.compile("importlinter:contract:.*")]
),
Expand Down Expand Up @@ -271,6 +275,9 @@ def _get_layered_architecture_by_module_by_root_package(
try:
layered_architecture_by_module = get_layered_architectures(root_package)
except ImportGraphBuildFailedError:
layered_architecture_by_module = {}

if not layered_architecture_by_module:
layered_architecture_by_module = {
root_package: LayeredArchitecture(layers=[], excluded=set())
}
Expand Down
18 changes: 18 additions & 0 deletions tests/usethis/_tool/impl/test_import_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ def test_pyproject_toml_exists(self, tmp_path: Path):
assert not (tmp_path / ".importlinter").exists()
assert (tmp_path / "pyproject.toml").exists()

class TestGetConfigSpec:
def test_empty_src_directory(self, tmp_path: Path):
# Arrange: Create empty src directory with package subdirectory
# src/ contains mypkg/ but mypkg/ is completely empty (no __init__.py)
(tmp_path / "pyproject.toml").write_text('[project]\nname = "mypkg"')
(tmp_path / "src").mkdir()
(tmp_path / "src" / "mypkg").mkdir()
# mypkg directory is empty - no __init__.py, no files

# Act: get_config_spec should not crash with AssertionError
# when get_importable_packages returns empty and grimp fails
with change_cwd(tmp_path), files_manager():
config_spec = ImportLinterTool().get_config_spec()

# Assert: Should return a valid config spec with fallback contract
assert config_spec is not None
assert len(config_spec.config_items) > 0


class TestIsINPRule:
def test_inp_rule(self):
Expand Down