Skip to content

Commit d43f865

Browse files
committed
[3.15] Detect slice type subscription support
1 parent 14aa557 commit d43f865

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ generalized unpacking, ellipsis literal (``...``) out of slices, dictionary unio
155155
keyword, pattern matching with ``match``, union types written as ``X | Y``, type alias statements
156156
(``type X = SomeType``), type alias statements with lambdas/comprehensions in class scopes, generic
157157
classes (``class C[T]: ...``), template string literals (``t'{var}'``), lazy imports (``lazy
158-
import``, ``lazy from ... import``), and special module attributes like ``__lazy_modules__``. It
159-
tries to detect and ignore user-defined functions, classes, arguments, and variables with names that
160-
clash with library-defined symbols.
158+
import``, ``lazy from ... import``), special module attributes like ``__lazy_modules__``, and slice
159+
type subscription (``slice[1:2:3]``). It tries to detect and ignore user-defined functions, classes,
160+
arguments, and variables with names that clash with library-defined symbols.
161161

162162
Caveats
163163
=======

tests/lang.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,15 @@ def test_lazy_modules(self):
12061206

12071207
visitor = self.visit("__all__ = []")
12081208
self.assertFalse(visitor.lazy_modules())
1209+
1210+
def test_slice_subscription(self):
1211+
visitor = self.visit("x = slice[1:2:3]")
1212+
self.assertTrue(visitor.slice_subscription())
1213+
self.assertOnlyIn((3, 15), visitor.minimum_versions())
1214+
1215+
visitor = self.visit("x = slice(1, 2, 3)")
1216+
self.assertFalse(visitor.slice_subscription())
1217+
12091218
@VerminTest.skipUnlessVersion(3, 5)
12101219
def test_bytes_format(self):
12111220
visitor = self.visit("b'%x' % 10")

vermin/source_state.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def __init__(self, config, path=None, source=None):
138138

139139
# `__lazy_modules__` module attribute.
140140
self.lazy_modules = False
141+
142+
# `slice` type subscript support, e.g. `slice[1:2:3]`.
143+
self.slice_subscription = False
141144
# Imported members of modules, like "exc_clear" of "sys".
142145
self.import_mem_mod = {}
143146

vermin/source_visitor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ def lazy_imports(self):
285285
def lazy_modules(self):
286286
return self.__s.lazy_modules
287287

288+
def slice_subscription(self):
289+
return self.__s.slice_subscription
290+
288291
def __get_source_line(self, line, col=0):
289292
if self.__s.source is None:
290293
return None # pragma: no cover
@@ -550,6 +553,10 @@ def minimum_versions(self):
550553
mins = self.__add_versions_entity(mins, (None, (3, 15)),
551554
"`__lazy_modules__`")
552555

556+
if self.slice_subscription():
557+
mins = self.__add_versions_entity(mins, (None, (3, 15)),
558+
"slice subscription")
559+
553560
for directive in self.strftime_directives():
554561
if directive in STRFTIME_REQS:
555562
vers = STRFTIME_REQS[directive]
@@ -2291,6 +2298,13 @@ def match(name):
22912298
if is_ellipsis_node(n):
22922299
self.__s.ellipsis_nodes_in_slices.add(n)
22932300

2301+
# `slice[...]` subscript support (3.15+).
2302+
if isinstance(node.value, ast.Name) and node.value.id == "slice" \
2303+
and "slice" not in self.__s.user_defs:
2304+
self.__s.slice_subscription = True
2305+
self.__vvprint("slice subscription", line=node.lineno,
2306+
versions=[None, (3, 15)])
2307+
22942308
self.generic_visit(node)
22952309

22962310
def visit_Match(self, node):

0 commit comments

Comments
 (0)