コード例 #1
0
def _(obj: Mapping, _started: bool = False) -> Union[NamedTuple, Tuple]:
    keys = []
    for key in obj.keys():
        if hasattr(key, 'capitalize'):
            key = cast(str, key)
            try:
                validate_identifier(key, allow_underscore=False)
            except SyntaxError:
                continue
            if key.isidentifier():
                keys.append(key)
    if not isinstance(obj, OrderedDict):
        keys = tuple(sorted(keys))
    args = []
    for key in keys:
        val: Any = obj[key]
        val = _to_namedtuple(val, _started=True)
        args.append(val)
    if args:
        # noinspection Mypy
        make = namedtuple('NamedTuple', keys)  # type: ignore[misc]
        # noinspection PyTypeChecker,PyArgumentList
        out: NamedTuple = make(*args)
        return out
    make_empty = namedtuple('NamedTuple', '')
    out = make_empty()
    return out
コード例 #2
0
 def test_integration_validate_identifier_builtin_raises(self):
     names = tuple(filter(
         lambda x: x.startswith('__') and x.endswith('__'),
         dir('__builtins__')
     ))
     for val in names:
         with self.assertRaises(SyntaxError):
             validate_identifier(val)
コード例 #3
0
 def test_integration_validate_identifier_digit_raises(self):
     vals = (
         '0',
         '05g',
         '6foo'
     )
     for val in vals:
         with self.assertRaises(SyntaxError):
             validate_identifier(val)
コード例 #4
0
 def test_integration_validate_identifier_empty_raises(self):
     vals = (
         '',
         ' ',
         '\t'
     )
     for val in vals:
         with self.assertRaises(SyntaxError):
             validate_identifier(val)
コード例 #5
0
 def test_integration_validate_identifier_invalid_raises(self):
     vals = (
         'a-b',
         'c.d',
         'e=f',
         'g+h',
         '{adf}',
         'j*k'
     )
     for val in vals:
         with self.assertRaises(SyntaxError):
             validate_identifier(val)
コード例 #6
0
 def test_integration_validate_identifier_invalid_type(self):
     vals = (
         None,
         33,
         33.33,
         list(),
         tuple(),
         SimpleNamespace(),
         dict(a=5),
     )
     for val in vals:
         with self.assertRaises(TypeError):
             validate_identifier(val)
コード例 #7
0
 def test_integration_validate_identifier_user_string(self):
     try:
         validate_identifier(UserString('bar'))
     except TypeError as e:
         self.fail(
             "'validate_identifier()' raised TypeError unexpectedly! %s"
             % e
         )
     except SyntaxError as e:
         self.fail(
             "'validate_identifier()' raised SyntaxError unexpectedly! %s"
             % e
         )
コード例 #8
0
 def test_integration_validate_identifier(self):
     try:
         validate_identifier('foo')
     except TypeError as e:
         self.fail(
             "'validate_identifier()' raised TypeError unexpectedly! %s"
             % e
         )
     except SyntaxError as e:
         self.fail(
             "'validate_identifier()' raised SyntaxError unexpectedly! %s"
             % e
         )
コード例 #9
0
 def test_integration_validate_identifier_keyword_raises(self):
     for val in kwlist:
         with self.assertRaises(SyntaxError):
             validate_identifier(val)
コード例 #10
0
 def test_integration_validate_identifier_underscore_raises(self):
     with self.assertRaises(SyntaxError):
         validate_identifier('_foo', allow_underscore=False)