Esempio n. 1
0
 def test_get_existing_name(self):
     obj = get_name_in_module(a_module_func.__module__,
                              a_module_func.__qualname__)
     assert obj == a_module_func
     # Make sure we handle nested classes
     obj = get_name_in_module(Outer.__module__, Outer.Inner.f.__qualname__)
     assert obj == Outer.Inner.f
Esempio n. 2
0
def type_from_dict(d: TypeDict) -> type:
    """Given a dictionary produced by type_to_dict, return the equivalent type.

    Raises:
        NameLookupError if we can't reify the specified type
        InvalidTypeError if the named type isn't actually a type
    """
    module, qualname = d['module'], d['qualname']
    if d.get('is_typed_dict', False):
        return typed_dict_from_dict(d)
    if module == 'builtins' and qualname in _HIDDEN_BUILTIN_TYPES:
        typ = _HIDDEN_BUILTIN_TYPES[qualname]
    else:
        typ = get_name_in_module(module, qualname)
    if not (
        isinstance(typ, type) or
        is_any(typ) or
        is_generic(typ)
    ):
        raise InvalidTypeError(
            f"Attribute specified by '{qualname}' in module '{module}' "
            f"is of type {type(typ)}, not type."
        )
    elem_type_dicts = d.get('elem_types')
    if elem_type_dicts is not None and is_generic(typ):
        elem_types = tuple(type_from_dict(e) for e in elem_type_dicts)
        # mypy complains that a value of type `type` isn't indexable. That's
        # true, but we know typ is a subtype that is indexable. Even checking
        # with hasattr(typ, '__getitem__') doesn't help
        typ = typ[elem_types]  # type: ignore
    return typ
Esempio n. 3
0
 def from_callable(cls, func: Callable) -> 'FunctionKind':
     if '.' not in func.__qualname__:
         return FunctionKind.MODULE
     func_or_desc = get_name_in_module(func.__module__, func.__qualname__,
                                       inspect.getattr_static)
     if isinstance(func_or_desc, classmethod):
         return FunctionKind.CLASS
     elif isinstance(func_or_desc, staticmethod):
         return FunctionKind.STATIC
     elif isinstance(func_or_desc, property):
         return FunctionKind.PROPERTY
     return FunctionKind.INSTANCE
Esempio n. 4
0
 def from_callable(cls, func: Callable) -> 'FunctionKind':
     # pyre-fixme[16]: Anonymous callable has no attribute `__qualname__`.
     if '.' not in func.__qualname__:
         return FunctionKind.MODULE
     func_or_desc = get_name_in_module(func.__module__, func.__qualname__,
                                       inspect.getattr_static)
     if isinstance(func_or_desc, classmethod):
         return FunctionKind.CLASS
     elif isinstance(func_or_desc, staticmethod):
         return FunctionKind.STATIC
     elif isinstance(func_or_desc, property):
         return FunctionKind.PROPERTY
     elif cached_property and isinstance(func_or_desc, cached_property):
         return FunctionKind.DJANGO_CACHED_PROPERTY
     return FunctionKind.INSTANCE
Esempio n. 5
0
def get_monkeytype_config(path: str) -> Config:
    """Imports the config instance specified by path.

    Path should be in the form module:qualname. Optionally, path may end with (),
    in which case we will call/instantiate the given class/function.
    """
    should_call = False
    if path.endswith('()'):
        should_call = True
        path = path[:-2]
    module, qualname = module_path_with_qualname(path)
    try:
        config = get_name_in_module(module, qualname)
    except MonkeyTypeError as mte:
        raise argparse.ArgumentTypeError(f'cannot import {path}: {mte}')
    if should_call:
        config = config()
    return config
Esempio n. 6
0
 def test_get_nonexistent_qualname(self):
     with pytest.raises(NameLookupError):
         get_name_in_module(a_module_func.__module__,
                            'Outer.xxx_i_dont_exist_xxx')
Esempio n. 7
0
 def test_get_nonexistent_module(self):
     with pytest.raises(NameLookupError):
         get_name_in_module('xxx.dontexist', 'foo')