Skip to content

Commit

Permalink
fix: use new meth
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 18, 2023
1 parent ff0e6dc commit ab41734
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
40 changes: 3 additions & 37 deletions ape_solidity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict, List, Optional, Sequence, Set, Union

from ape.exceptions import CompilerError
from ape.logging import logger
from ape.utils import pragma_str_to_specifier_set
from packaging.specifiers import SpecifierSet
from packaging.version import InvalidVersion
from packaging.version import Version
Expand Down Expand Up @@ -148,7 +148,7 @@ def get_pragma_spec_from_path(source_file_path: Union[Path, str]) -> Optional[Sp
source_file_path (Union[Path, str]): Solidity source file path.
Returns:
``packaging.specifiers.SpecifierSet``
``Optional[packaging.specifiers.SpecifierSet]``
"""
path = Path(source_file_path)
if not path.is_file():
Expand All @@ -166,41 +166,7 @@ def get_pragma_spec_from_str(source_str: str) -> Optional[SpecifierSet]:
):
return None # Try compiling with latest

# The following logic handles the case where the user puts a space
# between the operator and the version number in the pragma string,
# such as `solidity >= 0.4.19 < 0.7.0`.
pragma_parts = pragma_match.groups()[0].split()

def _to_spec(item: str) -> str:
item = item.replace("^", "~=")
if item and item[0].isnumeric():
return f"=={item}"
elif item and len(item) >= 2 and item[0] == "=" and item[1] != "=":
return f"={item}"

return item

pragma_parts_fixed = []
builder = ""
for sub_part in pragma_parts:
if not any(c.isnumeric() for c in sub_part):
# Handle pragma with spaces between constraint and values
# like `>= 0.6.0`.
builder += sub_part
continue
elif builder:
spec = _to_spec(f"{builder}{sub_part}")
builder = ""
else:
spec = _to_spec(sub_part)

pragma_parts_fixed.append(spec)

try:
return SpecifierSet(",".join(pragma_parts_fixed))
except ValueError as err:
logger.error(str(err))
return None
return pragma_str_to_specifier_set(pragma_match.groups()[0])


def load_dict(data: Union[str, dict]) -> Dict:
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/SpacesInPragma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file exists to test a bug that occurred when the user has a pragma
// like this one. It was failing to register as a proper NpmSpec because
// of the spaces between the operator and the version.
pragma solidity >= 0.4.19 < 0.5.0;
pragma solidity >= 0.4.19 < 0.5.0;

interface SpacesInPragma {
// This syntax fails on version >= 5.0, thus we are testing
Expand Down

0 comments on commit ab41734

Please sign in to comment.