@@ -138,6 +138,7 @@ def _idfunc(_, x):
138
138
'NoReturn' ,
139
139
'NotRequired' ,
140
140
'overload' ,
141
+ 'override' ,
141
142
'ParamSpecArgs' ,
142
143
'ParamSpecKwargs' ,
143
144
'Required' ,
@@ -2657,6 +2658,7 @@ class Other(Leaf): # Error reported by type checker
2657
2658
# Internal type variable used for Type[].
2658
2659
CT_co = TypeVar ('CT_co' , covariant = True , bound = type )
2659
2660
2661
+
2660
2662
# A useful type variable with constraints. This represents string types.
2661
2663
# (This one *is* for export!)
2662
2664
AnyStr = TypeVar ('AnyStr' , bytes , str )
@@ -2748,6 +2750,8 @@ def new_user(user_class: Type[U]) -> U:
2748
2750
At this point the type checker knows that joe has type BasicUser.
2749
2751
"""
2750
2752
2753
+ # Internal type variable for callables. Not for export.
2754
+ F = TypeVar ("F" , bound = Callable [..., Any ])
2751
2755
2752
2756
@runtime_checkable
2753
2757
class SupportsInt (Protocol ):
@@ -3448,3 +3452,40 @@ def decorator(cls_or_fn):
3448
3452
}
3449
3453
return cls_or_fn
3450
3454
return decorator
3455
+
3456
+
3457
+
3458
+ def override (method : F , / ) -> F :
3459
+ """Indicate that a method is intended to override a method in a base class.
3460
+
3461
+ Usage:
3462
+
3463
+ class Base:
3464
+ def method(self) -> None: ...
3465
+ pass
3466
+
3467
+ class Child(Base):
3468
+ @override
3469
+ def method(self) -> None:
3470
+ super().method()
3471
+
3472
+ When this decorator is applied to a method, the type checker will
3473
+ validate that it overrides a method or attribute with the same name on a
3474
+ base class. This helps prevent bugs that may occur when a base class is
3475
+ changed without an equivalent change to a child class.
3476
+
3477
+ There is no runtime checking of this property. The decorator sets the
3478
+ ``__override__`` attribute to ``True`` on the decorated object to allow
3479
+ runtime introspection.
3480
+
3481
+ See PEP 698 for details.
3482
+
3483
+ """
3484
+ try :
3485
+ method .__override__ = True
3486
+ except (AttributeError , TypeError ):
3487
+ # Skip the attribute silently if it is not writable.
3488
+ # AttributeError happens if the object has __slots__ or a
3489
+ # read-only property, TypeError if it's a builtin class.
3490
+ pass
3491
+ return method
0 commit comments