Skip to content

Commit acc5243

Browse files
Remove detect_vyper_version_from_source
1 parent 19acbc1 commit acc5243

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

tests/test_detect_vyper_version.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from vvm import detect_vyper_version_from_source
4+
5+
6+
def test_detect_vyper_version_from_source(foo_source, all_versions):
7+
assert detect_vyper_version_from_source(foo_source) == str(all_versions)
8+
9+
10+
@pytest.mark.parametrize(
11+
"version_str,decorator",
12+
[
13+
("0.1.0b17", "public"),
14+
("0.3.0-beta17", "external"),
15+
("0.4.0rc6", "external"),
16+
],
17+
)
18+
def test_detect_vyper_version_beta(version_str, decorator):
19+
source = f"""
20+
# @version {version_str}
21+
22+
@{decorator}
23+
def foo() -> int128:
24+
return 42
25+
"""
26+
assert detect_vyper_version_from_source(source) == version_str

vvm/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
install_vyper,
66
set_vyper_version,
77
)
8-
from vvm.main import compile_files, compile_source, compile_standard, get_vyper_version
8+
from vvm.main import (
9+
compile_files,
10+
compile_source,
11+
compile_standard,
12+
detect_vyper_version_from_source,
13+
get_vyper_version,
14+
)

vvm/main.py

+27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ def get_vyper_version() -> Version:
2323
return wrapper._get_vyper_version(vyper_binary)
2424

2525

26+
def detect_vyper_version_from_source(source_code: str) -> Optional[str]:
27+
"""
28+
Detect the version given by the pragma version in the source code.
29+
TODO: when the user has a range, we should compare to the installed versions
30+
31+
Arguments
32+
---------
33+
source_code : str
34+
Source code to detect the version from.
35+
36+
Returns
37+
-------
38+
str
39+
vyper version, or None if no version could be detected.
40+
"""
41+
try:
42+
finditer = VERSION_RE.finditer(source_code)
43+
version_str = next(finditer).group(1)
44+
Version(version_str) # validate the version
45+
return version_str
46+
except (StopIteration, InvalidVersion):
47+
return None
48+
49+
2650
def compile_source(
2751
source: str,
2852
base_path: Union[Path, str] = None,
@@ -57,6 +81,9 @@ def compile_source(
5781
Dict
5882
Compiler output. The source file name is given as `<stdin>`.
5983
"""
84+
if vyper_version is None:
85+
vyper_version = detect_vyper_version_from_source(source)
86+
6087
source_path = tempfile.mkstemp(suffix=".vy", prefix="vyper-", text=True)[1]
6188
with open(source_path, "w") as fp:
6289
fp.write(source)

0 commit comments

Comments
 (0)