Skip to content

Commit

Permalink
Add type hints for @TypeChecked and fix some pytype errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarnett committed Aug 3, 2020
1 parent befcedd commit 77bdc7b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Empty file added pytypes/py.typed
Empty file.
20 changes: 12 additions & 8 deletions pytypes/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,20 @@ def _preprocess_override(meth_types, base_types, meth_argspec, base_argspec):
while len(arg_types2) < len(base_arg_types2):
arg_types2.append(vargtype)
if not kw is None:
assert isinstance(argnames, list)
kw_type = arg_types[argnames.index(kw)]
if not base_kwonly is None:
# FIXME: This isn't guaranteed.
assert isinstance(base_argnames, list)
for name in base_kwonly:
base_arg_types2.append(base_arg_types[base_argnames.index(name)])
if name in argnames:
if argnames and name in argnames:
arg_types2.append(arg_types[argnames.index(name)])
else:
arg_types2.append(kw_type)
if not base_kw is None:
# FIXME: This isn't guaranteed.
assert isinstance(base_argnames, list)
base_arg_types2.append(base_arg_types[base_argnames.index(base_kw)])
arg_types2.append(kw_type)
return (typing.Tuple[tuple(arg_types2)], meth_types[1]), \
Expand Down Expand Up @@ -399,11 +404,10 @@ def override(func, auto = False):
# as it is just getting defined. Luckily we can get base-classes via inspect.stack():
stack = inspect.stack()
try:
base_classes = _re.search(r'class.+\((.+)\)\s*\:', stack[2][4][0]).group(1)
match = _re.search(r'class.+\((.+)\)\s*\:', stack[2][4][0])
base_classes = match.group(1) if match else 'object'
except IndexError:
raise _function_instead_of_method_error(func)
except AttributeError:
base_classes = 'object'
meth_cls_name = stack[1][3]
if func.__name__ == '__init__':
raise OverrideError(
Expand All @@ -425,8 +429,8 @@ def override(func, auto = False):
base_classes[i] = derived_class_locals[base_class]
elif base_class in derived_class_globals:
base_classes[i] = derived_class_globals[base_class]
elif base_class in types.__builtins__:
base_classes[i] = types.__builtins__[base_class]
elif hasattr(types, base_class):
base_classes[i] = getattr(types, base_class)
else:
raise TypeError("Could not lookup type: "+base_class)
else:
Expand All @@ -436,8 +440,8 @@ def override(func, auto = False):
obj = derived_class_locals[components[0]]
elif components[0] in derived_class_globals:
obj = derived_class_globals[components[0]]
elif components[0] in types.__builtins__:
obj = types.__builtins__[components[0]]
elif hasattr(types, components[0]):
obj = getattr(types, components[0])
elif components[0] in sys.modules:
obj = sys.modules[components[0]]
else:
Expand Down
8 changes: 8 additions & 0 deletions pytypes/typechecker.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from types import ModuleType
from typing import Callable, TypeVar, Union, overload

T = TypeVar('T', Callable, property, type, ModuleType)
@overload
def typechecked(memb: str) -> Union[str, ModuleType]: ...
@overload
def typechecked(memb: T) -> T: ...
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ max-line-length = 99

[wheel]
universal = 1

[options.package_data]
pytypes = py.typed, *.pyi

0 comments on commit 77bdc7b

Please sign in to comment.