bpo-23722: Raise a RuntimeError for absent classcell. (GH-6931) · python/cpython@f5e7b19 (original) (raw)

1

1

`"""Unit tests for zero-argument super() & related machinery."""

`

2

2

``

3

3

`import unittest

`

4

``

`-

import warnings

`

5

``

`-

from test.support import check_warnings

`

6

4

``

7

5

``

8

6

`class A:

`

`@@ -173,14 +171,10 @@ def new(cls, name, bases, namespace):

`

173

171

`test_namespace = namespace

`

174

172

`return None

`

175

173

``

176

``

`-

This case shouldn't trigger the classcell deprecation warning

`

177

``

`-

with check_warnings() as w:

`

178

``

`-

warnings.simplefilter("always", DeprecationWarning)

`

179

``

`-

class A(metaclass=Meta):

`

180

``

`-

@staticmethod

`

181

``

`-

def f():

`

182

``

`-

return class

`

183

``

`-

self.assertEqual(w.warnings, [])

`

``

174

`+

class A(metaclass=Meta):

`

``

175

`+

@staticmethod

`

``

176

`+

def f():

`

``

177

`+

return class

`

184

178

``

185

179

`self.assertIs(A, None)

`

186

180

``

`@@ -244,37 +238,19 @@ def new(cls, name, bases, namespace):

`

244

238

`namespace.pop('classcell', None)

`

245

239

`return super().new(cls, name, bases, namespace)

`

246

240

``

247

``

`-

The default case should continue to work without any warnings

`

248

``

`-

with check_warnings() as w:

`

249

``

`-

warnings.simplefilter("always", DeprecationWarning)

`

250

``

`-

class WithoutClassRef(metaclass=Meta):

`

251

``

`-

pass

`

252

``

`-

self.assertEqual(w.warnings, [])

`

``

241

`+

The default case should continue to work without any errors

`

``

242

`+

class WithoutClassRef(metaclass=Meta):

`

``

243

`+

pass

`

253

244

``

254

245

`# With zero-arg super() or an explicit class reference, we expect

`

255

``

`-

build_class to emit a DeprecationWarning complaining that

`

``

246

`+

build_class to raise a RuntimeError complaining that

`

256

247

`# class was not set, and asking if classcell was propagated

`

257

248

`# to type.new.

`

258

``

`-

In Python 3.7, that warning will become a RuntimeError.

`

259

``

`-

expected_warning = (

`

260

``

`-

'class not set.*classcell propagated',

`

261

``

`-

DeprecationWarning

`

262

``

`-

)

`

263

``

`-

with check_warnings(expected_warning):

`

264

``

`-

warnings.simplefilter("always", DeprecationWarning)

`

``

249

`+

expected_error = 'class not set.*classcell propagated'

`

``

250

`+

with self.assertRaisesRegex(RuntimeError, expected_error):

`

265

251

`class WithClassRef(metaclass=Meta):

`

266

252

`def f(self):

`

267

253

`return class

`

268

``

`-

Check class still gets set despite the warning

`

269

``

`-

self.assertIs(WithClassRef().f(), WithClassRef)

`

270

``

-

271

``

`-

Check the warning is turned into an error as expected

`

272

``

`-

with warnings.catch_warnings():

`

273

``

`-

warnings.simplefilter("error", DeprecationWarning)

`

274

``

`-

with self.assertRaises(DeprecationWarning):

`

275

``

`-

class WithClassRef(metaclass=Meta):

`

276

``

`-

def f(self):

`

277

``

`-

return class

`

278

254

``

279

255

`def test___classcell___overwrite(self):

`

280

256

`# See issue #23722

`