_compat.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import sys
  6. from ._typing import MYPY_CHECK_RUNNING
  7. if MYPY_CHECK_RUNNING: # pragma: no cover
  8. from typing import Any, Dict, Tuple, Type
  9. PY2 = sys.version_info[0] == 2
  10. PY3 = sys.version_info[0] == 3
  11. # flake8: noqa
  12. if PY3:
  13. string_types = (str,)
  14. else:
  15. string_types = (basestring,)
  16. def with_metaclass(meta, *bases):
  17. # type: (Type[Any], Tuple[Type[Any], ...]) -> Any
  18. """
  19. Create a base class with a metaclass.
  20. """
  21. # This requires a bit of explanation: the basic idea is to make a dummy
  22. # metaclass for one level of class instantiation that replaces itself with
  23. # the actual metaclass.
  24. class metaclass(meta): # type: ignore
  25. def __new__(cls, name, this_bases, d):
  26. # type: (Type[Any], str, Tuple[Any], Dict[Any, Any]) -> Any
  27. return meta(name, bases, d)
  28. return type.__new__(metaclass, "temporary_class", (), {})