this repo has no description
at trunk 61 lines 1.6 kB view raw
1#!/usr/bin/env python3 2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 3import sys 4import unittest 5from typing import Dict, Generic, List, NamedTuple, Optional, TypeVar, Union 6 7 8class TypingTests(unittest.TestCase): 9 def test_generic_subclass_with_metaclass(self): 10 class M(type): 11 pass 12 13 A = TypeVar("A") 14 B = TypeVar("B") 15 16 class C(Generic[A, B], metaclass=M): 17 pass 18 19 self.assertIs(type(C), M) 20 self.assertTrue(issubclass(C, Generic)) 21 self.assertTrue(isinstance(C(), Generic)) 22 23 def test_typing_parameterized_generics(self): 24 def fn(val: Optional[Union[Dict, List]]): 25 return 5 26 27 self.assertEqual(fn(None), 5) 28 29 def test_namedtuple_keeps_textual_order_of_fields(self): 30 class C(NamedTuple): 31 zoozle: int 32 aardvark: str = "" 33 foo: int = 1 34 bar: int = 2 35 baz: int = 3 36 37 c = C(999, "abc", -1, -2, -3) 38 self.assertIs(c.zoozle, 999) 39 self.assertIs(c.aardvark, "abc") 40 self.assertIs(c.foo, -1) 41 self.assertIs(c.bar, -2) 42 self.assertIs(c.baz, -3) 43 44 def test_namedtuple_populates_default_values(self): 45 class C(NamedTuple): 46 zoozle: int 47 aardvark: str = "" 48 foo: int = 1 49 bar: int = 2 50 baz: int = 3 51 52 c = C(999) 53 self.assertIs(c.zoozle, 999) 54 self.assertIs(c.aardvark, "") 55 self.assertIs(c.foo, 1) 56 self.assertIs(c.bar, 2) 57 self.assertIs(c.baz, 3) 58 59 60if __name__ == "__main__": 61 unittest.main()