Skip to content

Commit

Permalink
by version
Browse files Browse the repository at this point in the history
  • Loading branch information
kavigupta committed Feb 13, 2024
1 parent 96ac5dd commit aa722dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tests/assignment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ImportAssignmentTests(DisplayAnnotatedTestCase):
def test_global_import(self):
self.assertAnnotationWorks(
"""
import os
import {>=3.10!g}os
{g}def f():
{g}os
"""
Expand All @@ -84,20 +84,21 @@ def test_local_import(self):
self.assertAnnotationWorks(
"""
{g}def f():
import os
import {>=3.10!~f@1:0}os
{~f@1:0}os
"""
)
def test_sub_import(self):
self.assertAnnotationWorks(
"""
{g}def f():
from os import system
from os import {>=3.10!~f@1:0}system
{~f@1:0}system
{g}os
"""
)
def test_star_import(self):
# note: no annotation in any version
self.assertAnnotationWorks(
"""
from os import *
Expand All @@ -107,8 +108,8 @@ def test_aliases(self):
self.assertAnnotationWorks(
"""
{g}def f():
from os import system as a
import sys as b
from os import {>=3.10!~f@1:0}system as a
import {>=3.10!~f@1:0}sys as b
{g}os
{g}system
{~f@1:0}a
Expand Down
8 changes: 8 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def _check_nodes(self, mapping, *scopes):
self.assertCountEqual([node for _, node in overall_scope], list(mapping))

def assertAnnotationWorks(self, annotated_code, code=None, *, class_binds_near=False):
# directives of the form {>version!scope} are removed unless the version is satisfied
regex = r"\{>=(\d+\.\d+)!([^\}]+)\}"
def replacer(match):
version, scope = match.groups()
if sys.version_info >= tuple(map(int, version.split("."))):
return "{" + scope + "}"
return ""
annotated_code = re.sub(regex, replacer, annotated_code)
if code is None:
code = trim(re.sub(r"\{[^\}]+\}", "", annotated_code))

Expand Down

0 comments on commit aa722dc

Please sign in to comment.