Advertisement
mwchase

Internal annotation analysis function

Jun 6th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def _args_length(constructor, global_ns):
  2.     if isinstance(constructor, str):
  3.         ctor_ast = ast.parse(constructor, mode='eval')
  4.         if (
  5.                 isinstance(ctor_ast.body, ast.Subscript)
  6.                 and isinstance(ctor_ast.body.slice, ast.Index)):
  7.             index = ctor_ast.body.slice.value
  8.             ctor_ast.body = ctor_ast.body.value
  9.             try:
  10.                 value = eval(
  11.                     compile(ctor_ast, '<annotation>', 'eval'),
  12.                     global_ns,
  13.                     global_ns)
  14.             except Exception:
  15.                 return None
  16.             if value is Ctor:
  17.                 if isinstance(index, ast.Tuple):
  18.                     return len(index.elts)
  19.                 return 1
  20.         try:
  21.             constructor = eval(constructor, global_ns, global_ns)
  22.         except Exception:
  23.             return None
  24.     if issubclass(constructor, Ctor) and constructor is not Ctor:
  25.         return len(constructor.__args__)
  26.     return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement