How to use __class_getitem__ method in pandera

Best Python code snippet using pandera_python

test_genericclass.py

Source:test_genericclass.py Github

copy

Full Screen

...138class TestClassGetitem(unittest.TestCase):139 def test_class_getitem(self):140 getitem_args = []141 class C:142 def __class_getitem__(*args, **kwargs):143 getitem_args.extend([args, kwargs])144 return None145 C[int, str]146 self.assertEqual(getitem_args[0], (C, (int, str)))147 self.assertEqual(getitem_args[1], {})148 def test_class_getitem(self):149 class C:150 def __class_getitem__(cls, item):151 return f'C[{item.__name__}]'152 self.assertEqual(C[int], 'C[int]')153 self.assertEqual(C[C], 'C[C]')154 def test_class_getitem_inheritance(self):155 class C:156 def __class_getitem__(cls, item):157 return f'{cls.__name__}[{item.__name__}]'158 class D(C): ...159 self.assertEqual(D[int], 'D[int]')160 self.assertEqual(D[D], 'D[D]')161 def test_class_getitem_inheritance_2(self):162 class C:163 def __class_getitem__(cls, item):164 return 'Should not see this'165 class D(C):166 def __class_getitem__(cls, item):167 return f'{cls.__name__}[{item.__name__}]'168 self.assertEqual(D[int], 'D[int]')169 self.assertEqual(D[D], 'D[D]')170 def test_class_getitem_classmethod(self):171 class C:172 @classmethod173 def __class_getitem__(cls, item):174 return f'{cls.__name__}[{item.__name__}]'175 class D(C): ...176 self.assertEqual(D[int], 'D[int]')177 self.assertEqual(D[D], 'D[D]')178 def test_class_getitem_patched(self):179 class C:180 def __init_subclass__(cls):181 def __class_getitem__(cls, item):182 return f'{cls.__name__}[{item.__name__}]'183 cls.__class_getitem__ = classmethod(__class_getitem__)184 class D(C): ...185 self.assertEqual(D[int], 'D[int]')186 self.assertEqual(D[D], 'D[D]')187 def test_class_getitem_with_builtins(self):188 class A(dict):189 called_with = None190 def __class_getitem__(cls, item):191 cls.called_with = item192 class B(A):193 pass194 self.assertIs(B.called_with, None)195 B[int]196 self.assertIs(B.called_with, int)197 def test_class_getitem_errors(self):198 class C_too_few:199 def __class_getitem__(cls):200 return None201 with self.assertRaises(TypeError):202 C_too_few[int]203 class C_too_many:204 def __class_getitem__(cls, one, two):205 return None206 with self.assertRaises(TypeError):207 C_too_many[int]208 def test_class_getitem_errors_2(self):209 class C:210 def __class_getitem__(cls, item):211 return None212 with self.assertRaises(TypeError):213 C()[int]214 class E: ...215 e = E()216 e.__class_getitem__ = lambda cls, item: 'This will not work'217 with self.assertRaises(TypeError):218 e[int]219 class C_not_callable:220 __class_getitem__ = "Surprise!"221 with self.assertRaises(TypeError):222 C_not_callable[int]223 def test_class_getitem_metaclass(self):224 class Meta(type):225 def __class_getitem__(cls, item):226 return f'{cls.__name__}[{item.__name__}]'227 self.assertEqual(Meta[int], 'Meta[int]')228 def test_class_getitem_metaclass_2(self):229 class Meta(type):230 def __getitem__(cls, item):231 return 'from metaclass'232 class C(metaclass=Meta):233 def __class_getitem__(cls, item):234 return 'from __class_getitem__'235 self.assertEqual(C[int], 'from metaclass')236@support.cpython_only237class CAPITest(unittest.TestCase):238 def test_c_class(self):239 from _testcapi import Generic, GenericAlias240 self.assertIsInstance(Generic.__class_getitem__(int), GenericAlias)241 IntGeneric = Generic[int]242 self.assertIs(type(IntGeneric), GenericAlias)243 self.assertEqual(IntGeneric.__mro_entries__(()), (int,))244 class C(IntGeneric):245 pass246 self.assertEqual(C.__bases__, (int,))247 self.assertEqual(C.__orig_bases__, (IntGeneric,))248 self.assertEqual(C.__mro__, (C, int, object))249if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful