forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ta_inheritence.py
74 lines (58 loc) · 2.33 KB
/
test_ta_inheritence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Inheritence around add_cmdline_args can be tricky.
This serves as an example, and verifies inheritence is behaving correctly.
"""
import unittest
from parlai.core.params import ParlaiParser
from parlai.core.torch_generator_agent import TorchGeneratorAgent
class FakeDict(object):
@classmethod
def add_cmdline_args(cls, parser):
parser.add_argument('--dictarg', default='d')
class SubClassA(TorchGeneratorAgent):
@classmethod
def dictionary_class(cls):
return FakeDict
@classmethod
def add_cmdline_args(cls, parser):
parser.add_argument('--withclassinheritence', default='a')
super(SubClassA, cls).add_cmdline_args(parser)
class SubClassB(SubClassA):
@classmethod
def add_cmdline_args(cls, parser):
parser.add_argument('--withoutclassinheritence', default='b')
class TestInheritence(unittest.TestCase):
def test_subclassA(self):
"""
Verify that class A does contain the super args.
"""
parser = ParlaiParser(add_model_args=True)
opt = parser.parse_args(args=['--model', 'tests.test_ta_inheritence:SubClassA'])
self.assertEqual('a', opt.get('withclassinheritence'))
# make sure we have the dictionary arg
self.assertEqual('d', opt.get('dictarg'))
# something that torch agent has
self.assertIn('no_cuda', opt)
# something torch generator agent has
self.assertIn('beam_size', opt)
def test_subclassB(self):
"""
Verify that class B does not contain the super args.
"""
parser = ParlaiParser(add_model_args=True)
opt = parser.parse_args(args=['--model', 'tests.test_ta_inheritence:SubClassB'])
self.assertEqual('b', opt.get('withoutclassinheritence'))
# make sure we don't have the dictionary now
self.assertNotIn('dictarg', opt)
# something the parent has
self.assertNotIn('withclassinheritence', opt)
# something that torch agent has
self.assertNotIn('no_cuda', opt)
# something torch generator agent has
self.assertNotIn('beam_size', opt)
if __name__ == '__main__':
unittest.main()