1- """JupyterLab command handler"""
1+ """JupyterLab command handler. """
22
33# Copyright (c) Jupyter Development Team.
44# Distributed under the terms of the Modified BSD License.
55
6+ from __future__ import annotations
7+
68import itertools
9+ from collections .abc import Callable
10+ from typing import Any
711
812from .jupyterlab_semver import Range , gt , gte , lt , lte
913
14+ _CmpFn = Callable [[str | Any , str | Any , bool ], bool ]
15+
1016
11- def _test_overlap (spec1 , spec2 , drop_prerelease1 = False , drop_prerelease2 = False ):
17+ def _test_overlap (
18+ spec1 : str ,
19+ spec2 : str ,
20+ drop_prerelease1 : bool = False ,
21+ drop_prerelease2 : bool = False ,
22+ ) -> bool | None :
1223 """Test whether two version specs overlap.
1324
1425 Returns `None` if we cannot determine compatibility,
1526 otherwise whether there is an overlap
1627 """
1728 cmp = _compare_ranges (
18- spec1 , spec2 , drop_prerelease1 = drop_prerelease1 , drop_prerelease2 = drop_prerelease2
29+ spec1 ,
30+ spec2 ,
31+ drop_prerelease1 = drop_prerelease1 ,
32+ drop_prerelease2 = drop_prerelease2 ,
1933 )
2034 if cmp is None :
21- return
35+ return None
2236 return cmp == 0
2337
2438
25- def _compare_ranges (spec1 , spec2 , drop_prerelease1 = False , drop_prerelease2 = False ): # noqa
39+ def _compare_ranges ( # noqa: C901, PLR0912
40+ spec1 : str ,
41+ spec2 : str ,
42+ drop_prerelease1 : bool = False ,
43+ drop_prerelease2 : bool = False ,
44+ ) -> int | None :
2645 """Test whether two version specs overlap.
2746
2847 Returns `None` if we cannot determine compatibility,
@@ -31,15 +50,15 @@ def _compare_ranges(spec1, spec2, drop_prerelease1=False, drop_prerelease2=False
3150 is higher/newer than spec2.
3251 """
3352 # Test for overlapping semver ranges.
34- r1 = Range (spec1 , True )
35- r2 = Range (spec2 , True )
53+ r1 = Range (spec1 , True ) # type: ignore[no-untyped-call]
54+ r2 = Range (spec2 , True ) # type: ignore[no-untyped-call]
3655
3756 # If either range is empty, we cannot verify.
3857 if not r1 .range or not r2 .range :
39- return
58+ return None
4059
4160 # Set return_value to a sentinel value
42- return_value = False
61+ return_value : int | bool | None = False
4362
4463 # r1.set may be a list of ranges if the range involved an ||,
4564 # so we need to test for overlaps between each pair.
@@ -63,13 +82,14 @@ def _compare_ranges(spec1, spec2, drop_prerelease1=False, drop_prerelease2=False
6382 continue
6483
6584 # Handle single value specifiers.
66- lx = lte if x1 == x2 else lt
67- ly = lte if y1 == y2 else lt
68- gx = gte if x1 == x2 else gt
69- gy = gte if x1 == x2 else gt
85+ lx : _CmpFn = lte if x1 == x2 else lt
86+ ly : _CmpFn = lte if y1 == y2 else lt
87+ gx : _CmpFn = gte if x1 == x2 else gt
88+ gy : _CmpFn = gte if x1 == x2 else gt
7089
7190 # Handle unbounded (>) specifiers.
72- def noop (x , y , z ):
91+ def noop (_x : str | Any , _y : str | Any , _z : bool ) -> bool :
92+ """Return True unconditionally, representing an unbounded range."""
7393 return True
7494
7595 if x1 == x2 and o1 .startswith (">" ):
@@ -79,15 +99,15 @@ def noop(x, y, z):
7999
80100 # Check for overlap.
81101 if (
82- (gte (x1 , y1 , True ) and ly (x1 , y2 , True ))
102+ (gte (x1 , y1 , True ) and ly (x1 , y2 , True )) # type: ignore[no-untyped-call]
83103 or (gy (x2 , y1 , True ) and ly (x2 , y2 , True ))
84- or (gte (y1 , x1 , True ) and lx (y1 , x2 , True ))
104+ or (gte (y1 , x1 , True ) and lx (y1 , x2 , True )) # type: ignore[no-untyped-call]
85105 or (gx (y2 , x1 , True ) and lx (y2 , x2 , True ))
86106 ):
87107 # if we ever find an overlap, we can return immediately
88108 return 0
89109
90- if gte (y1 , x2 , True ):
110+ if gte (y1 , x2 , True ): # type: ignore[no-untyped-call]
91111 if return_value is False :
92112 # We can possibly return 1
93113 return_value = 1
@@ -96,7 +116,7 @@ def noop(x, y, z):
96116 return_value = None
97117 continue
98118
99- if gte (x1 , y2 , True ):
119+ if gte (x1 , y2 , True ): # type: ignore[no-untyped-call]
100120 if return_value is False :
101121 return_value = - 1
102122 elif return_value == 1 :
0 commit comments