bpo-37045: PEP 591: Add final qualifiers to typing module (GH-13571) · python/cpython@f367242 (original) (raw)

`@@ -35,6 +35,7 @@

`

35

35

`'Any',

`

36

36

`'Callable',

`

37

37

`'ClassVar',

`

``

38

`+

'Final',

`

38

39

`'Generic',

`

39

40

`'Optional',

`

40

41

`'Tuple',

`

92

93

`# One-off things.

`

93

94

`'AnyStr',

`

94

95

`'cast',

`

``

96

`+

'final',

`

95

97

`'get_type_hints',

`

96

98

`'NewType',

`

97

99

`'no_type_check',

`

`@@ -121,7 +123,7 @@ def _type_check(arg, msg, is_argument=True):

`

121

123

` """

`

122

124

`invalid_generic_forms = (Generic, _Protocol)

`

123

125

`if is_argument:

`

124

``

`-

invalid_generic_forms = invalid_generic_forms + (ClassVar, )

`

``

126

`+

invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)

`

125

127

``

126

128

`if arg is None:

`

127

129

`return type(None)

`

`@@ -336,8 +338,8 @@ def subclasscheck(self, cls):

`

336

338

``

337

339

`@_tp_cache

`

338

340

`def getitem(self, parameters):

`

339

``

`-

if self._name == 'ClassVar':

`

340

``

`-

item = _type_check(parameters, 'ClassVar accepts only single type.')

`

``

341

`+

if self._name in ('ClassVar', 'Final'):

`

``

342

`+

item = _type_check(parameters, f'{self._name} accepts only single type.')

`

341

343

`return _GenericAlias(self, (item,))

`

342

344

`if self._name == 'Union':

`

343

345

`if parameters == ():

`

`@@ -398,6 +400,24 @@ class Starship:

`

398

400

` be used with isinstance() or issubclass().

`

399

401

` """)

`

400

402

``

``

403

`+

Final = _SpecialForm('Final', doc=

`

``

404

`+

"""Special typing construct to indicate final names to type checkers.

`

``

405

+

``

406

`+

A final name cannot be re-assigned or overridden in a subclass.

`

``

407

`+

For example:

`

``

408

+

``

409

`+

MAX_SIZE: Final = 9000

`

``

410

`+

MAX_SIZE += 1 # Error reported by type checker

`

``

411

+

``

412

`+

class Connection:

`

``

413

`+

TIMEOUT: Final[int] = 10

`

``

414

+

``

415

`+

class FastConnector(Connection):

`

``

416

`+

TIMEOUT = 1 # Error reported by type checker

`

``

417

+

``

418

`+

There is no runtime checking of these properties.

`

``

419

`+

""")

`

``

420

+

401

421

`Union = _SpecialForm('Union', doc=

`

402

422

`"""Union type; Union[X, Y] means either X or Y.

`

403

423

``

`@@ -1085,6 +1105,32 @@ def utf8(value):

`

1085

1105

`return _overload_dummy

`

1086

1106

``

1087

1107

``

``

1108

`+

def final(f):

`

``

1109

`+

"""A decorator to indicate final methods and final classes.

`

``

1110

+

``

1111

`+

Use this decorator to indicate to type checkers that the decorated

`

``

1112

`+

method cannot be overridden, and decorated class cannot be subclassed.

`

``

1113

`+

For example:

`

``

1114

+

``

1115

`+

class Base:

`

``

1116

`+

@final

`

``

1117

`+

def done(self) -> None:

`

``

1118

`+

...

`

``

1119

`+

class Sub(Base):

`

``

1120

`+

def done(self) -> None: # Error reported by type checker

`

``

1121

`+

...

`

``

1122

+

``

1123

`+

@final

`

``

1124

`+

class Leaf:

`

``

1125

`+

...

`

``

1126

`+

class Other(Leaf): # Error reported by type checker

`

``

1127

`+

...

`

``

1128

+

``

1129

`+

There is no runtime checking of these properties.

`

``

1130

`+

"""

`

``

1131

`+

return f

`

``

1132

+

``

1133

+

1088

1134

`class _ProtocolMeta(type):

`

1089

1135

`"""Internal metaclass for _Protocol.

`

1090

1136

``