Skip to content

Commit

Permalink
fix: allow detect_vyper_version_from_source to return None (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 20, 2024
1 parent ad38afe commit 2526c3e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 1 addition & 3 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def foo() -> int128:


def test_no_version_in_source():
with pytest.raises(UnexpectedVersionError) as excinfo:
detect_vyper_version_from_source("def foo() -> int128: return 42")
assert str(excinfo.value) == "No version detected in source code"
assert detect_vyper_version_from_source("def foo() -> int128: return 42") is None


def test_version_does_not_exist():
Expand Down
10 changes: 6 additions & 4 deletions vvm/utils/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
_VERSION_RE = re.compile(r"\s*#\s*(?:pragma\s+|@)version\s+([=><^~]*)(\d+\.\d+\.\d+\S*)")


def _detect_version_specifier(source_code: str) -> Specifier:
def _detect_version_specifier(source_code: str) -> Optional[Specifier]:
"""
Detect the version given by the pragma version in the source code.
Expand All @@ -27,7 +27,7 @@ def _detect_version_specifier(source_code: str) -> Specifier:
"""
match = _VERSION_RE.search(source_code)
if match is None:
raise UnexpectedVersionError("No version detected in source code")
return None

specifier, version_str = match.groups()
if specifier in ("~", "^"): # convert from npm-style to pypi-style
Expand Down Expand Up @@ -82,7 +82,7 @@ def _pick_vyper_version(
return ret


def detect_vyper_version_from_source(source_code: str, **kwargs: Any) -> Version:
def detect_vyper_version_from_source(source_code: str, **kwargs: Any) -> Optional[Version]:
"""
Detect the version given by the pragma version in the source code.
Expand All @@ -95,8 +95,10 @@ def detect_vyper_version_from_source(source_code: str, **kwargs: Any) -> Version
Returns
-------
Version
Optional[Version]
vyper version, or None if no version could be detected.
"""
specifier = _detect_version_specifier(source_code)
if specifier is None:
return None
return _pick_vyper_version(specifier, **kwargs)

0 comments on commit 2526c3e

Please sign in to comment.