hook-pydantic.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2020 PyInstaller Development Team.
  3. #
  4. # This file is distributed under the terms of the GNU General Public
  5. # License (version 2.0 or later).
  6. #
  7. # The full license is available in LICENSE, distributed with
  8. # this software.
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-or-later
  11. # ------------------------------------------------------------------
  12. from PyInstaller.utils.hooks import get_module_attribute, collect_submodules
  13. from PyInstaller.utils.hooks import is_module_satisfies
  14. # By default, PyPi wheels for pydantic < 2.0.0 come with all modules compiled as cython extensions, which prevents
  15. # PyInstaller from automatically picking up the submodules.
  16. if is_module_satisfies('pydantic >= 2.0.0'):
  17. # The `pydantic.compiled` attribute was removed in v2.
  18. is_compiled = False
  19. else:
  20. # NOTE: in PyInstaller 4.x and earlier, get_module_attribute() returns the string representation of the value
  21. # ('True'), while in PyInstaller 5.x and later, the actual value is returned (True).
  22. is_compiled = get_module_attribute('pydantic', 'compiled') in {'True', True}
  23. # Collect submodules from pydantic; even if the package is not compiled, contemporary versions (2.11.1 at the time
  24. # of writing) contain redirections and programmatic imports.
  25. hiddenimports = collect_submodules('pydantic')
  26. if is_compiled:
  27. # In compiled version, we need to collect the following modules from the standard library.
  28. hiddenimports += [
  29. 'colorsys',
  30. 'dataclasses',
  31. 'decimal',
  32. 'json',
  33. 'ipaddress',
  34. 'pathlib',
  35. 'uuid',
  36. # Optional dependencies.
  37. 'dotenv',
  38. 'email_validator'
  39. ]
  40. # Older releases (prior 1.4) also import distutils.version
  41. if not is_module_satisfies('pydantic >= 1.4'):
  42. hiddenimports += ['distutils.version']
  43. # Version 1.8.0 introduced additional dependency on typing_extensions
  44. if is_module_satisfies('pydantic >= 1.8'):
  45. hiddenimports += ['typing_extensions']