Skip to content

Commit c2d6ebe

Browse files
committed
Handle the case where the is no resolver
Some python packages, e.g. pytz, are purely written in python and do not have any other dependencies. In those cases, a `resolver` is not set on the compiler, causing `AttributeError` exceptions in various places. This commit performs a `None` check on the `resolver` attribute before proceeding to access nested attributes. Fixes: #304 Signed-off-by: Luiz Carvalho <[email protected]>
1 parent aecf883 commit c2d6ebe

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/pybuild_deps/scripts/compile.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def compile(
112112
compiler = BuildDependencyCompiler(repository)
113113
try:
114114
results = compiler.resolve(dependencies)
115-
hashes = compiler.resolver.resolve_hashes(results) if generate_hashes else None
115+
hashes = None
116+
if compiler.resolver is not None and generate_hashes:
117+
hashes = compiler.resolver.resolve_hashes(results)
116118
except (PipToolsError, PyBuildDepsError) as e:
117119
log.error(str(e))
118120
sys.exit(2)
@@ -142,10 +144,15 @@ def compile(
142144
emit_find_links=True,
143145
emit_options=True,
144146
)
147+
unsafe_packages = None
148+
unsafe_requirements = None
149+
if compiler.resolver is not None:
150+
unsafe_packages = compiler.resolver.unsafe_packages
151+
unsafe_requirements = compiler.resolver.unsafe_constraints
145152
writer.write(
146153
results=results,
147-
unsafe_packages=compiler.resolver.unsafe_packages,
148-
unsafe_requirements=compiler.resolver.unsafe_constraints,
154+
unsafe_packages=unsafe_packages,
155+
unsafe_requirements=unsafe_requirements,
149156
markers={
150157
key_from_ireq(ireq): ireq.markers for ireq in dependencies if ireq.markers
151158
},

tests/test_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,13 @@ def test_compile_consistent_ordering(runner: CliRunner, tmp_path: Path):
255255
)
256256
assert result2.exit_code == 0
257257
assert outfile1.read_text() == outfile2.read_text()
258+
259+
260+
def test_compile_package_without_dependencies(runner: CliRunner, tmp_path: Path):
261+
"""Test compile function with a package that has no dependencies."""
262+
chdir(tmp_path)
263+
requirements_path: Path = tmp_path / "requirements.txt"
264+
requirements_path.write_text("pytz==2024.1")
265+
outfile = tmp_path / "outfile"
266+
result = runner.invoke(main.cli, args=["compile", "-o", str(outfile)])
267+
assert result.exit_code == 0

0 commit comments

Comments
 (0)