27
27
)
28
28
29
29
import typing_extensions
30
- from typing_extensions import Never , ParamSpec , Self , TypeAlias , TypeGuard , TypeVarTuple
30
+ from typing_extensions import Never , ParamSpec , Self , TypeAlias , TypeGuard , \
31
+ TypeVarTuple , override
31
32
32
33
from basedtyping import transformer
33
34
from basedtyping .runtime_only import OldUnionType
69
70
class _BasedSpecialForm (_SpecialForm , _root = True ): # type: ignore[misc]
70
71
_name : str
71
72
73
+ @override
72
74
def __init_subclass__ (cls , _root = False ): # noqa: FBT002
73
75
super ().__init_subclass__ (_root = _root ) # type: ignore[call-arg]
74
76
75
77
def __init__ (self , * args : object , ** kwargs : object ):
76
78
self .alias = kwargs .pop ("alias" , _BasedGenericAlias )
77
79
super ().__init__ (* args , ** kwargs )
78
80
81
+ @override
79
82
def __repr__ (self ) -> str :
80
83
return "basedtyping." + self ._name
81
84
@@ -244,6 +247,7 @@ def _is_subclass(cls, subclass: object) -> TypeGuard[_ReifiedGenericMetaclass]:
244
247
cast (_ReifiedGenericMetaclass , subclass )._orig_class (),
245
248
)
246
249
250
+ @override
247
251
def __subclasscheck__ (cls , subclass : object ) -> bool :
248
252
if not cls ._is_subclass (subclass ):
249
253
return False
@@ -264,6 +268,7 @@ def __subclasscheck__(cls, subclass: object) -> bool:
264
268
subclass ._check_generics_reified ()
265
269
return cls ._type_var_check (subclass .__reified_generics__ )
266
270
271
+ @override
267
272
def __instancecheck__ (cls , instance : object ) -> bool :
268
273
if not cls ._is_subclass (type (instance )):
269
274
return False
@@ -272,6 +277,7 @@ def __instancecheck__(cls, instance: object) -> bool:
272
277
return cls ._type_var_check (cast (ReifiedGeneric [object ], instance ).__reified_generics__ )
273
278
274
279
# need the generic here for pyright. see https://github.com/microsoft/pyright/issues/5488
280
+ @override
275
281
def __call__ (cls : type [T ], * args : object , ** kwargs : object ) -> T :
276
282
"""A placeholder ``__call__`` method that gets called when the class is
277
283
instantiated directly, instead of first supplying the type parameters.
@@ -400,6 +406,7 @@ def __class_getitem__( # type: ignore[no-any-decorated]
400
406
reified_generic_copy ._can_do_instance_and_subclass_checks_without_generics = False
401
407
return reified_generic_copy
402
408
409
+ @override
403
410
def __init_subclass__ (cls ):
404
411
cls ._can_do_instance_and_subclass_checks_without_generics = True
405
412
super ().__init_subclass__ ()
@@ -473,14 +480,17 @@ def Untyped( # noqa: N802
473
480
474
481
475
482
class _IntersectionGenericAlias (_BasedGenericAlias , _root = True ):
483
+ @override
476
484
def copy_with (self , args : object ) -> Self : # type: ignore[override] # TODO: put in the overloads # noqa: TD003
477
485
return cast (Self , Intersection [args ])
478
486
487
+ @override
479
488
def __eq__ (self , other : object ) -> bool :
480
489
if not isinstance (other , _IntersectionGenericAlias ):
481
490
return NotImplemented
482
491
return set (self .__args__ ) == set (other .__args__ )
483
492
493
+ @override
484
494
def __hash__ (self ) -> int :
485
495
return hash (frozenset (self .__args__ ))
486
496
@@ -490,6 +500,7 @@ def __instancecheck__(self, obj: object) -> bool:
490
500
def __subclasscheck__ (self , cls : type [object ]) -> bool :
491
501
return all (issubclass (cls , arg ) for arg in self .__args__ )
492
502
503
+ @override
493
504
def __reduce__ (self ) -> (object , object ):
494
505
func , (_ , args ) = super ().__reduce__ () # type: ignore[no-any-expr, misc]
495
506
return func , (Intersection , args )
@@ -538,10 +549,13 @@ def Intersection(self: _BasedSpecialForm, parameters: object) -> object: # noqa
538
549
539
550
540
551
class _TypeFormForm (_BasedSpecialForm , _root = True ): # type: ignore[misc]
552
+ # TODO: decorator-ify
541
553
def __init__ (self , doc : str ):
554
+ super ().__init__ ()
542
555
self ._name = "TypeForm"
543
556
self ._doc = self .__doc__ = doc
544
557
558
+ @override
545
559
def __getitem__ (self , parameters : object | tuple [object ]) -> _BasedGenericAlias :
546
560
if not isinstance (parameters , tuple ):
547
561
parameters = (parameters ,)
@@ -610,8 +624,8 @@ def __init__(self, arg: str, *, is_argument=True, module: object = None, is_clas
610
624
except SyntaxError :
611
625
# Callable: () -> int
612
626
if "->" not in arg_to_compile :
613
- raise RuntimeError (
614
- f"expected a callable type, but found... what is { arg_to_compile } ?"
627
+ raise SyntaxError (
628
+ f"invalid syntax in ForwardRef: { arg_to_compile } ?"
615
629
) from None
616
630
code = compile ("'un-representable callable type'" , "<string>" , "eval" )
617
631
@@ -625,6 +639,7 @@ def __init__(self, arg: str, *, is_argument=True, module: object = None, is_clas
625
639
626
640
if sys .version_info >= (3 , 13 ):
627
641
642
+ @override
628
643
def _evaluate (
629
644
self ,
630
645
globalns : dict [str , object ] | None ,
@@ -637,6 +652,7 @@ def _evaluate(
637
652
638
653
elif sys .version_info >= (3 , 12 ):
639
654
655
+ @override
640
656
def _evaluate (
641
657
self ,
642
658
globalns : dict [str , object ] | None ,
@@ -649,6 +665,7 @@ def _evaluate(
649
665
650
666
else :
651
667
668
+ @override
652
669
def _evaluate (
653
670
self ,
654
671
globalns : dict [str , object ] | None ,
0 commit comments