Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(Dummy.a_cached_property.func, FunctionKind.DJANGO_CACHED_PROPERTY),
(a_module_func, FunctionKind.MODULE),
],
)
def test_from_callable(self, func, expected):
assert FunctionKind.from_callable(func) == expected
def _func_stub_from_callable(func: Callable, strip_modules: List[str] = None):
kind = FunctionKind.from_callable(func)
sig = Signature.from_callable(func)
return FunctionStub(func.__name__, sig, kind, strip_modules)
(a_module_func, FunctionKind.MODULE),
],
)
def test_from_callable(self, func, expected):
assert FunctionKind.from_callable(func) == expected
def test_classmethod(self):
stub = FunctionStub('test', inspect.signature(Dummy.a_class_method), FunctionKind.CLASS)
expected = "\n".join([
'@classmethod',
'def test%s: ...' % (render_signature(stub.signature),),
])
assert stub.render() == expected
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
elif cached_property and isinstance(func_or_desc, cached_property):
return FunctionKind.DJANGO_CACHED_PROPERTY
return FunctionKind.INSTANCE
def from_callable(cls, func: Callable, kind: FunctionKind = None) -> 'FunctionDefinition':
kind = FunctionKind.from_callable(func)
sig = inspect.Signature.from_callable(func)
is_async = asyncio.iscoroutinefunction(func)
return FunctionDefinition(func.__module__, func.__qualname__, kind, sig, is_async)
s = prefix
if self.is_async:
s += 'async '
s += 'def ' + self.name
s += render_signature(self.signature, 120 - len(s), prefix) + ': ...'
# Yes, this is a horrible hack, but inspect.py gives us no way to
# specify the function that should be used to format annotations.
for module in self.strip_modules:
s = s.replace(module + '.', '')
if self.kind == FunctionKind.CLASS:
s = prefix + "@classmethod\n" + s
elif self.kind == FunctionKind.STATIC:
s = prefix + "@staticmethod\n" + s
elif self.kind == FunctionKind.PROPERTY:
s = prefix + "@property\n" + s
elif self.kind == FunctionKind.DJANGO_CACHED_PROPERTY:
s = prefix + "@cached_property\n" + s
return s
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
elif cached_property and isinstance(func_or_desc, cached_property):
return FunctionKind.DJANGO_CACHED_PROPERTY
return FunctionKind.INSTANCE
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
elif cached_property and isinstance(func_or_desc, cached_property):
return FunctionKind.DJANGO_CACHED_PROPERTY
return FunctionKind.INSTANCE
def render(self, prefix: str = '') -> str:
s = prefix
if self.is_async:
s += 'async '
s += 'def ' + self.name
s += render_signature(self.signature, 120 - len(s), prefix) + ': ...'
# Yes, this is a horrible hack, but inspect.py gives us no way to
# specify the function that should be used to format annotations.
for module in self.strip_modules:
s = s.replace(module + '.', '')
if self.kind == FunctionKind.CLASS:
s = prefix + "@classmethod\n" + s
elif self.kind == FunctionKind.STATIC:
s = prefix + "@staticmethod\n" + s
elif self.kind == FunctionKind.PROPERTY:
s = prefix + "@property\n" + s
elif self.kind == FunctionKind.DJANGO_CACHED_PROPERTY:
s = prefix + "@cached_property\n" + s
return s