Skip to content

Commit

Permalink
fix: make version pragma check more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Oct 25, 2023
1 parent 8df6803 commit af57d27
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,25 @@ def get_version_pragma_spec(source: Union[str, Path]) -> Optional[SpecifierSet]:
Returns:
``packaging.specifiers.SpecifierSet``, or None if no valid pragma is found.
"""
source_str = source if isinstance(source, str) else source.read_text()
pragma_match = next(re.finditer(r"(?:\n|^)\s*#\s*@version\s*([^\n]*)", source_str), None)
if pragma_match is None:
# support new pragma syntax
pragma_match = next(
re.finditer(r"(?:\n|^)\s*#\s*pragma\s+version\s*([^\n]*)", source_str), None
)
if pragma_match is None:
return None # Try compiling with latest
version_pragma_patterns = [
r"(?:\n|^)\s*#\s*@version\s*([^\n]*)",
r"(?:\n|^)\s*#\s*pragma\s+version\s*([^\n]*)",
]

raw_pragma = pragma_match.groups()[0]
pragma_str = " ".join(raw_pragma.split()).replace("^", "~=")
if pragma_str and pragma_str[0].isnumeric():
pragma_str = f"=={pragma_str}"
source_str = source if isinstance(source, str) else source.read_text()
for pattern in version_pragma_patterns:
for match in re.finditer(pattern, source_str):
raw_pragma = match.groups()[0]
pragma_str = " ".join(raw_pragma.split()).replace("^", "~=")
if pragma_str and pragma_str[0].isnumeric():
pragma_str = f"=={pragma_str}"

try:
return SpecifierSet(pragma_str)
except InvalidSpecifier:
logger.warning(f"Invalid pragma spec: '{raw_pragma}'. Trying latest.")
return None
try:
return SpecifierSet(pragma_str)
except InvalidSpecifier:
logger.warning(f"Invalid pragma spec: '{raw_pragma}'. Trying latest.")
return None
return None


def get_optimization_pragma(source: Union[str, Path]) -> Optional[str]:
Expand Down

0 comments on commit af57d27

Please sign in to comment.