Skip to content

Commit e6278cc

Browse files
committed
Refactor typing.TypeVar into PEP695 syntax (UP046 & UP047)
1 parent 881c2f2 commit e6278cc

40 files changed

+153
-253
lines changed

edb/common/ast/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from typing import (
2828
Any,
2929
Callable,
30-
TypeVar,
3130
cast,
3231
get_type_hints,
3332
TYPE_CHECKING,
@@ -39,9 +38,6 @@
3938
from edb.common import typing_inspect
4039

4140

42-
T = TypeVar('T')
43-
44-
4541
class ASTError(Exception):
4642
pass
4743

@@ -69,7 +65,7 @@ def __init__(self, factory):
6965
self.factory = factory
7066

7167

72-
def field(*, factory: Callable[[], T]) -> T:
68+
def field[T](*, factory: Callable[[], T]) -> T:
7369
return cast(T, _FieldSpec(factory=factory))
7470

7571

@@ -275,7 +271,7 @@ def __deepcopy__(self, memo):
275271
def _init_copy(self):
276272
return self.__class__()
277273

278-
def replace(self: T, **changes) -> T:
274+
def replace[T](self: T, **changes) -> T:
279275
copied = copy.copy(self)
280276
for field, value in changes.items():
281277
object.__setattr__(copied, field, value)

edb/common/asyncutil.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
import warnings
3434

3535

36-
_T = TypeVar('_T')
37-
38-
39-
async def deferred_shield(arg: Awaitable[_T]) -> _T:
36+
async def deferred_shield[T](arg: Awaitable[T]) -> T:
4037
'''Wait for a future, deferring cancellation until it is complete.
4138
4239
If you do
@@ -74,9 +71,9 @@ async def deferred_shield(arg: Awaitable[_T]) -> _T:
7471
return task.result()
7572

7673

77-
async def debounce(
78-
input: Callable[[], Awaitable[_T]],
79-
output: Callable[[list[_T]], Awaitable[None]],
74+
async def debounce[T](
75+
input: Callable[[], Awaitable[T]],
76+
output: Callable[[list[T]], Awaitable[None]],
8077
*,
8178
max_wait: float,
8279
delay_amt: float,

edb/common/checked.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
from typing import (
2121
Any,
2222
ClassVar,
23-
Generic,
2423
Optional,
25-
TypeVar,
2624
AbstractSet,
2725
Iterable,
2826
Iterator,
@@ -51,11 +49,6 @@
5149
]
5250

5351

54-
T = TypeVar("T")
55-
K = TypeVar("K")
56-
V = TypeVar("V")
57-
58-
5952
class ParametricContainer:
6053

6154
types: ClassVar[Optional[tuple[type, ...]]] = None
@@ -81,7 +74,7 @@ def __restore__(
8174
return cls[params](data) # type: ignore
8275

8376

84-
class AbstractCheckedList(Generic[T]):
77+
class AbstractCheckedList[T]:
8578
type: type
8679
_container: list[T]
8780

@@ -128,7 +121,7 @@ def __repr__(self) -> str:
128121
return f"{_type_repr(type(self))}({repr(self._container)})"
129122

130123

131-
class FrozenCheckedList(
124+
class FrozenCheckedList[T](
132125
ParametricContainer,
133126
parametric.SingleParametricType[T],
134127
AbstractCheckedList[T],
@@ -179,7 +172,7 @@ def __mul__(self, n: int) -> FrozenCheckedList[T]:
179172
__rmul__ = __mul__
180173

181174

182-
class CheckedList(
175+
class CheckedList[T](
183176
ParametricContainer,
184177
parametric.SingleParametricType[T],
185178
AbstractCheckedList[T],
@@ -265,7 +258,7 @@ def sort(self, *, key: Any = None, reverse: bool = False) -> None:
265258
self._container.sort(key=key, reverse=reverse)
266259

267260

268-
class AbstractCheckedSet(AbstractSet[T]):
261+
class AbstractCheckedSet[T](AbstractSet[T]):
269262
type: type
270263
_container: AbstractSet[T]
271264

@@ -326,7 +319,7 @@ def issuperset(self, other: AbstractSet[Any]) -> bool:
326319
return self.__ge__(other)
327320

328321

329-
class FrozenCheckedSet(
322+
class FrozenCheckedSet[T](
330323
ParametricContainer,
331324
parametric.SingleParametricType[T],
332325
AbstractCheckedSet[T],
@@ -400,7 +393,7 @@ def __xor__( # type: ignore
400393
symmetric_difference = __xor__
401394

402395

403-
class CheckedSet(
396+
class CheckedSet[T](
404397
ParametricContainer,
405398
parametric.SingleParametricType[T],
406399
AbstractCheckedSet[T],
@@ -520,7 +513,7 @@ def _type_repr(obj: Any) -> str:
520513
return repr(obj)
521514

522515

523-
class AbstractCheckedDict(Generic[K, V]):
516+
class AbstractCheckedDict[K, V]:
524517
keytype: type
525518
valuetype: type
526519
_container: dict[K, V]
@@ -557,7 +550,7 @@ def __repr__(self) -> str:
557550
return f"{_type_repr(type(self))}({repr(self._container)})"
558551

559552

560-
class CheckedDict(
553+
class CheckedDict[K, V](
561554
ParametricContainer,
562555
parametric.KeyValueParametricType[K, V],
563556
AbstractCheckedDict[K, V],
@@ -617,7 +610,7 @@ def fromkeys(
617610
return new
618611

619612

620-
def _identity(cls: type, value: T) -> T:
613+
def _identity[T](cls: type, value: T) -> T:
621614
return value
622615

623616

edb/common/compiler.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,42 @@
2121

2222
from typing import (
2323
Any,
24-
Generic,
2524
Optional,
26-
TypeVar,
2725
ContextManager,
26+
Self,
2827
)
2928

3029
import collections
3130
import re
3231

3332

34-
ContextLevel_T = TypeVar('ContextLevel_T', bound='ContextLevel')
35-
36-
3733
class ContextLevel:
38-
_stack: CompilerContext[ContextLevel]
34+
_stack: CompilerContext[Self]
3935

40-
def __init__(self, prevlevel: Optional[ContextLevel], mode: Any) -> None:
36+
def __init__(self, prevlevel: Optional[Self], mode: Any) -> None:
4137
pass
4238

4339
def on_pop(
44-
self: ContextLevel_T,
45-
prevlevel: Optional[ContextLevel_T],
40+
self: Self,
41+
prevlevel: Optional[Self],
4642
) -> None:
4743
pass
4844

4945
def new(
50-
self: ContextLevel_T,
46+
self: Self,
5147
mode: Any=None,
52-
) -> CompilerContextManager[ContextLevel_T]:
53-
return self._stack.new(mode, self) # type: ignore
48+
) -> CompilerContextManager[Self]:
49+
return self._stack.new(mode, self)
5450

5551
def reenter(
56-
self: ContextLevel_T,
57-
) -> CompilerReentryContextManager[ContextLevel_T]:
58-
return CompilerReentryContextManager(self._stack, self) # type: ignore
52+
self: Self,
53+
) -> CompilerReentryContextManager[Self]:
54+
return CompilerReentryContextManager(self._stack, self)
5955

6056

61-
class CompilerContextManager(ContextManager[ContextLevel_T]):
57+
class CompilerContextManager[ContextLevel_T: ContextLevel](
58+
ContextManager[ContextLevel_T]
59+
):
6260
def __init__(
6361
self,
6462
context: CompilerContext[ContextLevel_T],
@@ -76,7 +74,9 @@ def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
7674
self.context.pop()
7775

7876

79-
class CompilerReentryContextManager(ContextManager[ContextLevel_T]):
77+
class CompilerReentryContextManager[ContextLevel_T: ContextLevel](
78+
ContextManager[ContextLevel_T]
79+
):
8080
def __init__(
8181
self,
8282
context: CompilerContext[ContextLevel_T],
@@ -92,7 +92,7 @@ def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
9292
self.context.pop()
9393

9494

95-
class CompilerContext(Generic[ContextLevel_T]):
95+
class CompilerContext[ContextLevel_T: ContextLevel]:
9696
stack: list[ContextLevel_T]
9797
ContextLevelClass: type[ContextLevel_T]
9898
default_mode: Any
@@ -131,7 +131,7 @@ def _push(
131131
raise AssertionError(
132132
'Calling new() on a context other than the current one')
133133
level = self.ContextLevelClass(prevlevel, mode)
134-
level._stack = self # type: ignore
134+
level._stack = self
135135
self.stack.append(level)
136136
return level
137137

edb/common/lru.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import functools
2424

2525

26-
from typing import TypeVar, Callable
26+
from typing import Callable
2727

2828

2929
class LRUMapping(collections.abc.MutableMapping):
@@ -81,9 +81,6 @@ def __iter__(self):
8181
return iter(self._dict)
8282

8383

84-
Tf = TypeVar('Tf', bound=Callable)
85-
86-
8784
class _NoPickle:
8885
def __init__(self, obj):
8986
self.obj = obj
@@ -98,7 +95,7 @@ def __setstate__(self, _d):
9895
self.obj = None
9996

10097

101-
def lru_method_cache(size: int | None=128) -> Callable[[Tf], Tf]:
98+
def lru_method_cache[Tf: Callable](size: int | None=128) -> Callable[[Tf], Tf]:
10299
"""A version of lru_cache for methods that shouldn't leak memory.
103100
104101
Basically the idea is that we generate a per-object lru-cached
@@ -125,5 +122,5 @@ def func(self, *args, **kwargs):
125122
return transformer
126123

127124

128-
def method_cache(f: Tf) -> Tf:
125+
def method_cache[Tf: Callable](f: Tf) -> Tf:
129126
return lru_method_cache(None)(f)

edb/common/markup/serializer/base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
from __future__ import annotations
2121

22-
from typing import TypeVar
23-
2422
import collections
2523
import collections.abc
2624
import decimal
@@ -51,10 +49,7 @@
5149
__all__ = 'serialize',
5250

5351

54-
T = TypeVar('T')
55-
56-
57-
def no_ref_detect(func: T) -> T:
52+
def no_ref_detect[T](func: T) -> T:
5853
"""Serializer decorated with ``no_ref_detect`` will be executed without
5954
prior checking the memo if object was already serialized"""
6055

edb/common/parametric.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from typing import (
2121
Any,
2222
ClassVar,
23-
Generic,
2423
Optional,
2524
TypeVar,
2625
get_type_hints,
@@ -40,10 +39,6 @@
4039
]
4140

4241

43-
T = TypeVar("T")
44-
V = TypeVar("V")
45-
46-
4742
try:
4843
from types import GenericAlias
4944
except ImportError:
@@ -323,12 +318,12 @@ def __reduce__(self) -> tuple[Any, ...]:
323318
)
324319

325320

326-
class SingleParametricType(ParametricType, Generic[T]):
321+
class SingleParametricType[T](ParametricType):
327322

328323
type: ClassVar[type[T]] # type: ignore
329324

330325

331-
class KeyValueParametricType(ParametricType, Generic[T, V]):
326+
class KeyValueParametricType[T, V](ParametricType):
332327

333328
keytype: ClassVar[type[T]] # type: ignore
334329
valuetype: ClassVar[type[V]] # type: ignore

0 commit comments

Comments
 (0)