Skip to content

Commit 06d2360

Browse files
committed
Delay imports of non-stdlib dependencies until time of use
This is a bit of a hack, but we do this to ensure that when setuptools loads entrypoint based hooks it cannot (will not?) crash. The issue is that setuptools plugins are autoloaded, whether any given project uses them at all or not. So if setuptools-rust is installed, setuptools always tries to use it, and crashes if setuptools-rust is broken. Of course, setuptools-rust can't be broken, because it's a wonderful project. BUT. As it happens, third-party vendors providing setuptools-rust can get into a situation where multiple packages need to be installed, including setuptools-rust, and also build yet other packages from source. In the middle of this, setuptools-rust itself could be installed but in "half-configured" state, i.e. its dependencies were queued for afterwards due to complicated dependency graph magic. In such a scenario, it should be nominally all right to have an inert package installed, since if nothing actually uses setuptools-rust it doesn't need to *work* yet. And in fact, it is all right, as long as setuptools can import the autoloaded plugin hooks (and do nothing with them). Bug: https://bugs.gentoo.org/933553
1 parent d7868df commit 06d2360

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

setuptools_rust/extension.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35
import re
@@ -14,11 +16,13 @@
1416
NewType,
1517
Optional,
1618
Sequence,
19+
TYPE_CHECKING,
1720
Union,
1821
cast,
1922
)
2023

21-
from semantic_version import SimpleSpec
24+
if TYPE_CHECKING:
25+
from semantic_version import SimpleSpec
2226

2327
from ._utils import format_called_process_error
2428

@@ -185,6 +189,7 @@ def get_rust_version(self) -> Optional[SimpleSpec]: # type: ignore[no-any-unimp
185189
if self.rust_version is None:
186190
return None
187191
try:
192+
from semantic_version import SimpleSpec
188193
return SimpleSpec(self.rust_version)
189194
except ValueError:
190195
raise SetupError(

setuptools_rust/rustc_info.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from __future__ import annotations
2+
13
import subprocess
24
from setuptools.errors import PlatformError
35
from functools import lru_cache
4-
from typing import Dict, List, NewType, Optional
6+
from typing import Dict, List, NewType, Optional, TYPE_CHECKING
57

6-
from semantic_version import Version
8+
if TYPE_CHECKING:
9+
from semantic_version import Version
710

811

912
def get_rust_version() -> Optional[Version]: # type: ignore[no-any-unimported]
1013
try:
1114
# first line of rustc -Vv is something like
1215
# rustc 1.61.0 (fe5b13d68 2022-05-18)
16+
from semantic_version import Version
1317
return Version(_rust_version().split(" ")[1])
1418
except (subprocess.CalledProcessError, OSError):
1519
return None

0 commit comments

Comments
 (0)