typing_util.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Auxiliary definitions used in type annotations.
  2. """
  3. # Copyright The Mbed TLS Contributors
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from typing import Any
  18. # The typing_extensions module is necessary for type annotations that are
  19. # checked with mypy. It is only used for type annotations or to define
  20. # things that are themselves only used for type annotations. It is not
  21. # available on a default Python installation. Therefore, try loading
  22. # what we need from it for the sake of mypy (which depends on, or comes
  23. # with, typing_extensions), and if not define substitutes that lack the
  24. # static type information but are good enough at runtime.
  25. try:
  26. from typing_extensions import Protocol #pylint: disable=import-error
  27. except ImportError:
  28. class Protocol: #type: ignore
  29. #pylint: disable=too-few-public-methods
  30. pass
  31. class Writable(Protocol):
  32. """Abstract class for typing hints."""
  33. # pylint: disable=no-self-use,too-few-public-methods,unused-argument
  34. def write(self, text: str) -> Any:
  35. ...