Skip to content

Commit 8d5417e

Browse files
committed
Replace Cls with type(self).
1 parent 5690135 commit 8d5417e

3 files changed

Lines changed: 17 additions & 25 deletions

File tree

tests/test_type_eval.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from typemap.typing import (
2222
Attrs,
2323
Bool,
24-
Cls,
2524
FromUnion,
2625
GenericCallable,
2726
GetArg,
@@ -1517,14 +1516,14 @@ def static_method(x: int) -> int: ...
15171516
""")
15181517

15191518

1520-
def test_type_eval_cls_01():
1521-
res = eval_typing(Cls)
1522-
assert res == Cls
1519+
def test_type_eval_type_self_01():
1520+
res = eval_typing(type(Self))
1521+
assert res is type(Self)
15231522

15241523

1525-
def test_type_eval_cls_02():
1524+
def test_type_eval_type_self_02():
15261525
class A:
1527-
child: Cls | None
1526+
child: type(Self) | None
15281527

15291528
class B(A):
15301529
pass
@@ -1553,19 +1552,19 @@ class B(A):
15531552
fmt = format_helper.format_class(A)
15541553
assert fmt == textwrap.dedent("""\
15551554
class A:
1556-
child: tests.test_type_eval.test_type_eval_cls_02.<locals>.A | None
1555+
child: tests.test_type_eval.test_type_eval_type_self_02.<locals>.A | None
15571556
""")
15581557
fmt = format_helper.format_class(B)
15591558
assert fmt == textwrap.dedent("""\
15601559
class B:
1561-
child: tests.test_type_eval.test_type_eval_cls_02.<locals>.B | None
1560+
child: tests.test_type_eval.test_type_eval_type_self_02.<locals>.B | None
15621561
""")
15631562

15641563

1565-
type ValueOrNone = GetAttr[Cls, Literal["value"]] | None
1564+
type ValueOrNone = GetAttr[type(Self), Literal["value"]] | None
15661565

15671566

1568-
def test_type_eval_cls_03():
1567+
def test_type_eval_type_self_03():
15691568
class A:
15701569
value: int
15711570
maybe: ValueOrNone
@@ -1595,7 +1594,7 @@ class A:
15951594
""")
15961595

15971596

1598-
def test_type_eval_cls_04():
1597+
def test_type_eval_type_self_04():
15991598
class A[T]:
16001599
value: T
16011600
maybe: ValueOrNone
@@ -1660,7 +1659,7 @@ class A[T]:
16601659
)
16611660

16621661

1663-
def test_type_eval_cls_05():
1662+
def test_type_eval_type_self_05():
16641663
class A:
16651664
value: str
16661665
maybe: ValueOrNone
@@ -1688,7 +1687,7 @@ class B:
16881687
)
16891688

16901689

1691-
def test_type_eval_cls_06():
1690+
def test_type_eval_type_self_06():
16921691
class A[T]:
16931692
value: T
16941693
maybe: ValueOrNone
@@ -1748,7 +1747,7 @@ class B[T, U]:
17481747
)
17491748

17501749

1751-
def test_type_eval_cls_07():
1750+
def test_type_eval_type_self_07():
17521751
class A:
17531752
value: int
17541753
maybe: ValueOrNone
@@ -1761,7 +1760,7 @@ class B:
17611760
assert eval_typing(AttrSetOfOnlyInt[B, Literal["value"]]) is str
17621761

17631762

1764-
def test_type_eval_cls_08():
1763+
def test_type_eval_type_self_08():
17651764
class A[T]:
17661765
value: T
17671766
maybe: ValueOrNone

typemap/type_eval/_eval_typing.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,11 @@ def _get_class_type_hint_namespaces(
320320

321321
@_eval_types_impl.register
322322
def _eval_type_type(obj: type, ctx: EvalContext):
323-
from typemap.typing import Cls
324-
325-
# Special handling for Cls
326-
if obj is Cls:
327-
# If we are evaluating Cls, return the current class
323+
if obj is type(typing.Self):
324+
# If we are evaluating type(Self), return the current class
328325
if ctx.current_cls is not None:
329326
return ctx.current_cls
330-
return Cls
327+
return type(typing.Self)
331328

332329
# Ensure that any string annotations are resolved
333330
if (

typemap/typing.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ class NewProtocol[*T]:
175175
pass
176176

177177

178-
class Cls:
179-
pass
180-
181-
182178
##################################################################
183179

184180
# TODO: type better

0 commit comments

Comments
 (0)